Skip to content

Commit

Permalink
backend: Update GitHub Service to get a PR (#3095)
Browse files Browse the repository at this point in the history
  • Loading branch information
septum authored Aug 21, 2024
1 parent 9d52695 commit c3c421a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
6 changes: 6 additions & 0 deletions backend/mock/service/githubmock/githubmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions backend/service/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
59 changes: 59 additions & 0 deletions backend/service/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 2 additions & 0 deletions backend/service/github/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit c3c421a

Please sign in to comment.