Package edu.ucsb.cs.scaffold.services
Class GithubService
java.lang.Object
edu.ucsb.cs.scaffold.services.GithubService
Thin wrapper around the GitHub REST API, authenticated per call with a user's personal access
token. Callers handle GitHub's HTTP errors (401/403 = bad or unapproved token, 404 = missing repo
or path), which RestTemplate surfaces as HttpClientErrorException subclasses.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordOne entry of a directory listing:typeis"dir","file", etc. -
Constructor Summary
ConstructorsConstructorDescriptionGithubService(org.springframework.web.client.RestTemplate restTemplate, String githubApiBase, long retryInitialSleepSeconds, int retryMax, long rateLimitSleepInitialMs) -
Method Summary
Modifier and TypeMethodDescriptiongetFileContent(String repoName, String path, String token) Fetches the content of a file in the given repository as a UTF-8 string, using the Contents API (which returns file bodies base64-encoded).booleanhasWriteAccess(String repoName, String token) Reports whether the token has push (read/write) access to the repository, from thepermissionsblock of GET /repos/{owner}/{repo}.listDirectory(String repoName, String path, String token) Lists the contents of a directory in the given repository, using the Contents API.listSubdirectories(String repoName, String path, String token) Lists the names of the subdirectories ofpathin the given repository.
-
Constructor Details
-
GithubService
public GithubService(org.springframework.web.client.RestTemplate restTemplate, @Value("${github.api.base:https://api.github.com}") String githubApiBase, @Value("${GITHUB_SERVICE_RETRY_INITIAL_SLEEP_SECONDS:8}") long retryInitialSleepSeconds, @Value("${GITHUB_SERVICE_RETRY_MAX:3}") int retryMax, @Value("${GITHUB_SERVICE_RATE_LIMIT_SLEEP_INITIAL_MS:1000}") long rateLimitSleepInitialMs)
-
-
Method Details
-
listDirectory
Lists the contents of a directory in the given repository, using the Contents API.- Parameters:
repoName- the repository inowner/repoformpath- the directory path within the repository, e.g.questions/footoken- the user's PAT (plaintext)- Returns:
- the entries, in the order GitHub returns them (alphabetical)
-
listSubdirectories
Lists the names of the subdirectories ofpathin the given repository. Files at that path are ignored. -
getFileContent
Fetches the content of a file in the given repository as a UTF-8 string, using the Contents API (which returns file bodies base64-encoded).- Parameters:
repoName- the repository inowner/repoformpath- the file path within the repository, e.g.questions/foo/info.jsontoken- the user's PAT (plaintext)
-
hasWriteAccess
Reports whether the token has push (read/write) access to the repository, from thepermissionsblock of GET /repos/{owner}/{repo}. A successful call already proves read access;push=trueadditionally proves write access — nothing is written to check this.- Parameters:
repoName- the repository inowner/repoformtoken- the user's PAT (plaintext)- Throws:
org.springframework.web.client.HttpClientErrorException- when the token cannot read the repo at all (GitHub answers 404 for repos a token cannot see, or 401 for a bad token)
-