From 6ed09ed31151605f76f23c9fb34fe4d9c45e9d8d Mon Sep 17 00:00:00 2001 From: Orlando Valverde <4467518+septum@users.noreply.github.com> Date: Thu, 6 Feb 2025 14:27:50 -0600 Subject: [PATCH] backend: Add ListCheckRunsForRef method to GitHub service (#3218) --- backend/mock/service/githubmock/githubmock.go | 14 ++++++++++++++ backend/service/github/github.go | 13 ++++++++++--- backend/service/github/iface.go | 8 ++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/backend/mock/service/githubmock/githubmock.go b/backend/mock/service/githubmock/githubmock.go index ce809b6ad5..0ecc9f1eef 100644 --- a/backend/mock/service/githubmock/githubmock.go +++ b/backend/mock/service/githubmock/githubmock.go @@ -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 } diff --git a/backend/service/github/github.go b/backend/service/github/github.go index 36683175b0..43eec93906 100644 --- a/backend/service/github/github.go +++ b/backend/service/github/github.go @@ -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 @@ -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 @@ -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)) @@ -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 +} diff --git a/backend/service/github/iface.go b/backend/service/github/iface.go index dffdd475fa..04b8e724f3 100644 --- a/backend/service/github/iface.go +++ b/backend/service/github/iface.go @@ -17,6 +17,7 @@ type v3client struct { Repositories v3repositories Search v3search Users v3users + Checks v3checks } type v4client interface { @@ -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) +}