forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added service team review request support (GoogleCloudPlatform#9681)
* Initial work to support requesting service team reviews * Initial implementation of PR requests for service teams * Added test team * Added issue labeler dependency * Fixed build errors * Cleanup * Added request-service-reviewers to generate-diffs * Added unit tests * Removed test team * Tweaked variable name to be more explicit * Fixed ordering expectations in tests * Clarified which variables are sets
- Loading branch information
Showing
9 changed files
with
347 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
* Copyright 2023 Google LLC. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"magician/github" | ||
"math/rand" | ||
"os" | ||
"strings" | ||
|
||
"github.com/GoogleCloudPlatform/magic-modules/tools/issue-labeler/labeler" | ||
"github.com/spf13/cobra" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
// requestServiceReviewersCmd represents the requestServiceReviewers command | ||
var requestServiceReviewersCmd = &cobra.Command{ | ||
Use: "request-service-reviewers PR_NUMBER", | ||
Short: "Assigns reviewers based on the PR's service labels.", | ||
Long: `This command requests (or re-requests) review based on the PR's service labels. | ||
If a PR has more than 3 service labels, the command will not do anything. | ||
`, | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
prNumber := args[0] | ||
fmt.Println("PR Number: ", prNumber) | ||
|
||
gh := github.NewClient() | ||
execRequestServiceReviewers(prNumber, gh, labeler.EnrolledTeamsYaml) | ||
}, | ||
} | ||
|
||
func execRequestServiceReviewers(prNumber string, gh GithubClient, enrolledTeamsYaml []byte) { | ||
pullRequest, err := gh.GetPullRequest(prNumber) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
enrolledTeams := make(map[string]labeler.LabelData) | ||
if err := yaml.Unmarshal(enrolledTeamsYaml, &enrolledTeams); err != nil { | ||
fmt.Printf("Error unmarshalling enrolled teams yaml: %s", err) | ||
os.Exit(1) | ||
} | ||
|
||
requestedReviewers, err := gh.GetPullRequestRequestedReviewers(prNumber) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
previousReviewers, err := gh.GetPullRequestPreviousReviewers(prNumber) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
// If more than three service labels are impacted, don't request reviews. | ||
// Only request reviews from unique service teams. | ||
githubTeamsSet := make(map[string]struct{}) | ||
teamCount := 0 | ||
for _, label := range pullRequest.Labels { | ||
if !strings.HasPrefix(label.Name, "service/") || label.Name == "service/terraform" { | ||
continue | ||
} | ||
teamCount += 1 | ||
if labelData, ok := enrolledTeams[label.Name]; ok { | ||
githubTeamsSet[labelData.Team] = struct{}{} | ||
} | ||
} | ||
|
||
if teamCount > 3 { | ||
fmt.Println("Provider-wide change (>3 services impacted); not requesting service team reviews") | ||
return | ||
} | ||
|
||
// For each service team, check if one of the team members is already a reviewer. Rerequest | ||
// review if there is and choose a random reviewer from the list if there isn't. | ||
reviewersToRequest := []string{} | ||
requestedReviewersSet := make(map[string]struct{}) | ||
for _, reviewer := range requestedReviewers { | ||
requestedReviewersSet[reviewer.Login] = struct{}{} | ||
} | ||
|
||
previousReviewersSet := make(map[string]struct{}) | ||
for _, reviewer := range previousReviewers { | ||
previousReviewersSet[reviewer.Login] = struct{}{} | ||
} | ||
|
||
exitCode := 0 | ||
for githubTeam, _ := range githubTeamsSet { | ||
members, err := gh.GetTeamMembers("GoogleCloudPlatform", githubTeam) | ||
if err != nil { | ||
fmt.Printf("Error fetching members for GoogleCloudPlatform/%s: %s", githubTeam, err) | ||
exitCode = 1 | ||
continue | ||
} | ||
hasReviewer := false | ||
reviewerPool := []string{} | ||
for _, member := range members { | ||
// Exclude PR author | ||
if member.Login != pullRequest.User.Login { | ||
reviewerPool = append(reviewerPool, member.Login) | ||
} | ||
// Don't re-request review if there's an active review request | ||
if _, ok := requestedReviewersSet[member.Login]; ok { | ||
hasReviewer = true | ||
} | ||
if _, ok := previousReviewersSet[member.Login]; ok { | ||
hasReviewer = true | ||
reviewersToRequest = append(reviewersToRequest, member.Login) | ||
} | ||
} | ||
|
||
if !hasReviewer && len(reviewerPool) > 0 { | ||
reviewersToRequest = append(reviewersToRequest, reviewerPool[rand.Intn(len(reviewerPool))]) | ||
} | ||
} | ||
|
||
for _, reviewer := range reviewersToRequest { | ||
err = gh.RequestPullRequestReviewer(prNumber, reviewer) | ||
if err != nil { | ||
fmt.Println(err) | ||
exitCode = 1 | ||
} | ||
} | ||
if exitCode != 0 { | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(requestServiceReviewersCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"magician/github" | ||
"testing" | ||
) | ||
|
||
var enrolledTeamsYaml = []byte(` | ||
service/google-x: | ||
team: google-x | ||
resources: | ||
- google_x_resource | ||
service/google-y: | ||
team: google-y | ||
resources: | ||
- google_y_resource | ||
service/google-z: | ||
resources: | ||
- google_z_resource | ||
`) | ||
|
||
func TestExecRequestServiceReviewersMembershipChecker(t *testing.T) { | ||
cases := map[string]struct { | ||
pullRequest github.PullRequest | ||
requestedReviewers []string | ||
previousReviewers []string | ||
teamMembers map[string][]string | ||
expectSpecificReviewers []string | ||
}{ | ||
"no service labels means no service team reviewers": { | ||
pullRequest: github.PullRequest{ | ||
User: github.User{Login: "googler_author"}, | ||
}, | ||
expectSpecificReviewers: []string{}, | ||
}, | ||
"unregistered service labels will not trigger review": { | ||
pullRequest: github.PullRequest{ | ||
User: github.User{Login: "googler_author"}, | ||
Labels: []github.Label{{Name: "service/google-a"}}, | ||
}, | ||
expectSpecificReviewers: []string{}, | ||
}, | ||
"no configured team means no reviewers": { | ||
pullRequest: github.PullRequest{ | ||
User: github.User{Login: "googler_author"}, | ||
Labels: []github.Label{{Name: "service/google-z"}}, | ||
}, | ||
expectSpecificReviewers: []string{}, | ||
}, | ||
"no previous reviewers means all reviews will be requested": { | ||
pullRequest: github.PullRequest{ | ||
User: github.User{Login: "googler_author"}, | ||
Labels: []github.Label{{Name: "service/google-x"}}, | ||
}, | ||
teamMembers: map[string][]string{"google-x": []string{"googler_team_member"}}, | ||
expectSpecificReviewers: []string{"googler_team_member"}, | ||
}, | ||
"previous reviewers will be re-requested": { | ||
pullRequest: github.PullRequest{ | ||
User: github.User{Login: "googler_author"}, | ||
Labels: []github.Label{{Name: "service/google-x"}}, | ||
}, | ||
previousReviewers: []string{"googler_team_member"}, | ||
teamMembers: map[string][]string{"google-x": []string{"googler_team_member", "googler_team_member_2", "googler_team_member_3", "googler_team_member_4", "googler_team_member_5"}}, | ||
expectSpecificReviewers: []string{"googler_team_member"}, | ||
}, | ||
"active reviewers will not be re-requested": { | ||
pullRequest: github.PullRequest{ | ||
User: github.User{Login: "googler_author"}, | ||
Labels: []github.Label{{Name: "service/google-x"}}, | ||
}, | ||
requestedReviewers: []string{"googler_team_member"}, | ||
teamMembers: map[string][]string{"google-x": []string{"googler_team_member"}}, | ||
expectSpecificReviewers: []string{}, | ||
}, | ||
"authors will not be requested on their own PRs": { | ||
pullRequest: github.PullRequest{ | ||
User: github.User{Login: "googler_team_member"}, | ||
Labels: []github.Label{{Name: "service/google-x"}}, | ||
}, | ||
teamMembers: map[string][]string{"google-x": []string{"googler_team_member"}}, | ||
expectSpecificReviewers: []string{}, | ||
}, | ||
"other team members be requested even if the author is excluded": { | ||
pullRequest: github.PullRequest{ | ||
User: github.User{Login: "googler_team_member"}, | ||
Labels: []github.Label{{Name: "service/google-x"}}, | ||
}, | ||
teamMembers: map[string][]string{"google-x": []string{"googler_team_member", "googler_team_member_2"}}, | ||
expectSpecificReviewers: []string{"googler_team_member_2"}, | ||
}, | ||
"multiple teams can be requested at once": { | ||
pullRequest: github.PullRequest{ | ||
User: github.User{Login: "googler_author"}, | ||
Labels: []github.Label{{Name: "service/google-x"}, {Name: "service/google-y"}, {Name: "service/google-z"}}, | ||
}, | ||
teamMembers: map[string][]string{"google-x": []string{"googler_team_member"}, "google-y": []string{"googler_y_team_member"}}, | ||
expectSpecificReviewers: []string{"googler_team_member", "googler_y_team_member"}, | ||
}, | ||
">3 service teams will not be requested": { | ||
pullRequest: github.PullRequest{ | ||
User: github.User{Login: "googler_author"}, | ||
Labels: []github.Label{{Name: "service/google-x"}, {Name: "service/google-y"}, {Name: "service/google-z"}, {Name: "service/google-a"}}, | ||
}, | ||
teamMembers: map[string][]string{"google-x": []string{"googler_team_member"}, "google-y": []string{"googler_y_team_member"}}, | ||
expectSpecificReviewers: []string{}, | ||
}, | ||
} | ||
for tn, tc := range cases { | ||
t.Run(tn, func(t *testing.T) { | ||
requestedReviewers := []github.User{} | ||
for _, login := range tc.requestedReviewers { | ||
requestedReviewers = append(requestedReviewers, github.User{Login: login}) | ||
} | ||
previousReviewers := []github.User{} | ||
for _, login := range tc.previousReviewers { | ||
previousReviewers = append(previousReviewers, github.User{Login: login}) | ||
} | ||
teamMembers := map[string][]github.User{} | ||
for team, logins := range tc.teamMembers { | ||
teamMembers[team] = []github.User{} | ||
for _, login := range logins { | ||
teamMembers[team] = append(teamMembers[team], github.User{Login: login}) | ||
} | ||
} | ||
gh := &mockGithub{ | ||
pullRequest: tc.pullRequest, | ||
requestedReviewers: requestedReviewers, | ||
previousReviewers: previousReviewers, | ||
teamMembers: teamMembers, | ||
calledMethods: make(map[string][][]any), | ||
} | ||
|
||
execRequestServiceReviewers("1", gh, enrolledTeamsYaml) | ||
|
||
actualReviewers := []string{} | ||
for _, args := range gh.calledMethods["RequestPullRequestReviewer"] { | ||
actualReviewers = append(actualReviewers, args[1].(string)) | ||
} | ||
|
||
if tc.expectSpecificReviewers != nil { | ||
assert.ElementsMatch(t, tc.expectSpecificReviewers, actualReviewers) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.