diff --git a/github/pulls.go b/github/pulls.go index a927bd9..017da75 100644 --- a/github/pulls.go +++ b/github/pulls.go @@ -42,13 +42,23 @@ type PullRequest struct { IssueURL *string `json:"issue_url,omitempty"` StatusesURL *string `json:"statuses_url,omitempty"` - // TODO(willnorris): add head and base once we have a Commit struct defined somewhere + Head *PullRequestBranch `json:"head,omitempty"` + Base *PullRequestBranch `json:"base,omitempty"` } func (p PullRequest) String() string { return Stringify(p) } +// PullRequestBranch represents a base or head branch in a GitHub pull request. +type PullRequestBranch struct { + Label *string `json:"label,omitempty"` + Ref *string `json:"ref,omitempty"` + SHA *string `json:"sha,omitempty"` + Repo *Repository `json:"repo,omitempty"` + User *User `json:"user,omitempty"` +} + // PullRequestListOptions specifies the optional parameters to the // PullRequestsService.List method. type PullRequestListOptions struct { @@ -109,10 +119,19 @@ func (s *PullRequestsService) Get(owner string, repo string, number int) (*PullR return pull, resp, err } +// NewPullRequest represents a new pull request to be created. +type NewPullRequest struct { + Title *string `json:"title,omitempty"` + Head *string `json:"head,omitempty"` + Base *string `json:"base,omitempty"` + Body *string `json:"body,omitempty"` + Issue *int `json:"issue,omitempty"` +} + // Create a new pull request on the specified repository. // // GitHub API docs: https://developer.github.com/v3/pulls/#create-a-pull-request -func (s *PullRequestsService) Create(owner string, repo string, pull *PullRequest) (*PullRequest, *Response, error) { +func (s *PullRequestsService) Create(owner string, repo string, pull *NewPullRequest) (*PullRequest, *Response, error) { u := fmt.Sprintf("repos/%v/%v/pulls", owner, repo) req, err := s.client.NewRequest("POST", u, pull) if err != nil { diff --git a/github/pulls_test.go b/github/pulls_test.go index 04638f7..9307c4e 100644 --- a/github/pulls_test.go +++ b/github/pulls_test.go @@ -67,6 +67,37 @@ func TestPullRequestsService_Get(t *testing.T) { } } +func TestPullRequestsService_Get_headAndBase(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/pulls/1", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{"number":1,"head":{"ref":"r2","repo":{"id":2}},"base":{"ref":"r1","repo":{"id":1}}}`) + }) + + pull, _, err := client.PullRequests.Get("o", "r", 1) + + if err != nil { + t.Errorf("PullRequests.Get returned error: %v", err) + } + + want := &PullRequest{ + Number: Int(1), + Head: &PullRequestBranch{ + Ref: String("r2"), + Repo: &Repository{ID: Int(2)}, + }, + Base: &PullRequestBranch{ + Ref: String("r1"), + Repo: &Repository{ID: Int(1)}, + }, + } + if !reflect.DeepEqual(pull, want) { + t.Errorf("PullRequests.Get returned %+v, want %+v", pull, want) + } +} + func TestPullRequestsService_Get_invalidOwner(t *testing.T) { _, _, err := client.PullRequests.Get("%", "r", 1) testURLParseError(t, err) @@ -76,10 +107,10 @@ func TestPullRequestsService_Create(t *testing.T) { setup() defer teardown() - input := &PullRequest{Title: String("t")} + input := &NewPullRequest{Title: String("t")} mux.HandleFunc("/repos/o/r/pulls", func(w http.ResponseWriter, r *http.Request) { - v := new(PullRequest) + v := new(NewPullRequest) json.NewDecoder(r.Body).Decode(v) testMethod(t, r, "POST")