Skip to content

Commit 361256a

Browse files
sks444gmlewis
authored andcommitted
Add preview support to list branches or PRs for a commit (#1186)
Fixes #1151.
1 parent 6a35880 commit 361256a

File tree

6 files changed

+137
-0
lines changed

6 files changed

+137
-0
lines changed

github/github-accessors.go

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ const (
140140

141141
// https://developer.github.com/changes/2019-05-29-update-branch-api/
142142
mediaTypeUpdatePullRequestBranchPreview = "application/vnd.github.lydian-preview+json"
143+
144+
// https://developer.github.com/changes/2019-04-11-pulls-branches-for-commit/
145+
mediaTypeListPullsOrBranchesForCommitPreview = "application/vnd.github.groot-preview+json"
143146
)
144147

145148
// A Client manages communication with the GitHub API.

github/pulls.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,35 @@ func (s *PullRequestsService) List(ctx context.Context, owner string, repo strin
159159
return pulls, resp, nil
160160
}
161161

162+
// ListPullRequestsWithCommit returns pull requests associated with a commit SHA.
163+
//
164+
// The results will include open and closed pull requests.
165+
//
166+
// GitHub API docs: https://developer.github.com/v3/repos/commits/#list-pull-requests-associated-with-commit
167+
func (s *PullRequestsService) ListPullRequestsWithCommit(ctx context.Context, owner, repo, sha string, opt *PullRequestListOptions) ([]*PullRequest, *Response, error) {
168+
u := fmt.Sprintf("repos/%v/%v/commits/%v/pulls", owner, repo, sha)
169+
u, err := addOptions(u, opt)
170+
if err != nil {
171+
return nil, nil, err
172+
}
173+
174+
req, err := s.client.NewRequest("GET", u, nil)
175+
if err != nil {
176+
return nil, nil, err
177+
}
178+
179+
// TODO: remove custom Accept header when this API fully launches.
180+
acceptHeaders := []string{mediaTypeListPullsOrBranchesForCommitPreview, mediaTypeDraftPreview, mediaTypeLabelDescriptionSearchPreview, mediaTypeLockReasonPreview}
181+
req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
182+
var pulls []*PullRequest
183+
resp, err := s.client.Do(ctx, req, &pulls)
184+
if err != nil {
185+
return nil, resp, err
186+
}
187+
188+
return pulls, resp, nil
189+
}
190+
162191
// Get a single pull request.
163192
//
164193
// GitHub API docs: https://developer.github.com/v3/pulls/#get-a-single-pull-request

github/pulls_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,37 @@ func TestPullRequestsService_List(t *testing.T) {
4747
}
4848
}
4949

50+
func TestPullRequestsService_ListPullRequestsWithCommit(t *testing.T) {
51+
client, mux, _, teardown := setup()
52+
defer teardown()
53+
54+
wantAcceptHeaders := []string{mediaTypeListPullsOrBranchesForCommitPreview, mediaTypeDraftPreview, mediaTypeLabelDescriptionSearchPreview, mediaTypeLockReasonPreview}
55+
mux.HandleFunc("/repos/o/r/commits/sha/pulls", func(w http.ResponseWriter, r *http.Request) {
56+
testMethod(t, r, "GET")
57+
testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", "))
58+
testFormValues(t, r, values{
59+
"state": "closed",
60+
"head": "h",
61+
"base": "b",
62+
"sort": "created",
63+
"direction": "desc",
64+
"page": "2",
65+
})
66+
fmt.Fprint(w, `[{"number":1}]`)
67+
})
68+
69+
opt := &PullRequestListOptions{"closed", "h", "b", "created", "desc", ListOptions{Page: 2}}
70+
pulls, _, err := client.PullRequests.ListPullRequestsWithCommit(context.Background(), "o", "r", "sha", opt)
71+
if err != nil {
72+
t.Errorf("PullRequests.ListPullRequestsWithCommit returned error: %v", err)
73+
}
74+
75+
want := []*PullRequest{{Number: Int(1)}}
76+
if !reflect.DeepEqual(pulls, want) {
77+
t.Errorf("PullRequests.ListPullRequestsWithCommit returned %+v, want %+v", pulls, want)
78+
}
79+
}
80+
5081
func TestPullRequestsService_List_invalidOwner(t *testing.T) {
5182
client, _, _, teardown := setup()
5283
defer teardown()

github/repos_commits.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ type CommitsListOptions struct {
115115
ListOptions
116116
}
117117

118+
// BranchCommit is the result of listing branches with commit SHA.
119+
type BranchCommit struct {
120+
Name *string `json:"name,omitempty"`
121+
Commit *Commit `json:"commit,omitempty"`
122+
Protected *string `json:"protected,omitempty"`
123+
}
124+
118125
// ListCommits lists the commits of a repository.
119126
//
120127
// 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
232239

233240
return comp, resp, nil
234241
}
242+
243+
// ListBranchesHeadCommit gets all branches where the given commit SHA is the HEAD,
244+
// or latest commit for the branch.
245+
//
246+
// GitHub API docs: https://developer.github.com/v3/repos/commits/#list-branches-for-head-commit
247+
func (s *RepositoriesService) ListBranchesHeadCommit(ctx context.Context, owner, repo, sha string) ([]*BranchCommit, *Response, error) {
248+
u := fmt.Sprintf("repos/%v/%v/commits/%v/branches-where-head", owner, repo, sha)
249+
250+
req, err := s.client.NewRequest("GET", u, nil)
251+
if err != nil {
252+
return nil, nil, err
253+
}
254+
255+
// TODO: remove custom Accept header when this API fully launches.
256+
req.Header.Set("Accept", mediaTypeListPullsOrBranchesForCommitPreview)
257+
var branchCommits []*BranchCommit
258+
resp, err := s.client.Do(ctx, req, &branchCommits)
259+
if err != nil {
260+
return nil, resp, err
261+
}
262+
263+
return branchCommits, resp, nil
264+
}

github/repos_commits_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,3 +360,23 @@ func TestRepositoriesService_CompareCommits(t *testing.T) {
360360
t.Errorf("Repositories.CompareCommits returned \n%+v, want \n%+v", got, want)
361361
}
362362
}
363+
364+
func TestRepositoriesService_ListBranchesHeadCommit(t *testing.T) {
365+
client, mux, _, teardown := setup()
366+
defer teardown()
367+
368+
mux.HandleFunc("/repos/o/r/commits/s/branches-where-head", func(w http.ResponseWriter, r *http.Request) {
369+
testMethod(t, r, "GET")
370+
fmt.Fprintf(w, `[{"name": "b"}]`)
371+
})
372+
373+
branches, _, err := client.Repositories.ListBranchesHeadCommit(context.Background(), "o", "r", "s")
374+
if err != nil {
375+
t.Errorf("Repositories.ListBranchesHeadCommit returned error: %v", err)
376+
}
377+
378+
want := []*BranchCommit{{Name: String("b")}}
379+
if !reflect.DeepEqual(branches, want) {
380+
t.Errorf("Repositories.ListBranchesHeadCommit returned %+v, want %+v", branches, want)
381+
}
382+
}

0 commit comments

Comments
 (0)