Skip to content

Commit 1284113

Browse files
committed
feat: Add flag to set email/user globally
Signed-off-by: Cezar Craciunoiu <cezar.craciunoiu@gmail.com>
1 parent 60667aa commit 1284113

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

cmd/governctl/pr/check/merable.go

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type Mergable struct {
2929
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"`
3030
ApproveStates []string `long:"approve-states" env:"GOVERN_APPROVE_STATES" usage:"The state of the GitHub approval from the assignee" default:"approve"`
3131
CommitterEmail string `long:"committer-email" short:"e" env:"GOVERN_COMMITTER_EMAIL" usage:"Set the Git committer author's email"`
32+
CommitterGlobal bool `long:"committer-global" env:"GOVERN_COMMITTER_GLOBAL" usage:"Set the Git committer author's email/name globally"`
3233
CommitterName string `long:"committer-name" short:"n" env:"GOVERN_COMMITTER_NAME" usage:"Set the Git committer author's name"`
3334
IgnoreLabels []string `long:"ignore-labels" env:"GOVERN_IGNORE_LABELS" usage:"Ignore the PR if it has any of these labels"`
3435
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 {
9495
opts.CommitterName,
9596
opts.CommitterEmail,
9697
ghPrId,
98+
opts.CommitterGlobal,
9799
// ghpr.WithBaseBranch(opts.BaseBranch),
98100
ghpr.WithWorkdir(kitcfg.G[config.Config](ctx).TempDir),
99101
)

cmd/governctl/pr/check/patch.go

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030

3131
type Patch struct {
3232
CommitterEmail string `long:"committer-email" short:"e" env:"GOVERN_COMMITTER_EMAIL" usage:"Set the Git committer author's email"`
33+
CommiterGlobal bool `long:"committer-global" env:"GOVERN_COMMITTER_GLOBAL" usage:"Set the Git committer author's email/name globally"`
3334
CommitterName string `long:"committer-name" short:"n" env:"GOVERN_COMMITTER_NAME" usage:"Set the Git committer author's name"`
3435
Output string `long:"output" short:"o" env:"GOVERN_OUTPUT" usage:"Set the output format of choice [table, html, json, yaml]" default:"table"`
3536
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 {
9798
opts.CommitterName,
9899
opts.CommitterEmail,
99100
ghPrId,
101+
opts.CommiterGlobal,
100102
ghpr.WithBaseBranch(opts.BaseBranch),
101103
ghpr.WithWorkdir(kitcfg.G[config.Config](ctx).TempDir),
102104
)

cmd/governctl/pr/merge.go

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type Merge struct {
3838
BaseBranch string `long:"base" env:"GOVERN_BASE" usage:"Set the base branch name that the PR will be rebased onto"`
3939
Branch string `long:"branch" env:"GOVERN_BRANCH" usage:"Set the branch to merge into"`
4040
CommitterEmail string `long:"committer-email" short:"e" env:"GOVERN_COMMITTER_EMAIL" usage:"Set the Git committer author's email"`
41+
CommitterGlobal bool `long:"committer-global" env:"GOVERN_COMMITTER_GLOBAL" usage:"Set the Git committer author's email/name globally"`
4142
CommitterName string `long:"committer-name" short:"n" env:"GOVERN_COMMITTER_NAME" usage:"Set the Git committer author's name"`
4243
IgnoreLabels []string `long:"ignore-labels" env:"GOVERN_IGNORE_LABELS" usage:"Ignore the PR if it has any of these labels"`
4344
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 {
9899
opts.CommitterName,
99100
opts.CommitterEmail,
100101
ghPrId,
102+
opts.CommitterGlobal,
101103
ghpr.WithBaseBranch(opts.BaseBranch),
102104
ghpr.WithWorkdir(kitcfg.G[config.Config](ctx).TempDir),
103105
)

internal/ghpr/ghpr.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type PullRequest struct {
4545
// NewPullRequestFromID fetches information about a pull request via GitHub as
4646
// well as preparing the pull request as a series of patches that can be parsed
4747
// internally.
48-
func NewPullRequestFromID(ctx context.Context, client *ghapi.GithubClient, ghOrg, ghRepo, committerName, committerEmail string, ghPrId int, opts ...PullRequestOption) (*PullRequest, error) {
48+
func NewPullRequestFromID(ctx context.Context, client *ghapi.GithubClient, ghOrg, ghRepo, committerName, committerEmail string, ghPrId int, committerGlobal bool, opts ...PullRequestOption) (*PullRequest, error) {
4949
var err error
5050

5151
pr := PullRequest{
@@ -170,7 +170,12 @@ func NewPullRequestFromID(ctx context.Context, client *ghapi.GithubClient, ghOrg
170170

171171
// Add commiter name
172172
if committerName != "" {
173-
cmd := exec.Command("git", "-C", pr.localRepo, "config", "user.name", committerName)
173+
args := []string{"-C", pr.localRepo, "config"}
174+
if committerGlobal {
175+
args = append(args, "--global")
176+
}
177+
args = append(args, "user.name", committerName)
178+
cmd := exec.Command("git", args...)
174179
cmd.Stderr = log.G(ctx).WriterLevel(logrus.ErrorLevel)
175180
cmd.Stdout = log.G(ctx).WriterLevel(logrus.DebugLevel)
176181
if err := cmd.Run(); err != nil {
@@ -180,7 +185,12 @@ func NewPullRequestFromID(ctx context.Context, client *ghapi.GithubClient, ghOrg
180185

181186
// Add commiter email
182187
if committerEmail != "" {
183-
cmd := exec.Command("git", "-C", pr.localRepo, "config", "user.email", committerEmail)
188+
args := []string{"-C", pr.localRepo, "config"}
189+
if committerGlobal {
190+
args = append(args, "--global")
191+
}
192+
args = append(args, "user.email", committerEmail)
193+
cmd := exec.Command("git", args...)
184194
cmd.Stderr = log.G(ctx).WriterLevel(logrus.ErrorLevel)
185195
cmd.Stdout = log.G(ctx).WriterLevel(logrus.DebugLevel)
186196
if err := cmd.Run(); err != nil {

0 commit comments

Comments
 (0)