Browse Source

Add squashing capability to the pull request merge function.

Fixes #324.
Fixes #353.

Change-Id: If176b98b951db7f2e45ad7578bb8eeac13ffdbc2
Garrett Squire 10 years ago
committed by Glenn Lewis
parent
commit
796cdb74b6
3 changed files with 21 additions and 5 deletions
  1. +3
    -0
      github/github.go
  2. +15
    -4
      github/pulls.go
  3. +3
    -1
      github/pulls_test.go

+ 3
- 0
github/github.go View File

@ -69,6 +69,9 @@ const (
// https://developer.github.com/changes/2016-05-12-reactions-api-preview/
mediaTypeReactionsPreview = "application/vnd.github.squirrel-girl-preview"
// https://developer.github.com/changes/2016-04-01-squash-api-preview/
mediaTypeSquashPreview = "application/vnd.github.polaris-preview+json"
)
// A Client manages communication with the GitHub API.


+ 15
- 4
github/pulls.go View File

@ -248,19 +248,30 @@ type PullRequestMergeResult struct {
Message *string `json:"message,omitempty"`
}
// PullRequestOptions lets you define how a pull request will be merged.
type PullRequestOptions struct {
Squash bool
}
type pullRequestMergeRequest struct {
CommitMessage *string `json:"commit_message"`
Squash *bool `json:"squash,omitempty"`
}
// Merge a pull request (Merge Button™).
//
// GitHub API docs: https://developer.github.com/v3/pulls/#merge-a-pull-request-merge-buttontrade
func (s *PullRequestsService) Merge(owner string, repo string, number int, commitMessage string) (*PullRequestMergeResult, *Response, error) {
func (s *PullRequestsService) Merge(owner string, repo string, number int, commitMessage string, options *PullRequestOptions) (*PullRequestMergeResult, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/pulls/%d/merge", owner, repo, number)
req, err := s.client.NewRequest("PUT", u, &pullRequestMergeRequest{
CommitMessage: &commitMessage,
})
pullRequestBody := &pullRequestMergeRequest{CommitMessage: &commitMessage}
if options != nil {
pullRequestBody.Squash = &options.Squash
}
req, err := s.client.NewRequest("PUT", u, pullRequestBody)
// TODO: This header will be unnecessary when the API is no longer in preview.
req.Header.Set("Accept", mediaTypeSquashPreview)
if err != nil {
return nil, nil, err


+ 3
- 1
github/pulls_test.go View File

@ -341,6 +341,7 @@ func TestPullRequestsService_Merge(t *testing.T) {
mux.HandleFunc("/repos/o/r/pulls/1/merge", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
testHeader(t, r, "Accept", mediaTypeSquashPreview)
fmt.Fprint(w, `
{
"sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
@ -349,7 +350,8 @@ func TestPullRequestsService_Merge(t *testing.T) {
}`)
})
merge, _, err := client.PullRequests.Merge("o", "r", 1, "merging pull request")
options := &PullRequestOptions{Squash: true}
merge, _, err := client.PullRequests.Merge("o", "r", 1, "merging pull request", options)
if err != nil {
t.Errorf("PullRequests.Merge returned error: %v", err)
}


Loading…
Cancel
Save