Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions github/githubclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ func (c *client) GetInfo(ctx context.Context, gitcmd git.GitInterface) *github.G
c.config.Repo.GitHubRepoOwner,
c.config.Repo.GitHubRepoName)
check(err)
check(checkRepository(resp.Repository == nil, c.config.Repo.GitHubRepoOwner, c.config.Repo.GitHubRepoName))
pullRequestConnection = resp.Viewer.PullRequests
loginName = resp.Viewer.Login
repoID = resp.Repository.Id
Expand All @@ -208,6 +209,7 @@ func (c *client) GetInfo(ctx context.Context, gitcmd git.GitInterface) *github.G
c.config.Repo.GitHubRepoOwner,
c.config.Repo.GitHubRepoName)
check(err)
check(checkRepository(resp.Repository == nil, c.config.Repo.GitHubRepoOwner, c.config.Repo.GitHubRepoName))
pullRequestConnection = resp.Viewer.PullRequests
loginName = resp.Viewer.Login
repoID = resp.Repository.Id
Expand Down Expand Up @@ -842,6 +844,17 @@ func computeRequiredCheckStatus(contexts []checkContextNode, requiredChecks map[
return github.CheckStatusPass
}

// checkRepository returns an error when the GitHub GraphQL query resolved the
// repository to null. The API returns a nil repository (sometimes without a
// top-level error) when the token cannot access the repository or the
// owner/name is wrong, and dereferencing it would panic.
func checkRepository(isNil bool, owner, name string) error {
if isNil {
return fmt.Errorf("repository %s/%s not found; check the repository name and that GITHUB_TOKEN has access to it", owner, name)
}
return nil
}

func check(err error) {
if err != nil {
msg := err.Error()
Expand Down
9 changes: 9 additions & 0 deletions github/githubclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -753,3 +753,12 @@ func TestComputeRequiredCheckStatus(t *testing.T) {
})
}
}

func TestCheckRepository(t *testing.T) {
err := checkRepository(false, "ejoffe", "spr")
require.NoError(t, err)

err = checkRepository(true, "ejoffe", "spr")
require.Error(t, err)
require.Contains(t, err.Error(), "ejoffe/spr")
}