Class GithubService

java.lang.Object
edu.ucsb.cs.scaffold.services.GithubService

@Service public class GithubService extends Object
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.
  • 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

      public List<GithubService.DirectoryEntry> listDirectory(String repoName, String path, String token)
      Lists the contents of a directory in the given repository, using the Contents API.
      Parameters:
      repoName - the repository in owner/repo form
      path - the directory path within the repository, e.g. questions/foo
      token - the user's PAT (plaintext)
      Returns:
      the entries, in the order GitHub returns them (alphabetical)
    • listSubdirectories

      public List<String> listSubdirectories(String repoName, String path, String token)
      Lists the names of the subdirectories of path in the given repository. Files at that path are ignored.
    • getFileContent

      public String getFileContent(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).
      Parameters:
      repoName - the repository in owner/repo form
      path - the file path within the repository, e.g. questions/foo/info.json
      token - the user's PAT (plaintext)
    • hasWriteAccess

      public boolean hasWriteAccess(String repoName, String token)
      Reports whether the token has push (read/write) access to the repository, from the permissions block of GET /repos/{owner}/{repo}. A successful call already proves read access; push=true additionally proves write access — nothing is written to check this.
      Parameters:
      repoName - the repository in owner/repo form
      token - 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)