Skip to content

Commit

Permalink
backend: Add ListCheckRunsForRef method to GitHub service (#3218)
Browse files Browse the repository at this point in the history
  • Loading branch information
septum authored Feb 6, 2025
1 parent 3b1b6eb commit 6ed09ed
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
14 changes: 14 additions & 0 deletions backend/mock/service/githubmock/githubmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ func (s *svc) GetFileContents(ctx context.Context, ref *github.RemoteRef, path s
}, nil
}

func (s *svc) ListCheckRunsForRef(ctx context.Context, ref *github.RemoteRef, opts *githubv3.ListCheckRunsOptions) (*githubv3.ListCheckRunsResults, error) {
var checkRuns []*githubv3.CheckRun

checkRuns = append(checkRuns, &githubv3.CheckRun{
Status: githubv3.String("completed"),
Conclusion: githubv3.String("success"),
})

return &githubv3.ListCheckRunsResults{
Total: githubv3.Int(1),
CheckRuns: checkRuns,
}, nil
}

func NewAsService(*any.Any, *zap.Logger, tally.Scope) (service.Service, error) {
return New(), nil
}
13 changes: 10 additions & 3 deletions backend/service/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ type Client interface {
CreateCommit(ctx context.Context, ref *RemoteRef, message string, files FileMap) (*Commit, error)
SearchCode(ctx context.Context, query string, opts *githubv3.SearchOptions) (*githubv3.CodeSearchResult, error)
GetFileContents(ctx context.Context, ref *RemoteRef, path string) (*githubv3.RepositoryContent, error)
ListCheckRunsForRef(ctx context.Context, ref *RemoteRef, opts *githubv3.ListCheckRunsOptions) (*githubv3.ListCheckRunsResults, error)
}

// This func can be used to create comments for PRs or Issues
Expand Down Expand Up @@ -422,6 +423,7 @@ func newService(config *githubv1.Config, scope tally.Scope, logger *zap.Logger)
Repositories: restClient.Repositories,
Search: restClient.Search,
Users: restClient.Users,
Checks: restClient.Checks,
}

httpClient.Transport = transport
Expand Down Expand Up @@ -707,9 +709,6 @@ func (s *svc) GetFileContents(ctx context.Context, ref *RemoteRef, filePath stri
s.httpTransport.AcceptRaw = true
file, _, _, err = s.rest.Repositories.GetContents(ctx, ref.RepoOwner, ref.RepoName, filePath, options)
s.httpTransport.AcceptRaw = false
if err != nil {
return nil, err
}

file.Path = &filePath
file.Name = githubv3.String(path.Base(filePath))
Expand All @@ -719,3 +718,11 @@ func (s *svc) GetFileContents(ctx context.Context, ref *RemoteRef, filePath stri

return file, nil
}

func (s *svc) ListCheckRunsForRef(ctx context.Context, ref *RemoteRef, opts *githubv3.ListCheckRunsOptions) (*githubv3.ListCheckRunsResults, error) {
results, _, err := s.rest.Checks.ListCheckRunsForRef(ctx, ref.RepoOwner, ref.RepoName, ref.Ref, opts)
if err != nil {
return nil, err
}
return results, nil
}
8 changes: 8 additions & 0 deletions backend/service/github/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type v3client struct {
Repositories v3repositories
Search v3search
Users v3users
Checks v3checks
}

type v4client interface {
Expand Down Expand Up @@ -103,3 +104,10 @@ type v3users interface {
// Get fetches a user. Passing the empty string will fetch the authenticated user.
Get(ctx context.Context, user string) (*githubv3.User, *githubv3.Response, error)
}

// Interface for struct defined in https://github.com/google/go-github/blob/master/github/checks.go
// Method comments below reproduced directly from original definition linked above.
type v3checks interface {
// Lists check runs for a specific ref.
ListCheckRunsForRef(ctx context.Context, owner, repo, ref string, opts *githubv3.ListCheckRunsOptions) (*githubv3.ListCheckRunsResults, *githubv3.Response, error)
}

0 comments on commit 6ed09ed

Please sign in to comment.