From bc8872c39403757a6176b2b97d06f69536e55bce Mon Sep 17 00:00:00 2001 From: Shrikrishna Singh Date: Tue, 11 Jun 2019 00:36:11 +0530 Subject: [PATCH] Add Support to preview endpoints to list branches or pull requests for a commit --- github/github-accessors.go | 24 ++++++++++++++++++++++++ github/github.go | 3 +++ github/pulls.go | 29 +++++++++++++++++++++++++++++ github/pulls_test.go | 31 +++++++++++++++++++++++++++++++ github/repos_commits.go | 30 ++++++++++++++++++++++++++++++ github/repos_commits_test.go | 20 ++++++++++++++++++++ 6 files changed, 137 insertions(+) diff --git a/github/github-accessors.go b/github/github-accessors.go index 62fddd20625..ba872be39cb 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -484,6 +484,30 @@ func (b *Branch) GetProtected() bool { return *b.Protected } +// GetCommit returns the Commit field. +func (b *BranchCommit) GetCommit() *Commit { + if b == nil { + return nil + } + return b.Commit +} + +// GetName returns the Name field if it's non-nil, zero value otherwise. +func (b *BranchCommit) GetName() string { + if b == nil || b.Name == nil { + return "" + } + return *b.Name +} + +// GetProtected returns the Protected field if it's non-nil, zero value otherwise. +func (b *BranchCommit) GetProtected() string { + if b == nil || b.Protected == nil { + return "" + } + return *b.Protected +} + // GetApp returns the App field. func (c *CheckRun) GetApp() *App { if c == nil { diff --git a/github/github.go b/github/github.go index bfe0453e23f..63f8a966439 100644 --- a/github/github.go +++ b/github/github.go @@ -140,6 +140,9 @@ const ( // https://developer.github.com/changes/2019-05-29-update-branch-api/ mediaTypeUpdatePullRequestBranchPreview = "application/vnd.github.lydian-preview+json" + + // https://developer.github.com/changes/2019-04-11-pulls-branches-for-commit/ + mediaTypeListPullsOrBranchesForCommitPreview = "application/vnd.github.groot-preview+json" ) // A Client manages communication with the GitHub API. diff --git a/github/pulls.go b/github/pulls.go index a8670f9b316..a06f97bcfd6 100644 --- a/github/pulls.go +++ b/github/pulls.go @@ -159,6 +159,35 @@ func (s *PullRequestsService) List(ctx context.Context, owner string, repo strin return pulls, resp, nil } +// ListPullRequestsWithCommit returns pull requests associated with a commit SHA. +// +// The results will include open and closed pull requests. +// +// GitHub API docs: https://developer.github.com/v3/repos/commits/#list-pull-requests-associated-with-commit +func (s *PullRequestsService) ListPullRequestsWithCommit(ctx context.Context, owner, repo, sha string, opt *PullRequestListOptions) ([]*PullRequest, *Response, error) { + u := fmt.Sprintf("repos/%v/%v/commits/%v/pulls", owner, repo, sha) + u, err := addOptions(u, opt) + if err != nil { + return nil, nil, err + } + + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + // TODO: remove custom Accept header when this API fully launches. + acceptHeaders := []string{mediaTypeListPullsOrBranchesForCommitPreview, mediaTypeDraftPreview, mediaTypeLabelDescriptionSearchPreview, mediaTypeLockReasonPreview} + req.Header.Set("Accept", strings.Join(acceptHeaders, ", ")) + var pulls []*PullRequest + resp, err := s.client.Do(ctx, req, &pulls) + if err != nil { + return nil, resp, err + } + + return pulls, resp, nil +} + // Get a single pull request. // // GitHub API docs: https://developer.github.com/v3/pulls/#get-a-single-pull-request diff --git a/github/pulls_test.go b/github/pulls_test.go index ba45b6fd61e..09d60e3a3e4 100644 --- a/github/pulls_test.go +++ b/github/pulls_test.go @@ -47,6 +47,37 @@ func TestPullRequestsService_List(t *testing.T) { } } +func TestPullRequestsService_ListPullRequestsWithCommit(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + wantAcceptHeaders := []string{mediaTypeListPullsOrBranchesForCommitPreview, mediaTypeDraftPreview, mediaTypeLabelDescriptionSearchPreview, mediaTypeLockReasonPreview} + mux.HandleFunc("/repos/o/r/commits/sha/pulls", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) + testFormValues(t, r, values{ + "state": "closed", + "head": "h", + "base": "b", + "sort": "created", + "direction": "desc", + "page": "2", + }) + fmt.Fprint(w, `[{"number":1}]`) + }) + + opt := &PullRequestListOptions{"closed", "h", "b", "created", "desc", ListOptions{Page: 2}} + pulls, _, err := client.PullRequests.ListPullRequestsWithCommit(context.Background(), "o", "r", "sha", opt) + if err != nil { + t.Errorf("PullRequests.ListPullRequestsWithCommit returned error: %v", err) + } + + want := []*PullRequest{{Number: Int(1)}} + if !reflect.DeepEqual(pulls, want) { + t.Errorf("PullRequests.ListPullRequestsWithCommit returned %+v, want %+v", pulls, want) + } +} + func TestPullRequestsService_List_invalidOwner(t *testing.T) { client, _, _, teardown := setup() defer teardown() diff --git a/github/repos_commits.go b/github/repos_commits.go index 83cdf5b3bf6..95096923432 100644 --- a/github/repos_commits.go +++ b/github/repos_commits.go @@ -115,6 +115,13 @@ type CommitsListOptions struct { ListOptions } +// BranchCommit is the result of listing branches with commit SHA. +type BranchCommit struct { + Name *string `json:"name,omitempty"` + Commit *Commit `json:"commit,omitempty"` + Protected *string `json:"protected,omitempty"` +} + // ListCommits lists the commits of a repository. // // GitHub API docs: https://developer.github.com/v3/repos/commits/#list @@ -232,3 +239,26 @@ func (s *RepositoriesService) CompareCommits(ctx context.Context, owner, repo st return comp, resp, nil } + +// ListBranchesHeadCommit gets all branches where the given commit SHA is the HEAD, +// or latest commit for the branch. +// +// GitHub API docs: https://developer.github.com/v3/repos/commits/#list-branches-for-head-commit +func (s *RepositoriesService) ListBranchesHeadCommit(ctx context.Context, owner, repo, sha string) ([]*BranchCommit, *Response, error) { + u := fmt.Sprintf("repos/%v/%v/commits/%v/branches-where-head", owner, repo, sha) + + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + // TODO: remove custom Accept header when this API fully launches. + req.Header.Set("Accept", mediaTypeListPullsOrBranchesForCommitPreview) + var branchCommits []*BranchCommit + resp, err := s.client.Do(ctx, req, &branchCommits) + if err != nil { + return nil, resp, err + } + + return branchCommits, resp, nil +} diff --git a/github/repos_commits_test.go b/github/repos_commits_test.go index e076febb5a4..feed2b95d04 100644 --- a/github/repos_commits_test.go +++ b/github/repos_commits_test.go @@ -360,3 +360,23 @@ func TestRepositoriesService_CompareCommits(t *testing.T) { t.Errorf("Repositories.CompareCommits returned \n%+v, want \n%+v", got, want) } } + +func TestRepositoriesService_ListBranchesHeadCommit(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/commits/s/branches-where-head", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprintf(w, `[{"name": "b"}]`) + }) + + branches, _, err := client.Repositories.ListBranchesHeadCommit(context.Background(), "o", "r", "s") + if err != nil { + t.Errorf("Repositories.ListBranchesHeadCommit returned error: %v", err) + } + + want := []*BranchCommit{{Name: String("b")}} + if !reflect.DeepEqual(branches, want) { + t.Errorf("Repositories.ListBranchesHeadCommit returned %+v, want %+v", branches, want) + } +}