Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

GitHub team review #351

Merged
merged 5 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
9 changes: 9 additions & 0 deletions README.md
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No changes needed in this file as it will be automatically generated on merge. Please revert the readme changes.

Copy link
Contributor Author

@tadam313 tadam313 May 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah okay sorry, I thought we need to run the script manually :)

Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ log-level: info
# If this value is set, reviewers will be randomized.
max-reviewers: 0

# If this value is set, team reviewers will be randomized
max-team-reviewers: 0

# The name of a GitHub organization. All repositories in that organization will be used.
org:
- example
Expand Down Expand Up @@ -222,6 +225,10 @@ skip-repo:
# Use SSH cloning URL instead of HTTPS + token. This requires that a setup with ssh keys that have access to all repos and that the server is already in known_hosts.
ssh-auth: false

# Github team names of the reviewers, in format: 'org/team'
team-reviewers:
- example

# The personal access token for the targeting platform. Can also be set using the GITHUB_TOKEN/GITLAB_TOKEN/GITEA_TOKEN/BITBUCKET_SERVER_TOKEN environment variable.
token:

Expand Down Expand Up @@ -608,6 +615,7 @@ Flags:
--log-format string The formatting of the logs. Available values: text, json, json-pretty. (default "text")
-L, --log-level string The level of logging that should be made. Available values: trace, debug, info, error. (default "info")
-M, --max-reviewers int If this value is set, reviewers will be randomized.
--max-team-reviewers int If this value is set, team reviewers will be randomized
-O, --org strings The name of a GitHub organization. All repositories in that organization will be used.
-o, --output string The file that the output of the script should be outputted to. "-" means stdout. (default "-")
-p, --platform string The platform that is used. Available values: github, gitlab, gitea, bitbucket_server. (default "github")
Expand All @@ -620,6 +628,7 @@ Flags:
--skip-pr Skip pull request and directly push to the branch.
-s, --skip-repo strings Skip changes on specified repositories, the name is including the owner of repository in the format "ownerName/repoName".
--ssh-auth Use SSH cloning URL instead of HTTPS + token. This requires that a setup with ssh keys that have access to all repos and that the server is already in known_hosts.
--team-reviewers strings Github team names of the reviewers, in format: 'org/team'
-T, --token string The personal access token for the targeting platform. Can also be set using the GITHUB_TOKEN/GITLAB_TOKEN/GITEA_TOKEN/BITBUCKET_SERVER_TOKEN environment variable.
--topic strings The topic of a GitHub/GitLab/Gitea repository. All repositories having at least one matching topic are targeted.
-U, --user strings The name of a user. All repositories owned by that user will be used.
Expand Down
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", "test@example.com",
"-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", "test@example.com",
"-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