Skip to content

Commit 6a35880

Browse files
waseem18gmlewis
authored andcommitted
Added update branch API (#1187)
Fixes #1183.
1 parent 8354c07 commit 6a35880

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-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
@@ -137,6 +137,9 @@ const (
137137

138138
// https://developer.github.com/changes/2019-04-24-vulnerability-alerts/
139139
mediaTypeRequiredVulnerabilityAlertsPreview = "application/vnd.github.dorian-preview+json"
140+
141+
// https://developer.github.com/changes/2019-05-29-update-branch-api/
142+
mediaTypeUpdatePullRequestBranchPreview = "application/vnd.github.lydian-preview+json"
140143
)
141144

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

github/pulls.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,49 @@ func (s *PullRequestsService) Create(ctx context.Context, owner string, repo str
242242
return p, resp, nil
243243
}
244244

245+
// PullReqestBranchUpdateOptions specifies the optional parameters to the
246+
// PullRequestsService.UpdateBranch method.
247+
type PullReqestBranchUpdateOptions struct {
248+
// ExpectedHeadSHA specifies the most recent commit on the pull request's branch.
249+
// Default value is the SHA of the pull request's current HEAD ref.
250+
ExpectedHeadSHA *string `json:"expected_head_sha,omitempty"`
251+
}
252+
253+
// PullRequestBranchUpdateResponse specifies the response of pull request branch update.
254+
type PullRequestBranchUpdateResponse struct {
255+
Message *string `json:"message,omitempty"`
256+
URL *string `json:"url,omitempty"`
257+
}
258+
259+
// UpdateBranch updates the pull request branch with latest upstream changes.
260+
//
261+
// This method might return an AcceptedError and a status code of
262+
// 202. This is because this is the status that GitHub returns to signify that
263+
// it has now scheduled the update of the pull request branch in a background task.
264+
// A follow up request, after a delay of a second or so, should result
265+
// in a successful request.
266+
//
267+
// GitHub API docs: https://developer.github.com/v3/pulls/#update-a-pull-request-branch
268+
func (s *PullRequestsService) UpdateBranch(ctx context.Context, owner, repo string, number int, opts *PullReqestBranchUpdateOptions) (*PullRequestBranchUpdateResponse, *Response, error) {
269+
u := fmt.Sprintf("repos/%v/%v/pulls/%d/update-branch", owner, repo, number)
270+
271+
req, err := s.client.NewRequest("PUT", u, opts)
272+
if err != nil {
273+
return nil, nil, err
274+
}
275+
276+
// TODO: remove custom Accept header when this API fully launches.
277+
req.Header.Set("Accept", mediaTypeUpdatePullRequestBranchPreview)
278+
279+
p := new(PullRequestBranchUpdateResponse)
280+
resp, err := s.client.Do(ctx, req, p)
281+
if err != nil {
282+
return nil, resp, err
283+
}
284+
285+
return p, resp, nil
286+
}
287+
245288
type pullRequestUpdate struct {
246289
Title *string `json:"title,omitempty"`
247290
Body *string `json:"body,omitempty"`

github/pulls_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,39 @@ func TestPullRequestsService_Create_invalidOwner(t *testing.T) {
303303
testURLParseError(t, err)
304304
}
305305

306+
func TestPullRequestsService_UpdateBranch(t *testing.T) {
307+
client, mux, _, teardown := setup()
308+
defer teardown()
309+
310+
mux.HandleFunc("/repos/o/r/pulls/1/update-branch", func(w http.ResponseWriter, r *http.Request) {
311+
testMethod(t, r, "PUT")
312+
testHeader(t, r, "Accept", mediaTypeUpdatePullRequestBranchPreview)
313+
fmt.Fprint(w, `
314+
{
315+
"message": "Updating pull request branch.",
316+
"url": "https://github.com/repos/o/r/pulls/1"
317+
}`)
318+
})
319+
320+
opts := &PullReqestBranchUpdateOptions{
321+
ExpectedHeadSHA: String("s"),
322+
}
323+
324+
pull, _, err := client.PullRequests.UpdateBranch(context.Background(), "o", "r", 1, opts)
325+
if err != nil {
326+
t.Errorf("PullRequests.UpdateBranch returned error: %v", err)
327+
}
328+
329+
want := &PullRequestBranchUpdateResponse{
330+
Message: String("Updating pull request branch."),
331+
URL: String("https://github.com/repos/o/r/pulls/1"),
332+
}
333+
334+
if !reflect.DeepEqual(pull, want) {
335+
t.Errorf("PullRequests.UpdateBranch returned %+v, want %+v", pull, want)
336+
}
337+
}
338+
306339
func TestPullRequestsService_Edit(t *testing.T) {
307340
client, mux, _, teardown := setup()
308341
defer teardown()

0 commit comments

Comments
 (0)