Skip to content

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

Merged
merged 16 commits into from
Mar 8, 2017
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions github/pulls_reviewers.go
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 creates a review request for the provided GitHub users for the specified pull request.
//
// 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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick, RemoveReviewers has a blank line between u and reviewers, but this doesn't. Please make it consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

...([]User, *Response, error) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return users, resp, nil

}

// 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)
}
119 changes: 119 additions & 0 deletions github/pulls_reviewers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// 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"
)

type reviewersRequest struct {
Reviewers []string `json:"reviewers,omitempty"`
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you apply my suggestions below, this won't be needed, so remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


func TestRequestReviewers(t *testing.T) {
setup()
defer teardown()

logins := []string{"octocat", "googlebot"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inline logins, it's used in one place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
}

reviewers := reviewersRequest{}
if err := json.Unmarshal(b, &reviewers); err != nil {
return
}
want := reviewersRequest{
Reviewers: logins,
}
if !reflect.DeepEqual(reviewers, want) {
t.Errorf("PullRequests.RequestReviewers returned %+v, want %+v", reviewers, want)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can replace all of the above, starting with b, err := ioutil.ReadAll(r.Body), with:

testBody(t, r, `{"reviewers":["octocat","googlebot"]}`)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

})

// This returns a PR, unmarshalling of which is tested elsewhere
Copy link
Member

Choose a reason for hiding this comment

The 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)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
}
}

func TestRemoveReviewers(t *testing.T) {
setup()
defer teardown()
logins := []string{"octocat", "googlebot"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inline logins, it's used in one place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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, "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)
}

reviewers := reviewersRequest{}
if err := json.Unmarshal(b, &reviewers); err != nil {
return
}

want := reviewersRequest{
Reviewers: logins,
}
if !reflect.DeepEqual(reviewers, want) {
t.Errorf("PullRequests.RemoveReviewers returned %+v, want %+v", reviewers, want)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can replace all of the above, starting with b, err := ioutil.ReadAll(r.Body), with:

testBody(t, r, `{"reviewers":["octocat","googlebot"]}`)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

})

_, err := client.PullRequests.RemoveReviewers(context.Background(), "o", "r", 1, logins)
if err != nil {
t.Errorf("PullRequests.RequestReviewers returned error: %v", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/PullRequests.RequestReviewers/PullRequests.RemoveReviewers

}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick, remove the unnecessary and inconsistent blank line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

func TestListReviewers(t *testing.T) {
setup()
defer teardown()

sampleResponse := `[
Copy link
Member

Choose a reason for hiding this comment

The 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}]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member

@dmitshur dmitshur Feb 28, 2017

Choose a reason for hiding this comment

The 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".

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
Copy link
Member

@dmitshur dmitshur Feb 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use fmt.Fprintf where fmt.Fprint will suffice. You don't want the extra formatting options if you're not using them, they can only cause unwanted issues.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

})

reviewers, _, err := client.PullRequests.ListReviewers(context.Background(), "o", "r", 1)
if err != nil {
t.Errorf("PullRequests.ListReviewers error: %v", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consitency with lines 34 and 50 (and elsewhere in the project), insert the word "returned":

t.Errorf("PullRequests.ListReviewers returned error: %v", err)

}

want := &[]User{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

want := []User{

Copy link
Member

@dmitshur dmitshur Mar 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on #557 (comment), this should become:

want := []*User{

{
Login: String("octocat"),
ID: Int(1),
},
}
if !reflect.DeepEqual(reviewers, want) {
t.Errorf("PullRequests.ListReviews returned %+v, want %+v", reviewers, want)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/PullRequests.ListReviews/PullRequests.ListReviewers

}
}