Skip to content

Commit

Permalink
Added update branch API (#1187)
Browse files Browse the repository at this point in the history
Fixes #1183.
  • Loading branch information
waseem18 authored and gmlewis committed Jun 9, 2019
1 parent 8354c07 commit 6a35880
Show file tree
Hide file tree
Showing 4 changed files with 103 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 @@ -137,6 +137,9 @@ const (

// https://developer.github.com/changes/2019-04-24-vulnerability-alerts/
mediaTypeRequiredVulnerabilityAlertsPreview = "application/vnd.github.dorian-preview+json"

// https://developer.github.com/changes/2019-05-29-update-branch-api/
mediaTypeUpdatePullRequestBranchPreview = "application/vnd.github.lydian-preview+json"
)

// A Client manages communication with the GitHub API.
Expand Down
43 changes: 43 additions & 0 deletions github/pulls.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,49 @@ func (s *PullRequestsService) Create(ctx context.Context, owner string, repo str
return p, resp, nil
}

// PullReqestBranchUpdateOptions specifies the optional parameters to the
// PullRequestsService.UpdateBranch method.
type PullReqestBranchUpdateOptions struct {
// ExpectedHeadSHA specifies the most recent commit on the pull request's branch.
// Default value is the SHA of the pull request's current HEAD ref.
ExpectedHeadSHA *string `json:"expected_head_sha,omitempty"`
}

// PullRequestBranchUpdateResponse specifies the response of pull request branch update.
type PullRequestBranchUpdateResponse struct {
Message *string `json:"message,omitempty"`
URL *string `json:"url,omitempty"`
}

// UpdateBranch updates the pull request branch with latest upstream changes.
//
// This method might return an AcceptedError and a status code of
// 202. This is because this is the status that GitHub returns to signify that
// it has now scheduled the update of the pull request branch in a background task.
// A follow up request, after a delay of a second or so, should result
// in a successful request.
//
// GitHub API docs: https://developer.github.com/v3/pulls/#update-a-pull-request-branch
func (s *PullRequestsService) UpdateBranch(ctx context.Context, owner, repo string, number int, opts *PullReqestBranchUpdateOptions) (*PullRequestBranchUpdateResponse, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/pulls/%d/update-branch", owner, repo, number)

req, err := s.client.NewRequest("PUT", u, opts)
if err != nil {
return nil, nil, err
}

// TODO: remove custom Accept header when this API fully launches.
req.Header.Set("Accept", mediaTypeUpdatePullRequestBranchPreview)

p := new(PullRequestBranchUpdateResponse)
resp, err := s.client.Do(ctx, req, p)
if err != nil {
return nil, resp, err
}

return p, resp, nil
}

type pullRequestUpdate struct {
Title *string `json:"title,omitempty"`
Body *string `json:"body,omitempty"`
Expand Down
33 changes: 33 additions & 0 deletions github/pulls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,39 @@ func TestPullRequestsService_Create_invalidOwner(t *testing.T) {
testURLParseError(t, err)
}

func TestPullRequestsService_UpdateBranch(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/repos/o/r/pulls/1/update-branch", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
testHeader(t, r, "Accept", mediaTypeUpdatePullRequestBranchPreview)
fmt.Fprint(w, `
{
"message": "Updating pull request branch.",
"url": "https://github.com/repos/o/r/pulls/1"
}`)
})

opts := &PullReqestBranchUpdateOptions{
ExpectedHeadSHA: String("s"),
}

pull, _, err := client.PullRequests.UpdateBranch(context.Background(), "o", "r", 1, opts)
if err != nil {
t.Errorf("PullRequests.UpdateBranch returned error: %v", err)
}

want := &PullRequestBranchUpdateResponse{
Message: String("Updating pull request branch."),
URL: String("https://github.com/repos/o/r/pulls/1"),
}

if !reflect.DeepEqual(pull, want) {
t.Errorf("PullRequests.UpdateBranch returned %+v, want %+v", pull, want)
}
}

func TestPullRequestsService_Edit(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
Expand Down

0 comments on commit 6a35880

Please sign in to comment.