diff --git a/cmd/governctl/pr/check/merable.go b/cmd/governctl/pr/check/merable.go index e9f4f3f..707758c 100644 --- a/cmd/governctl/pr/check/merable.go +++ b/cmd/governctl/pr/check/merable.go @@ -28,6 +28,8 @@ type Mergable struct { ApproverComments []string `long:"approver-comments" env:"GOVERN_APPROVER_COMMENTS" usage:"Regular expression that an approver writes"` ApproverTeams []string `long:"approver-teams" env:"GOVERN_APPROVER_TEAMS" usage:"The GitHub team that the approver must be a part of to be considered an approver"` ApproveStates []string `long:"approve-states" env:"GOVERN_APPROVE_STATES" usage:"The state of the GitHub approval from the assignee" default:"approve"` + CommitterEmail string `long:"committer-email" short:"e" env:"GOVERN_COMMITTER_EMAIL" usage:"Set the Git committer author's email"` + CommitterName string `long:"committer-name" short:"n" env:"GOVERN_COMMITTER_NAME" usage:"Set the Git committer author's name"` IgnoreLabels []string `long:"ignore-labels" env:"GOVERN_IGNORE_LABELS" usage:"Ignore the PR if it has any of these labels"` IgnoreStates []string `long:"ignore-states" env:"GOVERN_IGNORE_STATES" usage:"Ignore the PR if it has any of these states"` Labels []string `long:"labels" env:"GOVERN_LABELS" usage:"The PR must have these labels to be considered mergable"` @@ -89,6 +91,8 @@ func (opts *Mergable) Run(ctx context.Context, args []string) error { ghClient, ghOrg, ghRepo, + opts.CommitterName, + opts.CommitterEmail, ghPrId, // ghpr.WithBaseBranch(opts.BaseBranch), ghpr.WithWorkdir(kitcfg.G[config.Config](ctx).TempDir), diff --git a/cmd/governctl/pr/check/patch.go b/cmd/governctl/pr/check/patch.go index a4b6485..006d25e 100644 --- a/cmd/governctl/pr/check/patch.go +++ b/cmd/governctl/pr/check/patch.go @@ -29,6 +29,8 @@ import ( ) type Patch struct { + CommitterEmail string `long:"committer-email" short:"e" env:"GOVERN_COMMITTER_EMAIL" usage:"Set the Git committer author's email"` + CommitterName string `long:"committer-name" short:"n" env:"GOVERN_COMMITTER_NAME" usage:"Set the Git committer author's name"` Output string `long:"output" short:"o" env:"GOVERN_OUTPUT" usage:"Set the output format of choice [table, html, json, yaml]" default:"table"` CheckpatchScript string `long:"checkpatch-script" env:"GOVERN_CHECKPATCH_SCRIPT" usage:"Use an existing checkpatch.pl script"` BaseBranch string `long:"base" env:"GOVERN_BASE_BRANCH" usage:"Set the base branch name that the PR will be rebased onto"` @@ -91,6 +93,8 @@ func (opts *Patch) Run(ctx context.Context, args []string) error { ghClient, ghOrg, ghRepo, + opts.CommitterName, + opts.CommitterEmail, ghPrId, ghpr.WithBaseBranch(opts.BaseBranch), ghpr.WithWorkdir(kitcfg.G[config.Config](ctx).TempDir), diff --git a/cmd/governctl/pr/merge.go b/cmd/governctl/pr/merge.go index c7d5864..9604587 100644 --- a/cmd/governctl/pr/merge.go +++ b/cmd/governctl/pr/merge.go @@ -95,6 +95,8 @@ func (opts *Merge) Run(ctx context.Context, args []string) error { ghClient, ghOrg, ghRepo, + opts.CommitterName, + opts.CommitterEmail, ghPrId, ghpr.WithBaseBranch(opts.BaseBranch), ghpr.WithWorkdir(kitcfg.G[config.Config](ctx).TempDir), diff --git a/internal/ghpr/ghpr.go b/internal/ghpr/ghpr.go index 4bbe9ae..55d588c 100644 --- a/internal/ghpr/ghpr.go +++ b/internal/ghpr/ghpr.go @@ -45,7 +45,7 @@ type PullRequest struct { // NewPullRequestFromID fetches information about a pull request via GitHub as // well as preparing the pull request as a series of patches that can be parsed // internally. -func NewPullRequestFromID(ctx context.Context, client *ghapi.GithubClient, ghOrg, ghRepo string, ghPrId int, opts ...PullRequestOption) (*PullRequest, error) { +func NewPullRequestFromID(ctx context.Context, client *ghapi.GithubClient, ghOrg, ghRepo, committerName, committerEmail string, ghPrId int, opts ...PullRequestOption) (*PullRequest, error) { var err error pr := PullRequest{ @@ -166,9 +166,32 @@ func NewPullRequestFromID(ctx context.Context, client *ghapi.GithubClient, ghOrg return nil, fmt.Errorf("could not checkout branch '%s': %w", refname, err) } + log.G(ctx).Infof("configuring committer name and email") + + // Add commiter name + if committerName != "" { + cmd := exec.Command("git", "-C", pr.localRepo, "config", "user.name", committerName) + cmd.Stderr = log.G(ctx).WriterLevel(logrus.ErrorLevel) + cmd.Stdout = log.G(ctx).WriterLevel(logrus.DebugLevel) + if err := cmd.Run(); err != nil { + return nil, fmt.Errorf("could not config user: %w", err) + } + } + + // Add commiter email + if committerEmail != "" { + cmd := exec.Command("git", "-C", pr.localRepo, "config", "user.email", committerEmail) + cmd.Stderr = log.G(ctx).WriterLevel(logrus.ErrorLevel) + cmd.Stdout = log.G(ctx).WriterLevel(logrus.DebugLevel) + if err := cmd.Run(); err != nil { + return nil, fmt.Errorf("could not config email: %w", err) + } + } + log.G(ctx).Infof("rebasing pull request's branch on to '%s' branch", pr.baseBranch) - rebase := exec.Command( + rebase := exec.CommandContext( + ctx, "git", "-C", pr.localRepo, "rebase",