Skip to content

Commit

Permalink
add --pr-reviewers & --pr-team-reviewers (#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
jashandeep-sohi authored Aug 2, 2024
1 parent c097f0f commit c8699ae
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/current-version/content/repos/pull-request.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ You can control how the Pull Requests will be created or updated using the follo
- `--pr-body-update-operation` (string): the type of operation when updating a Pull Request's body: either `ignore` (keep old value), `replace`, `prepend` or `append`. Default is: `ignore` for "append" strategy, `replace` for "reset" strategy, and not applicable for "recreate" strategy.
- `--pr-comment` (array of string): optional list of comments to add to the Pull Request.
- `--pr-assignees` (array of string): optional list of assignees (Github usernames) to add to the Pull Request.
- `--pr-reviewers` (array of string): optional list of reviewers (Github usernames) for the Pull Request.
- `--pr-team-reviewers` (array of string): optional list of team reviewers (Github team names) for the Pull Request.
- `--pr-labels` (array of string): optional list of labels to set on the pull requests, and used to find existing pull requests to update. Default to `["octopilot-update"]`.
- `--pr-base-branch` (string): name of the branch used as a base when creating pull requests. Default to `master`.
- `--pr-draft` (bool): if enabled, the Pull Request will be created as a draft - instead of regular ones. It means that the PRs can't be merged until marked as "ready for review". Default to `false`.
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ func init() {
pflag.StringVar(&options.GitHub.PullRequest.BodyUpdateOperation, "pr-body-update-operation", "", `The type of operation when updating the PR's body: "ignore" (keep old value), "replace", "prepend" or "append". Default is: "ignore" for "append" strategy, "replace" for "reset" strategy, and not applicable for "recreate" strategy.`)
pflag.StringArrayVar(&options.GitHub.PullRequest.Comments, "pr-comment", []string{}, "List of comments to add to the Pull Request.")
pflag.StringSliceVar(&options.GitHub.PullRequest.Assignees, "pr-assignees", []string{}, "List of users to assign PR to.")
pflag.StringSliceVar(&options.GitHub.PullRequest.Reviewers, "pr-reviewers", []string{}, "List of users to request a review from.")
pflag.StringSliceVar(&options.GitHub.PullRequest.TeamReviewers, "pr-team-reviewers", []string{}, "List of teams to request a review from.")
pflag.StringSliceVar(&options.GitHub.PullRequest.Labels, "pr-labels", []string{"octopilot-update"}, "List of labels set on the pull requests, and used to find existing pull requests to update.")
pflag.StringVar(&options.GitHub.PullRequest.BaseBranch, "pr-base-branch", "", "Name of the branch used as a base when creating pull requests. If empty, the branch used will be the one referenced by the HEAD of each cloned repository.")
pflag.BoolVar(&options.GitHub.PullRequest.Draft, "pr-draft", false, `Create "draft" Pull Requests, instead of regular ones. It means that the PRs can't be merged until marked as "ready for review".`)
Expand Down
2 changes: 2 additions & 0 deletions repository/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ type PullRequestOptions struct {
BodyUpdateOperation string
Comments []string
Assignees []string
Reviewers []string
TeamReviewers []string
Draft bool
Merge PullRequestMergeOptions
}
Expand Down
46 changes: 46 additions & 0 deletions repository/pull_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ func (r Repository) createPullRequest(ctx context.Context, options GitHubOptions
return nil, fmt.Errorf("failed to add assignees to Pull Request %s: %w", pr.GetHTMLURL(), err)
}

err = r.addPullRequestReviewers(ctx, options, pr)
if err != nil {
return nil, fmt.Errorf("failed to add reviewers for Pull Request %s: %w", pr.GetHTMLURL(), err)
}

return pr, nil
}

Expand Down Expand Up @@ -292,6 +297,47 @@ func (r Repository) addPullRequestAssignees(ctx context.Context, options GitHubO
return nil
}

func (r Repository) addPullRequestReviewers(ctx context.Context, options GitHubOptions, pr *github.PullRequest) error {
if len(options.PullRequest.Reviewers) == 0 && len(options.PullRequest.TeamReviewers) == 0 {
logrus.WithFields(logrus.Fields{
"repository": r.FullName(),
"pull-request": pr.GetHTMLURL(),
}).Debug("No reviewers to add to the Pull Request")
return nil
}

client, _, err := githubClient(ctx, options)
if err != nil {
return fmt.Errorf("failed to create github client: %w", err)
}

logrus.WithFields(logrus.Fields{
"repository": r.FullName(),
"pull-request": pr.GetHTMLURL(),
"reviewers": options.PullRequest.Reviewers,
"team-reviewers": options.PullRequest.TeamReviewers,
}).Trace("Adding reviewers to the Pull Request")

reviewers := github.ReviewersRequest{
Reviewers: options.PullRequest.Reviewers,
TeamReviewers: options.PullRequest.TeamReviewers,
}
_, _, err = client.PullRequests.RequestReviewers(ctx, r.Owner, r.Name, pr.GetNumber(), reviewers)

if err != nil {
return fmt.Errorf("failed to add reviewers to PR %s: %w", pr.GetHTMLURL(), err)
}

logrus.WithFields(logrus.Fields{
"repository": r.FullName(),
"pull-request": pr.GetHTMLURL(),
"reviewers": options.PullRequest.Reviewers,
"team-reviewers": options.PullRequest.TeamReviewers,
}).Debug("Reviewers added to the Pull Request")

return nil
}

func (r Repository) mergePullRequest(ctx context.Context, options GitHubOptions, pr *github.PullRequest) error {
if options.PullRequest.Merge.Auto {
return r.mergePullRequestUsingAutoMerge(ctx, options, pr)
Expand Down

0 comments on commit c8699ae

Please sign in to comment.