Skip to content

Commit

Permalink
feat(github): added option to add team reviewers (lindell#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
tadam313 authored and lindell committed May 2, 2023
1 parent 1fddf2e commit bfe05b9
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 15 deletions.
6 changes: 6 additions & 0 deletions cmd/cmd-run.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ func RunCmd() *cobra.Command {
cmd.Flags().StringP("pr-body", "b", "", "The body of the commit message. Will default to everything but the first line of the commit message if none is set.")
cmd.Flags().StringP("commit-message", "m", "", "The commit message. Will default to title + body if none is set.")
cmd.Flags().StringSliceP("reviewers", "r", nil, "The username of the reviewers to be added on the pull request.")
cmd.Flags().StringSliceP("team-reviewers", "", nil, "Github team names of the reviewers, in format: 'org/team'")
cmd.Flags().StringSliceP("assignees", "a", nil, "The username of the assignees to be added on the pull request.")
cmd.Flags().IntP("max-reviewers", "M", 0, "If this value is set, reviewers will be randomized.")
cmd.Flags().IntP("max-team-reviewers", "", 0, "If this value is set, team reviewers will be randomized")
cmd.Flags().IntP("concurrent", "C", 1, "The maximum number of concurrent runs.")
cmd.Flags().BoolP("skip-pr", "", false, "Skip pull request and directly push to the branch.")
cmd.Flags().StringSliceP("skip-repo", "s", nil, "Skip changes on specified repositories, the name is including the owner of repository in the format \"ownerName/repoName\".")
Expand Down Expand Up @@ -79,7 +81,9 @@ func run(cmd *cobra.Command, _ []string) error {
prBody, _ := flag.GetString("pr-body")
commitMessage, _ := flag.GetString("commit-message")
reviewers, _ := flag.GetStringSlice("reviewers")
teamReviewers, _ := flag.GetStringSlice("team-reviewers")
maxReviewers, _ := flag.GetInt("max-reviewers")
maxTeamReviewers, _ := flag.GetInt("max-team-reviewers")
concurrent, _ := flag.GetInt("concurrent")
skipPullRequest, _ := flag.GetBool("skip-pr")
skipRepository, _ := flag.GetStringSlice("skip-repo")
Expand Down Expand Up @@ -185,7 +189,9 @@ func run(cmd *cobra.Command, _ []string) error {
PullRequestTitle: prTitle,
PullRequestBody: prBody,
Reviewers: reviewers,
TeamReviewers: teamReviewers,
MaxReviewers: maxReviewers,
MaxTeamReviewers: maxTeamReviewers,
Interactive: interactive,
DryRun: dryRun,
Fork: forkMode,
Expand Down
21 changes: 12 additions & 9 deletions internal/multigitter/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ type Runner struct {
PullRequestTitle string
PullRequestBody string
Reviewers []string
MaxReviewers int // If set to zero, all reviewers will be used
TeamReviewers []string
MaxReviewers int // If set to zero, all reviewers will be use
MaxTeamReviewers int // If set to zero, all team-reviewers will be used
DryRun bool
CommitAuthor *git.CommitAuthor
BaseBranch string // The base branch of the PR, use default branch if not set
Expand Down Expand Up @@ -326,14 +328,15 @@ func (r *Runner) runSingleRepo(ctx context.Context, repo scm.Repository) (scm.Pu
} else {
log.Info("Creating pull request")
pr, err = r.VersionController.CreatePullRequest(ctx, repo, prRepo, scm.NewPullRequest{
Title: r.PullRequestTitle,
Body: r.PullRequestBody,
Head: r.FeatureBranch,
Base: baseBranch,
Reviewers: getReviewers(r.Reviewers, r.MaxReviewers),
Assignees: r.Assignees,
Draft: r.Draft,
Labels: r.Labels,
Title: r.PullRequestTitle,
Body: r.PullRequestBody,
Head: r.FeatureBranch,
Base: baseBranch,
Reviewers: getReviewers(r.Reviewers, r.MaxReviewers),
TeamReviewers: getReviewers(r.TeamReviewers, r.MaxTeamReviewers),
Assignees: r.Assignees,
Draft: r.Draft,
Labels: r.Labels,
})
if err != nil {
return nil, err
Expand Down
6 changes: 4 additions & 2 deletions internal/scm/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,14 @@ func (g *Github) createPullRequest(ctx context.Context, repo repository, prRepo
}

func (g *Github) addReviewers(ctx context.Context, repo repository, newPR scm.NewPullRequest, createdPR *github.PullRequest) error {
if len(newPR.Reviewers) == 0 {
if len(newPR.Reviewers) == 0 && len(newPR.TeamReviewers) == 0 {
return nil
}

_, _, err := retry(ctx, func() (*github.PullRequest, *github.Response, error) {
return g.ghClient.PullRequests.RequestReviewers(ctx, repo.ownerName, repo.name, createdPR.GetNumber(), github.ReviewersRequest{
Reviewers: newPR.Reviewers,
Reviewers: newPR.Reviewers,
TeamReviewers: newPR.TeamReviewers,
})
})
return err
Expand Down
9 changes: 5 additions & 4 deletions internal/scm/pullrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ type NewPullRequest struct {
Head string
Base string

Reviewers []string // The username of all reviewers
Assignees []string
Draft bool
Labels []string
Reviewers []string // The username of all reviewers
TeamReviewers []string // Teams to assign as reviewers
Assignees []string
Draft bool
Labels []string
}

// PullRequestStatus is the status of a pull request, including statuses of the last commit
Expand Down
27 changes: 27 additions & 0 deletions tests/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,16 @@ func TestTable(t *testing.T) {
"--author-email", "[email protected]",
"-m", "custom message",
"-r", "reviewer1,reviewer2",
"--team-reviewers", "team-1,team-2",
changerBinaryPath,
},
verify: func(t *testing.T, vcMock *vcmock.VersionController, runData runData) {
require.Len(t, vcMock.PullRequests, 1)
assert.Len(t, vcMock.PullRequests[0].Reviewers, 2)
assert.Contains(t, vcMock.PullRequests[0].Reviewers, "reviewer1")
assert.Contains(t, vcMock.PullRequests[0].Reviewers, "reviewer2")
assert.Contains(t, vcMock.PullRequests[0].TeamReviewers, "team-1")
assert.Contains(t, vcMock.PullRequests[0].TeamReviewers, "team-2")
},
},

Expand Down Expand Up @@ -285,6 +288,30 @@ func TestTable(t *testing.T) {
},
},

{
name: "random team reviewers",
vcCreate: func(t *testing.T) *vcmock.VersionController {
return &vcmock.VersionController{
Repositories: []vcmock.Repository{
createRepo(t, "owner", "should-change", "i like apples"),
},
}
},
args: []string{
"run",
"--author-name", "Test Author",
"--author-email", "[email protected]",
"-m", "custom message",
"--team-reviewers", "team1,team2,team3",
"--max-team-reviewers", "2",
changerBinaryPath,
},
verify: func(t *testing.T, vcMock *vcmock.VersionController, runData runData) {
require.Len(t, vcMock.PullRequests, 1)
assert.Len(t, vcMock.PullRequests[0].TeamReviewers, 2)
},
},

{
name: "dry run",
vcCreate: func(t *testing.T) *vcmock.VersionController {
Expand Down

0 comments on commit bfe05b9

Please sign in to comment.