From 6f2a1e649ff5c6e54ebeb32f395266929d83c920 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 ++ internal/ghpr/ghpr.go | 11 ++++++++--- 3 files changed, 12 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/internal/ghpr/ghpr.go b/internal/ghpr/ghpr.go index 6d07aee..c1cbca2 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{ @@ -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 { @@ -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 {