Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Allow RemoveReviewers to remove only teams #3337

Merged
merged 3 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading