From 575b0b37560d9485f6d3bcb220a9111495b9c248 Mon Sep 17 00:00:00 2001 From: Garrett Squire Date: Mon, 10 Oct 2016 12:53:30 -0700 Subject: [PATCH] add support for merge method and repository squash settings Closes #437. Closes #446. Change-Id: I7a9bd0717310ef016b2695fe0e5cffb8e8b86ff8 --- github/github.go | 1 + github/pulls.go | 11 ++++++++--- github/pulls_test.go | 2 +- github/repos.go | 14 ++++++++++++-- github/repos_test.go | 4 +++- 5 files changed, 25 insertions(+), 7 deletions(-) diff --git a/github/github.go b/github/github.go index c52642d..53ce988 100644 --- a/github/github.go +++ b/github/github.go @@ -68,6 +68,7 @@ const ( mediaTypeReactionsPreview = "application/vnd.github.squirrel-girl-preview" // https://developer.github.com/changes/2016-04-01-squash-api-preview/ + // https://developer.github.com/changes/2016-09-26-pull-request-merge-api-update/ mediaTypeSquashPreview = "application/vnd.github.polaris-preview+json" // https://developer.github.com/changes/2016-04-04-git-signing-api-preview/ diff --git a/github/pulls.go b/github/pulls.go index 3c88365..85e04f8 100644 --- a/github/pulls.go +++ b/github/pulls.go @@ -253,12 +253,16 @@ type PullRequestMergeResult struct { // PullRequestOptions lets you define how a pull request will be merged. type PullRequestOptions struct { - Squash bool + CommitTitle string + + // The merge method to use. Possible values include: "merge", "squash", and "rebase" with the default being merge. + MergeMethod string } type pullRequestMergeRequest struct { CommitMessage *string `json:"commit_message"` - Squash *bool `json:"squash,omitempty"` + CommitTitle *string `json:"commit_title,omitempty"` + MergeMethod *string `json:"merge_method,omitempty"` } // Merge a pull request (Merge Button™). @@ -269,7 +273,8 @@ func (s *PullRequestsService) Merge(owner string, repo string, number int, commi pullRequestBody := &pullRequestMergeRequest{CommitMessage: &commitMessage} if options != nil { - pullRequestBody.Squash = &options.Squash + pullRequestBody.CommitTitle = &options.CommitTitle + pullRequestBody.MergeMethod = &options.MergeMethod } req, err := s.client.NewRequest("PUT", u, pullRequestBody) diff --git a/github/pulls_test.go b/github/pulls_test.go index de69cb0..31d41e1 100644 --- a/github/pulls_test.go +++ b/github/pulls_test.go @@ -363,7 +363,7 @@ func TestPullRequestsService_Merge(t *testing.T) { }`) }) - options := &PullRequestOptions{Squash: true} + options := &PullRequestOptions{MergeMethod: "rebase"} merge, _, err := client.PullRequests.Merge("o", "r", 1, "merging pull request", options) if err != nil { t.Errorf("PullRequests.Merge returned error: %v", err) diff --git a/github/repos.go b/github/repos.go index 98e4ac5..ffb8922 100644 --- a/github/repos.go +++ b/github/repos.go @@ -5,7 +5,10 @@ package github -import "fmt" +import ( + "fmt" + "strings" +) // RepositoriesService handles communication with the repository related // methods of the GitHub API. @@ -46,6 +49,9 @@ type Repository struct { Source *Repository `json:"source,omitempty"` Organization *Organization `json:"organization,omitempty"` Permissions *map[string]bool `json:"permissions,omitempty"` + AllowRebaseMerge *bool `json:"allow_rebase_merge,omitempty"` + AllowSquashMerge *bool `json:"allow_squash_merge,omitempty"` + AllowMergeCommit *bool `json:"allow_merge_commit,omitempty"` // Only provided when using RepositoriesService.Get while in preview License *License `json:"license,omitempty"` @@ -286,7 +292,8 @@ func (s *RepositoriesService) Get(owner, repo string) (*Repository, *Response, e // TODO: remove custom Accept header when the license support fully launches // https://developer.github.com/v3/licenses/#get-a-repositorys-license - req.Header.Set("Accept", mediaTypeLicensesPreview) + acceptHeaders := []string{mediaTypeLicensesPreview, mediaTypeSquashPreview} + req.Header.Set("Accept", strings.Join(acceptHeaders, ", ")) repository := new(Repository) resp, err := s.client.Do(req, repository) @@ -330,6 +337,9 @@ func (s *RepositoriesService) Edit(owner, repo string, repository *Repository) ( return nil, nil, err } + // TODO: Remove this preview header after API is fully vetted. + req.Header.Add("Accept", mediaTypeSquashPreview) + r := new(Repository) resp, err := s.client.Do(req, r) if err != nil { diff --git a/github/repos_test.go b/github/repos_test.go index 9f738f9..a75ac6b 100644 --- a/github/repos_test.go +++ b/github/repos_test.go @@ -10,6 +10,7 @@ import ( "fmt" "net/http" "reflect" + "strings" "testing" ) @@ -225,9 +226,10 @@ func TestRepositoriesService_Get(t *testing.T) { setup() defer teardown() + acceptHeader := []string{mediaTypeLicensesPreview, mediaTypeSquashPreview} mux.HandleFunc("/repos/o/r", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "GET") - testHeader(t, r, "Accept", mediaTypeLicensesPreview) + testHeader(t, r, "Accept", strings.Join(acceptHeader, ", ")) fmt.Fprint(w, `{"id":1,"name":"n","description":"d","owner":{"login":"l"},"license":{"key":"mit"}}`) })