From c3c421acaf12027079009b6c68eb87b29a1bc048 Mon Sep 17 00:00:00 2001 From: Orlando Valverde <4467518+septum@users.noreply.github.com> Date: Wed, 21 Aug 2024 11:05:55 -0600 Subject: [PATCH] backend: Update GitHub Service to get a PR (#3095) --- backend/mock/service/githubmock/githubmock.go | 6 ++ backend/service/github/github.go | 9 +++ backend/service/github/github_test.go | 59 +++++++++++++++++++ backend/service/github/iface.go | 2 + 4 files changed, 76 insertions(+) diff --git a/backend/mock/service/githubmock/githubmock.go b/backend/mock/service/githubmock/githubmock.go index e0b87d792b..d01dae344d 100644 --- a/backend/mock/service/githubmock/githubmock.go +++ b/backend/mock/service/githubmock/githubmock.go @@ -80,6 +80,12 @@ func (s svc) ListPullRequestsWithCommit(ctx context.Context, ref *github.RemoteR }, nil } +func (s svc) GetPullRequest(ctx context.Context, owner, repo string, number int) (*githubv3.PullRequest, error) { + return &githubv3.PullRequest{ + Number: githubv3.Int(4242), + }, nil +} + func (s svc) GetOrgMembership(ctx context.Context, user, org string) (*githubv3.Membership, error) { role := "member" return &githubv3.Membership{Role: &role}, nil diff --git a/backend/service/github/github.go b/backend/service/github/github.go index 85dc948c4d..a96286dd32 100644 --- a/backend/service/github/github.go +++ b/backend/service/github/github.go @@ -125,6 +125,7 @@ type Client interface { ListPullRequestsWithCommit(ctx context.Context, ref *RemoteRef, sha string, opts *githubv3.ListOptions) ([]*PullRequestInfo, error) GetOrgMembership(ctx context.Context, user, org string) (*githubv3.Membership, error) GetUser(ctx context.Context, username string) (*githubv3.User, error) + GetPullRequest(ctx context.Context, owner, repo string, number int) (*githubv3.PullRequest, error) } // This func can be used to create comments for PRs or Issues @@ -299,6 +300,14 @@ func (s *svc) ListPullRequestsWithCommit(ctx context.Context, ref *RemoteRef, sh return prInfos, nil } +func (s *svc) GetPullRequest(ctx context.Context, owner, repo string, number int) (*githubv3.PullRequest, error) { + pr, _, err := s.rest.PullRequests.Get(ctx, owner, repo, number) + if err != nil { + return nil, err + } + return pr, nil +} + type CreateBranchRequest struct { // The base for the new branch. Ref *RemoteRef diff --git a/backend/service/github/github_test.go b/backend/service/github/github_test.go index 256a806d9b..7803e884e6 100644 --- a/backend/service/github/github_test.go +++ b/backend/service/github/github_test.go @@ -896,6 +896,13 @@ func (m *mockPullRequests) Create(ctx context.Context, owner string, repo string return &githubv3.PullRequest{}, &githubv3.Response{}, nil } +func (m *mockPullRequests) Get(ctx context.Context, owner, repo string, number int) (*githubv3.PullRequest, *githubv3.Response, error) { + if m.generalError == true { + return nil, nil, errors.New(problem) + } + return &githubv3.PullRequest{}, &githubv3.Response{}, nil +} + // Mock of ListPullRequestsWithCommit API func (m *mockPullRequests) ListPullRequestsWithCommit(ctx context.Context, owner, repo, sha string, opts *githubv3.ListOptions) ([]*githubv3.PullRequest, *githubv3.Response, error) { if m.generalError { @@ -985,6 +992,58 @@ func TestListPullRequestsWithCommit(t *testing.T) { } } +var getPullRequestTests = []struct { + name string + errorText string + mockPullReq *mockPullRequests + repoOwner string + repoName string + prNumber int +}{ + { + name: "happy path", + mockPullReq: &mockPullRequests{}, + repoOwner: "my-org", + repoName: "my-repo", + prNumber: 4242, + }, + { + name: "v3 client error", + mockPullReq: &mockPullRequests{generalError: true}, + errorText: "we've had a problem", + repoOwner: "my-org", + repoName: "my-repo", + prNumber: 4242, + }, +} + +func TestGetPullRequest(t *testing.T) { + for _, tt := range getPullRequestTests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + s := &svc{rest: v3client{ + PullRequests: tt.mockPullReq, + }} + + resp, err := s.GetPullRequest( + context.Background(), + tt.repoOwner, + tt.repoName, + tt.prNumber) + + if tt.errorText != "" { + assert.Error(t, err) + assert.Contains(t, err.Error(), tt.errorText) + } else { + assert.NoError(t, err) + assert.Equal(t, tt.mockPullReq.actualNumber, resp.GetNumber()) + } + }) + } +} + func TestNewService(t *testing.T) { cfg := &githubconfigv1.Config{} _, err := newService(cfg, tally.NoopScope, zap.NewNop()) diff --git a/backend/service/github/iface.go b/backend/service/github/iface.go index 7c3c45bd9e..487a4144f5 100644 --- a/backend/service/github/iface.go +++ b/backend/service/github/iface.go @@ -35,6 +35,8 @@ type v3pullrequests interface { Create(ctx context.Context, owner string, repo string, pull *githubv3.NewPullRequest) (*githubv3.PullRequest, *githubv3.Response, error) // ListPullRequestsWithCommit returns pull requests associated with a commit SHA. ListPullRequestsWithCommit(ctx context.Context, owner, repo, sha string, opts *githubv3.ListOptions) ([]*githubv3.PullRequest, *githubv3.Response, error) + // GetPullRequest returns a single pull request specified by the PR number + Get(ctx context.Context, owner, repo string, number int) (*githubv3.PullRequest, *githubv3.Response, error) } type v4client interface {