Skip to content

Commit

Permalink
fix: Ensure name/email are set before rebasing
Browse files Browse the repository at this point in the history
Signed-off-by: Cezar Craciunoiu <cezar.craciunoiu@gmail.com>
  • Loading branch information
craciunoiuc committed Jan 27, 2024
1 parent 4162c08 commit 67ab299
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
4 changes: 4 additions & 0 deletions cmd/governctl/pr/check/merable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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),
Expand Down
4 changes: 4 additions & 0 deletions cmd/governctl/pr/check/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 2 additions & 0 deletions cmd/governctl/pr/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
27 changes: 25 additions & 2 deletions internal/ghpr/ghpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 67ab299

Please sign in to comment.