Skip to content

Commit

Permalink
Add preview support to list branches or PRs for a commit (#1186)
Browse files Browse the repository at this point in the history
Fixes #1151.
  • Loading branch information
sks444 authored and gmlewis committed Jun 10, 2019
1 parent 6a35880 commit 361256a
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 0 deletions.
24 changes: 24 additions & 0 deletions github/github-accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 29 additions & 0 deletions github/pulls.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions github/pulls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
30 changes: 30 additions & 0 deletions github/repos_commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
20 changes: 20 additions & 0 deletions github/repos_commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 361256a

Please sign in to comment.