Skip to content

Commit 2674ce0

Browse files
authored
Remove existing reviewer if reassign is triggerred. (#13460)
1 parent b798171 commit 2674ce0

File tree

5 files changed

+131
-0
lines changed

5 files changed

+131
-0
lines changed

.ci/magician/cmd/interfaces.go

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type GithubClient interface {
3333
PostComment(prNumber, comment string) error
3434
UpdateComment(prNumber, comment string, id int) error
3535
RequestPullRequestReviewers(prNumber string, reviewers []string) error
36+
RemovePullRequestReviewers(prNumber string, reviewers []string) error
3637
AddLabels(prNumber string, labels []string) error
3738
RemoveLabel(prNumber, label string) error
3839
CreateWorkflowDispatchEvent(workflowFileName string, inputs map[string]any) error

.ci/magician/cmd/mock_github_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ func (m *mockGithub) RequestPullRequestReviewers(prNumber string, reviewers []st
7474
return nil
7575
}
7676

77+
func (m *mockGithub) RemovePullRequestReviewers(prNumber string, reviewers []string) error {
78+
m.calledMethods["RemovePullRequestReviewers"] = append(m.calledMethods["RemovePullRequestReviewers"], []any{prNumber, reviewers})
79+
return nil
80+
}
81+
7782
func (m *mockGithub) PostComment(prNumber string, comment string) error {
7883
m.calledMethods["PostComment"] = append(m.calledMethods["PostComment"], []any{prNumber, comment})
7984
return nil

.ci/magician/cmd/reassign_reviewer.go

+3
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ func execReassignReviewer(prNumber, newPrimaryReviewer string, gh GithubClient)
9696
return err
9797
}
9898
} else {
99+
if err := gh.RemovePullRequestReviewers(prNumber, []string{currentReviewer}); err != nil {
100+
fmt.Printf("Failed to remove reviewer %s from pull request: %s\n", currentReviewer, err)
101+
}
99102
fmt.Println("Updating reviewer comment")
100103
err := gh.UpdateComment(prNumber, comment, reviewerComment.ID)
101104
if err != nil {
+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright 2025 Google LLC. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package cmd
17+
18+
import (
19+
"magician/github"
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
)
24+
25+
func TestExecReassignReviewer(t *testing.T) {
26+
availableReviewers := github.AvailableReviewers(nil)
27+
if len(availableReviewers) < 3 {
28+
t.Fatalf("not enough available reviewers (%v) to run TestExecRequestReviewer (need at least 3)", availableReviewers)
29+
}
30+
cases := map[string]struct {
31+
newPrimaryReviewer string
32+
comments []github.PullRequestComment
33+
expectSpecificReviewers []string
34+
expectRemovedReviewers []string
35+
}{
36+
"reassign from no current reviewer to random reviewer": {
37+
comments: []github.PullRequestComment{},
38+
},
39+
"reassign from no current reviewer to alice": {
40+
newPrimaryReviewer: "alice",
41+
comments: []github.PullRequestComment{},
42+
expectSpecificReviewers: []string{"alice"},
43+
},
44+
"reassign from bob to random reviewer": {
45+
comments: []github.PullRequestComment{
46+
{
47+
Body: github.FormatReviewerComment("bob"),
48+
ID: 1234,
49+
},
50+
},
51+
expectRemovedReviewers: []string{"bob"},
52+
},
53+
"reassign from bob to alice": {
54+
newPrimaryReviewer: "alice",
55+
comments: []github.PullRequestComment{
56+
{
57+
Body: github.FormatReviewerComment("bob"),
58+
ID: 1234,
59+
},
60+
},
61+
expectSpecificReviewers: []string{"alice"},
62+
expectRemovedReviewers: []string{"bob"},
63+
},
64+
}
65+
for tn, tc := range cases {
66+
t.Run(tn, func(t *testing.T) {
67+
gh := &mockGithub{
68+
pullRequest: github.PullRequest{
69+
User: github.User{Login: "author"},
70+
},
71+
calledMethods: make(map[string][][]any),
72+
pullRequestComments: tc.comments,
73+
}
74+
75+
err := execReassignReviewer("1", tc.newPrimaryReviewer, gh)
76+
if err != nil {
77+
t.Fatalf("execReassignReviewer failed: %v", err)
78+
}
79+
if len(gh.calledMethods["RequestPullRequestReviewers"]) != 1 {
80+
t.Errorf("Expected RequestPullRequestReviewers called 1 time, got %v", len(gh.calledMethods["RequestPullRequestReviewers"]))
81+
}
82+
83+
var assignedReviewers []string
84+
for _, args := range gh.calledMethods["RequestPullRequestReviewers"] {
85+
assignedReviewers = append(assignedReviewers, args[1].([]string)...)
86+
}
87+
var removedReviewers []string
88+
for _, args := range gh.calledMethods["RemovePullRequestReviewers"] {
89+
removedReviewers = append(removedReviewers, args[1].([]string)...)
90+
}
91+
92+
if tc.expectSpecificReviewers != nil {
93+
for _, reviewer := range assignedReviewers {
94+
assert.Contains(t, tc.expectSpecificReviewers, reviewer)
95+
}
96+
}
97+
if tc.expectRemovedReviewers != nil {
98+
for _, reviewer := range removedReviewers {
99+
assert.Contains(t, tc.expectRemovedReviewers, reviewer)
100+
}
101+
}
102+
})
103+
}
104+
}

.ci/magician/github/set.go

+18
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,24 @@ func (gh *Client) RequestPullRequestReviewers(prNumber string, reviewers []strin
9292
return nil
9393
}
9494

95+
func (gh *Client) RemovePullRequestReviewers(prNumber string, reviewers []string) error {
96+
url := fmt.Sprintf("https://api.github.com/repos/GoogleCloudPlatform/magic-modules/pulls/%s/requested_reviewers", prNumber)
97+
98+
body := map[string][]string{
99+
"reviewers": reviewers,
100+
"team_reviewers": {},
101+
}
102+
103+
err := utils.RequestCall(url, "DELETE", gh.token, nil, body)
104+
if err != nil {
105+
return err
106+
}
107+
108+
fmt.Printf("Successfully removed reviewers %v to pull request %s\n", reviewers, prNumber)
109+
110+
return nil
111+
}
112+
95113
func (gh *Client) AddLabels(prNumber string, labels []string) error {
96114
url := fmt.Sprintf("https://api.github.com/repos/GoogleCloudPlatform/magic-modules/issues/%s/labels", prNumber)
97115

0 commit comments

Comments
 (0)