-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add requestreviewers #557
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add requestreviewers #557
Changes from 10 commits
c940114
eead948
3d66e9c
817dd53
d122ebf
3977482
df46fa8
e9ab56b
4a5d897
e1d9534
7540355
56eee95
4f9f8e3
b74bcf8
08a3813
9ec6c3d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright 2017 The go-github AUTHORS. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package github | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
) | ||
|
||
// RequestReviewers submits a set of logins to be potential reviewers on a PR. | ||
// | ||
// GitHub API docs: https://developer.github.com/v3/pulls/review_requests/#create-a-review-request | ||
func (s *PullRequestsService) RequestReviewers(ctx context.Context, owner, repo string, number int, logins []string) (*PullRequest, *Response, error) { | ||
u := fmt.Sprintf("repos/%s/%s/pulls/%d/requested_reviewers", owner, repo, number) | ||
reviewers := struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
Reviewers []string `json:"reviewers,omitempty"` | ||
}{ | ||
Reviewers: logins, | ||
} | ||
req, err := s.client.NewRequest("POST", u, &reviewers) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
// TODO: remove custom Accept header when this API fully launches | ||
req.Header.Set("Accept", mediaTypePullRequestReviewsPreview) | ||
|
||
r := new(PullRequest) | ||
resp, err := s.client.Do(ctx, req, r) | ||
if err != nil { | ||
return nil, resp, err | ||
} | ||
|
||
return r, resp, nil | ||
} | ||
|
||
// ListReviewers lists users whose reviews have been requested on the specified pull request. | ||
// | ||
// GitHub API docs: https://developer.github.com/v3/pulls/review_requests/#list-review-requests | ||
func (s *PullRequestsService) ListReviewers(ctx context.Context, owner, repo string, number int) (*[]User, *Response, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need to return a pointer to a slice. Just return the slice which is already a reference type.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gmlewis, per https://godoc.org/github.com/google/go-github/github#UsersService.ListAll and #180 (comment), shouldn't we go with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow! Yes! Where was my brain? |
||
u := fmt.Sprintf("repos/%v/%v/pulls/%d/requested_reviewers", owner, repo, number) | ||
|
||
req, err := s.client.NewRequest("GET", u, nil) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
// TODO: remove custom Accept header when this API fully launches | ||
req.Header.Set("Accept", mediaTypePullRequestReviewsPreview) | ||
|
||
var users []User | ||
resp, err := s.client.Do(ctx, req, &users) | ||
if err != nil { | ||
return nil, resp, err | ||
} | ||
|
||
return &users, resp, nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
// RemoveReviewers removes the review request for the provided GitHub users for the specified pull request. | ||
// | ||
// GitHub API docs: https://developer.github.com/v3/pulls/review_requests/#delete-a-review-request | ||
func (s *PullRequestsService) RemoveReviewers(ctx context.Context, owner, repo string, number int, logins []string) (*Response, error) { | ||
u := fmt.Sprintf("repos/%s/%s/pulls/%d/requested_reviewers", owner, repo, number) | ||
|
||
reviewers := struct { | ||
Reviewers []string `json:"reviewers,omitempty"` | ||
}{ | ||
Reviewers: logins, | ||
} | ||
req, err := s.client.NewRequest("DELETE", u, &reviewers) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// TODO: remove custom Accept header when this API fully launches | ||
req.Header.Set("Accept", mediaTypePullRequestReviewsPreview) | ||
|
||
return s.client.Do(ctx, req, reviewers) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// Copyright 2017 The go-github AUTHORS. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package github | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestRequestReviewers(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
type reviewers struct { | ||
Reviewers []string `json:"reviewers,omitempty"` | ||
} | ||
have := reviewers{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
mux.HandleFunc("/repos/o/r/pulls/1/requested_reviewers", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "POST") | ||
testHeader(t, r, "Accept", mediaTypePullRequestReviewsPreview) | ||
b, err := ioutil.ReadAll(r.Body) | ||
if err != nil { | ||
t.Errorf("TestReviewerRequest couldn't read request body: %v", err) | ||
} | ||
if err := json.Unmarshal(b, &have); err != nil { | ||
return | ||
} | ||
}) | ||
|
||
logins := []string{"octocat", "googlebot"} | ||
|
||
// This returns a PR, unmarshalling of which is tested elsewhere | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not hard to test it. You don't need to test all fields. Just include the PR number. In the response handler above, add this at the end: fmt.Fprint(w, `{"number":1}`) And then do: pull, _, err := client.PullRequests.RequestReviewers(...)
...
want := &PullRequest{Number: Int(1)}
if !reflect.DeepEqual(pull, want) {
t.Errorf("PullRequests.RequestReviewers returned %+v, want %+v", pull, want)
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
_, _, err := client.PullRequests.RequestReviewers(context.Background(), "o", "r", 1, logins) | ||
if err != nil { | ||
t.Errorf("PullRequests.RequestReviewers returned error: %v", err) | ||
} | ||
|
||
want := reviewers{ | ||
Reviewers: logins, | ||
} | ||
if !reflect.DeepEqual(have, want) { | ||
t.Errorf("PullRequests.ListReviews returned %+v, want %+v", have, want) | ||
} | ||
} | ||
|
||
func TestRemoveReviewers(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
type reviewers struct { | ||
Reviewers []string `json:"reviewers,omitempty"` | ||
} | ||
have := reviewers{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't use |
||
mux.HandleFunc("/repos/o/r/pulls/1/requested_reviewers", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "DELETE") | ||
testHeader(t, r, "Accept", mediaTypePullRequestReviewsPreview) | ||
b, err := ioutil.ReadAll(r.Body) | ||
if err != nil { | ||
t.Errorf("TestReviewerRequest couldn't read request body: %v", err) | ||
} | ||
if err := json.Unmarshal(b, &have); err != nil { | ||
return | ||
} | ||
}) | ||
|
||
logins := []string{"octocat", "googlebot"} | ||
|
||
_, err := client.PullRequests.RemoveReviewers(context.Background(), "o", "r", 1, logins) | ||
if err != nil { | ||
t.Errorf("PullRequests.RequestReviewers returned error: %v", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/PullRequests.RequestReviewers/PullRequests.RemoveReviewers |
||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick, remove the unnecessary and inconsistent blank line. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
want := reviewers{ | ||
Reviewers: logins, | ||
} | ||
if !reflect.DeepEqual(have, want) { | ||
t.Errorf("PullRequests.ListReviews returned %+v, want %+v", have, want) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is misleading an not accurate. It says "PullRequests.ListReviews", but the method that was called was PullRequests.RemoveReviewers". I think you should move this check directly inside There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Looks like I mixed Remove and Request 😃 |
||
} | ||
} | ||
|
||
func TestListReviewers(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
sampleResponse := `[ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can inline this, it's used in one place. Also, make it a single line, it's short enough to be readable: [{"login": "octocat", "id": 1}] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You said 👍 here, but you didn't apply this change. Did you miss it? I mean the part about "make it a single line, it's short enough to be readable". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ooops. |
||
{ | ||
"login": "octocat", | ||
"id": 1 | ||
} | ||
]` | ||
|
||
mux.HandleFunc("/repos/o/r/pulls/1/requested_reviewers", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "GET") | ||
testHeader(t, r, "Accept", mediaTypePullRequestReviewsPreview) | ||
fmt.Fprintf(w, sampleResponse) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
}) | ||
|
||
// This returns a PR, unmarshalling of which is tested elsewhere | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand this comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
have, _, err := client.PullRequests.ListReviewers(context.Background(), "o", "r", 1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename Alternatively, use the word There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
if err != nil { | ||
t.Errorf("PullRequests.RequestReviewers returned error: %v", err) | ||
} | ||
_login := "octocat" | ||
_id := 1 | ||
|
||
want := []User{ | ||
{ | ||
Login: &_login, | ||
ID: &_id, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Go style doesn't use underscores in identifier names, see https://github.com/golang/go/wiki/CodeReviewComments#mixed-caps. Also, these variables are not needed. This can be replaced with: {
Login: String("octocat"),
ID: Int(1),
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
}, | ||
} | ||
if !reflect.DeepEqual(have, &want) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it work without taking address of if !reflect.DeepEqual(have, want) { Or would that fail? Other tests don't seem to take address of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does the error message say? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Never mind, I see that it's because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @shurcooL I looked at a couple of things and saw they return pointers to arrays, is that not the consistent thing to do? if not I can change it |
||
t.Errorf("PullRequests.ListReviews returned %+v, want %+v", have, want) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it'd be nicer to stay closer to the language the GitHub API docs use and say that this method creates a "review request". What do you think of what I wrote earlier?
That seems more consistent with the other endpoints (like the remove one).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍