Skip to content

Commit

Permalink
feat: Add flag to set email/user globally
Browse files Browse the repository at this point in the history
Signed-off-by: Cezar Craciunoiu <[email protected]>
  • Loading branch information
craciunoiuc committed Feb 7, 2024
1 parent 60667aa commit cf7f8c1
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
2 changes: 2 additions & 0 deletions cmd/governctl/pr/check/merable.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Mergable struct {
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"`
CommitterGlobal bool `long:"committer-global" env:"GOVERN_COMMITTER_GLOBAL" usage:"Set the Git committer author's email/name globally"`
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"`
Expand Down Expand Up @@ -94,6 +95,7 @@ func (opts *Mergable) Run(ctx context.Context, args []string) error {
opts.CommitterName,
opts.CommitterEmail,
ghPrId,
opts.CommitterGlobal,
// ghpr.WithBaseBranch(opts.BaseBranch),
ghpr.WithWorkdir(kitcfg.G[config.Config](ctx).TempDir),
)
Expand Down
2 changes: 2 additions & 0 deletions cmd/governctl/pr/check/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

type Patch struct {
CommitterEmail string `long:"committer-email" short:"e" env:"GOVERN_COMMITTER_EMAIL" usage:"Set the Git committer author's email"`
CommiterGlobal bool `long:"committer-global" env:"GOVERN_COMMITTER_GLOBAL" usage:"Set the Git committer author's email/name globally"`
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"`
Expand Down Expand Up @@ -97,6 +98,7 @@ func (opts *Patch) Run(ctx context.Context, args []string) error {
opts.CommitterName,
opts.CommitterEmail,
ghPrId,
opts.CommiterGlobal,
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 @@ -38,6 +38,7 @@ type Merge struct {
BaseBranch string `long:"base" env:"GOVERN_BASE" usage:"Set the base branch name that the PR will be rebased onto"`
Branch string `long:"branch" env:"GOVERN_BRANCH" usage:"Set the branch to merge into"`
CommitterEmail string `long:"committer-email" short:"e" env:"GOVERN_COMMITTER_EMAIL" usage:"Set the Git committer author's email"`
CommitterGlobal bool `long:"committer-global" env:"GOVERN_COMMITTER_GLOBAL" usage:"Set the Git committer author's email/name globally"`
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"`
Expand Down Expand Up @@ -98,6 +99,7 @@ func (opts *Merge) Run(ctx context.Context, args []string) error {
opts.CommitterName,
opts.CommitterEmail,
ghPrId,
opts.CommitterGlobal,
ghpr.WithBaseBranch(opts.BaseBranch),
ghpr.WithWorkdir(kitcfg.G[config.Config](ctx).TempDir),
)
Expand Down
11 changes: 8 additions & 3 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, committerName, committerEmail string, ghPrId int, opts ...PullRequestOption) (*PullRequest, error) {
func NewPullRequestFromID(ctx context.Context, client *ghapi.GithubClient, ghOrg, ghRepo, committerName, committerEmail string, ghPrId int, committerGlobal bool, opts ...PullRequestOption) (*PullRequest, error) {
var err error

pr := PullRequest{
Expand Down Expand Up @@ -168,9 +168,14 @@ func NewPullRequestFromID(ctx context.Context, client *ghapi.GithubClient, ghOrg

log.G(ctx).Infof("configuring committer name and email")

committerGlobalFlag := ""
if committerGlobal {
committerGlobalFlag = "--global"
}

// Add commiter name
if committerName != "" {
cmd := exec.Command("git", "-C", pr.localRepo, "config", "user.name", committerName)
cmd := exec.Command("git", "-C", pr.localRepo, "config", committerGlobalFlag, "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 {
Expand All @@ -180,7 +185,7 @@ func NewPullRequestFromID(ctx context.Context, client *ghapi.GithubClient, ghOrg

// Add commiter email
if committerEmail != "" {
cmd := exec.Command("git", "-C", pr.localRepo, "config", "user.email", committerEmail)
cmd := exec.Command("git", "-C", pr.localRepo, "config", committerGlobalFlag, "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 {
Expand Down

0 comments on commit cf7f8c1

Please sign in to comment.