Skip to content

Commit 1d8328d

Browse files
abhinavdmitshur
authored andcommitted
Add support for editing base branch of a PR. (#528)
As per the discussion in #421, the PullRequest parameter is converted into an unexported type pullRequestUpdate which matches the shape expected by the pulls PATCH endpoint. Resolves #421.
1 parent b61d23c commit 1d8328d

File tree

2 files changed

+70
-18
lines changed

2 files changed

+70
-18
lines changed

github/pulls.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,33 @@ func (s *PullRequestsService) Create(owner string, repo string, pull *NewPullReq
189189
return p, resp, err
190190
}
191191

192+
type pullRequestUpdate struct {
193+
Title *string `json:"title,omitempty"`
194+
Body *string `json:"body,omitempty"`
195+
State *string `json:"state,omitempty"`
196+
Base *string `json:"base,omitempty"`
197+
}
198+
192199
// Edit a pull request.
193200
//
201+
// The following fields are editable: Title, Body, State, and Base.Ref.
202+
// Base.Ref updates the base branch of the pull request.
203+
//
194204
// GitHub API docs: https://developer.github.com/v3/pulls/#update-a-pull-request
195205
func (s *PullRequestsService) Edit(owner string, repo string, number int, pull *PullRequest) (*PullRequest, *Response, error) {
196206
u := fmt.Sprintf("repos/%v/%v/pulls/%d", owner, repo, number)
197-
req, err := s.client.NewRequest("PATCH", u, pull)
207+
208+
update := new(pullRequestUpdate)
209+
if pull != nil {
210+
update.Title = pull.Title
211+
update.Body = pull.Body
212+
update.State = pull.State
213+
if pull.Base != nil {
214+
update.Base = pull.Base.Ref
215+
}
216+
}
217+
218+
req, err := s.client.NewRequest("PATCH", u, update)
198219
if err != nil {
199220
return nil, nil, err
200221
}

github/pulls_test.go

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package github
88
import (
99
"encoding/json"
1010
"fmt"
11+
"io"
1112
"net/http"
1213
"reflect"
1314
"strings"
@@ -235,28 +236,58 @@ func TestPullRequestsService_Edit(t *testing.T) {
235236
setup()
236237
defer teardown()
237238

238-
input := &PullRequest{Title: String("t")}
239+
tests := []struct {
240+
input *PullRequest
241+
sendResponse string
239242

240-
mux.HandleFunc("/repos/o/r/pulls/1", func(w http.ResponseWriter, r *http.Request) {
241-
v := new(PullRequest)
242-
json.NewDecoder(r.Body).Decode(v)
243+
wantUpdate string
244+
want *PullRequest
245+
}{
246+
{
247+
input: &PullRequest{Title: String("t")},
248+
sendResponse: `{"number":1}`,
249+
wantUpdate: `{"title":"t"}`,
250+
want: &PullRequest{Number: Int(1)},
251+
},
252+
{
253+
// nil request
254+
sendResponse: `{}`,
255+
wantUpdate: `{}`,
256+
want: &PullRequest{},
257+
},
258+
{
259+
// base update
260+
input: &PullRequest{Base: &PullRequestBranch{Ref: String("master")}},
261+
sendResponse: `{"number":1,"base":{"ref":"master"}}`,
262+
wantUpdate: `{"base":"master"}`,
263+
want: &PullRequest{
264+
Number: Int(1),
265+
Base: &PullRequestBranch{Ref: String("master")},
266+
},
267+
},
268+
}
243269

244-
testMethod(t, r, "PATCH")
245-
if !reflect.DeepEqual(v, input) {
246-
t.Errorf("Request body = %+v, want %+v", v, input)
247-
}
270+
for i, tt := range tests {
271+
madeRequest := false
272+
mux.HandleFunc(fmt.Sprintf("/repos/o/r/pulls/%v", i), func(w http.ResponseWriter, r *http.Request) {
273+
testMethod(t, r, "PATCH")
274+
testBody(t, r, tt.wantUpdate+"\n")
275+
io.WriteString(w, tt.sendResponse)
276+
madeRequest = true
277+
})
248278

249-
fmt.Fprint(w, `{"number":1}`)
250-
})
279+
pull, _, err := client.PullRequests.Edit("o", "r", i, tt.input)
280+
if err != nil {
281+
t.Errorf("%d: PullRequests.Edit returned error: %v", i, err)
282+
}
251283

252-
pull, _, err := client.PullRequests.Edit("o", "r", 1, input)
253-
if err != nil {
254-
t.Errorf("PullRequests.Edit returned error: %v", err)
255-
}
284+
if !reflect.DeepEqual(pull, tt.want) {
285+
t.Errorf("%d: PullRequests.Edit returned %+v, want %+v", i, pull, tt.want)
286+
}
256287

257-
want := &PullRequest{Number: Int(1)}
258-
if !reflect.DeepEqual(pull, want) {
259-
t.Errorf("PullRequests.Edit returned %+v, want %+v", pull, want)
288+
if !madeRequest {
289+
t.Errorf("%d: PullRequest.Edit did not make the expected request", i)
290+
}
260291
}
261292
}
262293

0 commit comments

Comments
 (0)