Skip to content

Commit 38a474f

Browse files
committed
Add 'list all reviews' endpoint from new pull request review API
This commit adds only one of the total 6 endpoints made available for developers preview. Partly fixes #495 EDIT: Fix minor things based on initial review.
1 parent 466070b commit 38a474f

File tree

3 files changed

+79
-6
lines changed

3 files changed

+79
-6
lines changed

github/github.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ const (
9191

9292
// https://developer.github.com/changes/2016-09-14-Integrations-Early-Access/
9393
mediaTypeIntegrationPreview = "application/vnd.github.machine-man-preview+json"
94+
95+
// https://developer.github.com/changes/2016-12-14-reviews-api/
96+
mediaTypePullRequestReviewsPreview = "application/vnd.github.black-cat-preview+json"
9497
)
9598

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

github/pulls_reviews.go

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,48 @@
55

66
package github
77

8-
import "time"
8+
import (
9+
"fmt"
10+
"time"
11+
)
912

1013
// PullRequestReview represents a review of a pull request.
1114
type PullRequestReview struct {
12-
ID *int `json:"id,omitempty"`
13-
User *User `json:"user,omitempty"`
14-
Body *string `json:"body,omitempty"`
15-
SubmittedAt *time.Time `json:"submitted_at,omitempty"`
15+
ID *int `json:"id,omitempty"`
16+
User *User `json:"user,omitempty"`
17+
Body *string `json:"body,omitempty"`
18+
SubmittedAt *time.Time `json:"submitted_at,omitempty"`
19+
CommitID *string `json:"commit_id,omitempty"`
20+
HTMLURL *string `json:"html_url,omitempty"`
21+
PullRequestURL *string `json:"pull_request_url,omitempty"`
1622

17-
// State can be "approved", "rejected", or "commented".
23+
// State can be "ACCEPTED", "DISMISSED", "CHANGES_REQUESTED" or "COMMENTED".
1824
State *string `json:"state,omitempty"`
1925
}
26+
27+
func (p PullRequestReview) String() string {
28+
return Stringify(p)
29+
}
30+
31+
// ListReviews lists all reviews on the specified pull request.
32+
//
33+
// GitHub API docs: https://developer.github.com/v3/pulls/reviews/#list-reviews-on-a-pull-request
34+
func (s *PullRequestsService) ListReviews(owner string, repo string, number int) ([]*PullRequestReview, *Response, error) {
35+
u := fmt.Sprintf("repos/%v/%v/pulls/%d/reviews", owner, repo, number)
36+
37+
req, err := s.client.NewRequest("GET", u, nil)
38+
if err != nil {
39+
return nil, nil, err
40+
}
41+
42+
// TODO: remove custom Accept header when this API fully launches
43+
req.Header.Set("Accept", mediaTypePullRequestReviewsPreview)
44+
45+
reviews := new([]*PullRequestReview)
46+
resp, err := s.client.Do(req, reviews)
47+
if err != nil {
48+
return nil, resp, err
49+
}
50+
51+
return *reviews, resp, err
52+
}

github/pulls_reviews_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2016 The go-github AUTHORS. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
package github
7+
8+
import (
9+
"fmt"
10+
"net/http"
11+
"reflect"
12+
"testing"
13+
)
14+
15+
func TestPullRequestsService_ListReviews(t *testing.T) {
16+
setup()
17+
defer teardown()
18+
19+
mux.HandleFunc("/repos/o/r/pulls/1/reviews", func(w http.ResponseWriter, r *http.Request) {
20+
testMethod(t, r, "GET")
21+
testHeader(t, r, "Accept", mediaTypePullRequestReviewsPreview)
22+
fmt.Fprint(w, `[{"id":1},{"id":2}]`)
23+
})
24+
25+
reviews, _, err := client.PullRequests.ListReviews("o", "r", 1)
26+
if err != nil {
27+
t.Errorf("PullRequests.ListReviews returned error: %v", err)
28+
}
29+
30+
want := []*PullRequestReview{
31+
{ID: Int(1)},
32+
{ID: Int(2)},
33+
}
34+
if !reflect.DeepEqual(reviews, want) {
35+
t.Errorf("PullRequests.ListReviews returned %+v, want %+v", reviews, want)
36+
}
37+
}

0 commit comments

Comments
 (0)