From 128411305aa26572cd9be2c0f1420cfe782e3dfe Mon Sep 17 00:00:00 2001 From: Cezar Craciunoiu Date: Wed, 7 Feb 2024 16:00:50 +0200 Subject: [PATCH] feat: Add flag to set email/user globally Signed-off-by: Cezar Craciunoiu --- cmd/governctl/pr/check/merable.go | 2 ++ cmd/governctl/pr/check/patch.go | 2 ++ cmd/governctl/pr/merge.go | 2 ++ internal/ghpr/ghpr.go | 16 +++++++++++++--- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/cmd/governctl/pr/check/merable.go b/cmd/governctl/pr/check/merable.go index 707758c..550def1 100644 --- a/cmd/governctl/pr/check/merable.go +++ b/cmd/governctl/pr/check/merable.go @@ -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"` @@ -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), ) diff --git a/cmd/governctl/pr/check/patch.go b/cmd/governctl/pr/check/patch.go index 6d8dab8..81ff919 100644 --- a/cmd/governctl/pr/check/patch.go +++ b/cmd/governctl/pr/check/patch.go @@ -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"` @@ -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), ) diff --git a/cmd/governctl/pr/merge.go b/cmd/governctl/pr/merge.go index 9604587..6490970 100644 --- a/cmd/governctl/pr/merge.go +++ b/cmd/governctl/pr/merge.go @@ -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"` @@ -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), ) diff --git a/internal/ghpr/ghpr.go b/internal/ghpr/ghpr.go index 6d07aee..708ade4 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, 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{ @@ -170,7 +170,12 @@ func NewPullRequestFromID(ctx context.Context, client *ghapi.GithubClient, ghOrg // Add commiter name if committerName != "" { - cmd := exec.Command("git", "-C", pr.localRepo, "config", "user.name", committerName) + args := []string{"-C", pr.localRepo, "config"} + if committerGlobal { + args = append(args, "--global") + } + args = append(args, "user.name", committerName) + cmd := exec.Command("git", args...) cmd.Stderr = log.G(ctx).WriterLevel(logrus.ErrorLevel) cmd.Stdout = log.G(ctx).WriterLevel(logrus.DebugLevel) if err := cmd.Run(); err != nil { @@ -180,7 +185,12 @@ 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) + args := []string{"-C", pr.localRepo, "config"} + if committerGlobal { + args = append(args, "--global") + } + args = append(args, "user.email", committerEmail) + cmd := exec.Command("git", args...) cmd.Stderr = log.G(ctx).WriterLevel(logrus.ErrorLevel) cmd.Stdout = log.G(ctx).WriterLevel(logrus.DebugLevel) if err := cmd.Run(); err != nil {