+
Skip to content

Add Support to preview endpoints to list branches or pull requests fo… #1186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载