Skip to content

Commit

Permalink
Allow RemoveReviewers to remove only teams (google#3337)
Browse files Browse the repository at this point in the history
  • Loading branch information
nagl-resourcely authored Oct 27, 2024
1 parent 5439fdf commit 9e5757d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
22 changes: 21 additions & 1 deletion github/pulls_reviewers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ type Reviewers struct {
Teams []*Team `json:"teams,omitempty"`
}

type removeReviewersRequest struct {
NodeID *string `json:"node_id,omitempty"`
// Note the lack of omitempty! See comment in RemoveReviewers.
Reviewers []string `json:"reviewers"`
TeamReviewers []string `json:"team_reviewers,omitempty"`
}

// RequestReviewers creates a review request for the provided reviewers for the specified pull request.
//
// GitHub API docs: https://docs.github.com/rest/pulls/review-requests#request-reviewers-for-a-pull-request
Expand Down Expand Up @@ -76,8 +83,21 @@ func (s *PullRequestsService) ListReviewers(ctx context.Context, owner, repo str
//
//meta:operation DELETE /repos/{owner}/{repo}/pulls/{pull_number}/requested_reviewers
func (s *PullRequestsService) RemoveReviewers(ctx context.Context, owner, repo string, number int, reviewers ReviewersRequest) (*Response, error) {
// reviewers.Reviewers may be empty if the caller wants to remove teams, but not users. Unlike AddReviewers,
// "reviewers" is a required param here. Reference: https://github.com/google/go-github/issues/3336
removeRequest := removeReviewersRequest{
NodeID: reviewers.NodeID,
Reviewers: reviewers.Reviewers,
TeamReviewers: reviewers.TeamReviewers,
}

if removeRequest.Reviewers == nil {
// GitHub accepts the empty list, but rejects null. Removing `omitempty` is not enough - we also have to promote nil to [].
removeRequest.Reviewers = []string{}
}

u := fmt.Sprintf("repos/%s/%s/pulls/%d/requested_reviewers", owner, repo, number)
req, err := s.client.NewRequest("DELETE", u, &reviewers)
req, err := s.client.NewRequest("DELETE", u, &removeRequest)
if err != nil {
return nil, err
}
Expand Down
21 changes: 21 additions & 0 deletions github/pulls_reviewers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,27 @@ func TestRemoveReviewers(t *testing.T) {
})
}

func TestRemoveReviewers_teamsOnly(t *testing.T) {
t.Parallel()
client, mux, _ := setup(t)

mux.HandleFunc("/repos/o/r/pulls/1/requested_reviewers", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
testBody(t, r, `{"reviewers":[],"team_reviewers":["justice-league"]}`+"\n")
})

ctx := context.Background()
_, err := client.PullRequests.RemoveReviewers(ctx, "o", "r", 1, ReviewersRequest{TeamReviewers: []string{"justice-league"}})
if err != nil {
t.Errorf("PullRequests.RemoveReviewers returned error: %v", err)
}

const methodName = "RemoveReviewers"
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
return client.PullRequests.RemoveReviewers(ctx, "o", "r", 1, ReviewersRequest{TeamReviewers: []string{"justice-league"}})
})
}

func TestListReviewers(t *testing.T) {
t.Parallel()
client, mux, _ := setup(t)
Expand Down

0 comments on commit 9e5757d

Please sign in to comment.