-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
133 changed files
with
5,459 additions
and
647 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright 2024 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" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// requestReviewerCmd represents the requestReviewer command | ||
var requestReviewerCmd = &cobra.Command{ | ||
Use: "request-reviewer", | ||
Short: "Assigns and re-requests reviewers", | ||
Long: `This command automatically requests (or re-requests) core contributor reviews for a PR based on whether the user is a core contributor. | ||
The command expects the following pull request details as arguments: | ||
1. PR Number | ||
2. Commit SHA | ||
3. Branch Name | ||
4. Head Repo URL | ||
5. Head Branch | ||
6. Base Branch | ||
It then performs the following operations: | ||
1. Determines the author of the pull request | ||
2. If the author is not a core contributor: | ||
a. Identifies the initially requested reviewer and those who previously reviewed this PR. | ||
b. Determines and requests reviewers based on the above. | ||
c. As appropriate, posts a welcome comment on the PR. | ||
`, | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
prNumber := args[0] | ||
fmt.Println("PR Number: ", prNumber) | ||
gh := github.NewClient() | ||
execRequestReviewer(prNumber, gh) | ||
}, | ||
} | ||
|
||
func execRequestReviewer(prNumber string, gh GithubClient) { | ||
pullRequest, err := gh.GetPullRequest(prNumber) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
author := pullRequest.User.Login | ||
if !github.IsCoreContributor(author) { | ||
fmt.Println("Not core contributor - assigning reviewer") | ||
|
||
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) | ||
} | ||
|
||
reviewersToRequest, newPrimaryReviewer := github.ChooseCoreReviewers(requestedReviewers, previousReviewers) | ||
|
||
for _, reviewer := range reviewersToRequest { | ||
err = gh.RequestPullRequestReviewer(prNumber, reviewer) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
if newPrimaryReviewer != "" { | ||
comment := github.FormatReviewerComment(newPrimaryReviewer) | ||
err = gh.PostComment(prNumber, comment) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(requestReviewerCmd) | ||
} |
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,103 @@ | ||
/* | ||
* Copyright 2024 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 ( | ||
"github.com/stretchr/testify/assert" | ||
"magician/github" | ||
"testing" | ||
) | ||
|
||
func TestExecRequestReviewer(t *testing.T) { | ||
availableReviewers := github.AvailableReviewers() | ||
cases := map[string]struct { | ||
pullRequest github.PullRequest | ||
requestedReviewers []string | ||
previousReviewers []string | ||
teamMembers map[string][]string | ||
expectSpecificReviewers []string | ||
expectReviewersFromList []string | ||
}{ | ||
"core contributor author doesn't get a new reviewer, re-request, or comment with no previous reviewers": { | ||
pullRequest: github.PullRequest{ | ||
User: github.User{Login: availableReviewers[0]}, | ||
}, | ||
expectSpecificReviewers: []string{}, | ||
}, | ||
"core contributor author doesn't get a new reviewer, re-request, or comment with previous reviewers": { | ||
pullRequest: github.PullRequest{ | ||
User: github.User{Login: availableReviewers[0]}, | ||
}, | ||
previousReviewers: []string{availableReviewers[1]}, | ||
expectSpecificReviewers: []string{}, | ||
}, | ||
"non-core-contributor author gets a new reviewer with no previous reviewers": { | ||
pullRequest: github.PullRequest{ | ||
User: github.User{Login: "author"}, | ||
}, | ||
expectReviewersFromList: availableReviewers, | ||
}, | ||
"non-core-contributor author doesn't get a new reviewer (but does get re-request) with previous reviewers": { | ||
pullRequest: github.PullRequest{ | ||
User: github.User{Login: "author"}, | ||
}, | ||
previousReviewers: []string{availableReviewers[1], "author2", availableReviewers[2]}, | ||
expectSpecificReviewers: []string{availableReviewers[1], availableReviewers[2]}, | ||
}, | ||
"non-core-contributor author doesn't get a new reviewer or a re-request with already-requested reviewers": { | ||
pullRequest: github.PullRequest{ | ||
User: github.User{Login: "author"}, | ||
}, | ||
requestedReviewers: []string{availableReviewers[1], "author2", availableReviewers[2]}, | ||
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}) | ||
} | ||
gh := &mockGithub{ | ||
pullRequest: tc.pullRequest, | ||
requestedReviewers: requestedReviewers, | ||
previousReviewers: previousReviewers, | ||
calledMethods: make(map[string][][]any), | ||
} | ||
|
||
execRequestReviewer("1", gh) | ||
|
||
actualReviewers := []string{} | ||
for _, args := range gh.calledMethods["RequestPullRequestReviewer"] { | ||
actualReviewers = append(actualReviewers, args[1].(string)) | ||
} | ||
|
||
if tc.expectSpecificReviewers != nil { | ||
assert.ElementsMatch(t, tc.expectSpecificReviewers, actualReviewers) | ||
} | ||
if tc.expectReviewersFromList != nil { | ||
for _, reviewer := range actualReviewers { | ||
assert.Contains(t, tc.expectReviewersFromList, reviewer) | ||
} | ||
} | ||
}) | ||
} | ||
} |
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.