From a45f50870bf0bb14e26bcca448b3b02d94e5e3ad Mon Sep 17 00:00:00 2001 From: Johan Lindell Date: Tue, 28 Jul 2020 13:39:11 +0200 Subject: [PATCH] feat: added option to specify the commit author --- cmd/run.go | 19 +++++++++++++++++++ internal/domain/commit.go | 7 +++++++ internal/git/git.go | 19 ++++++++++++++++++- internal/multigitter/run.go | 10 ++++++---- 4 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 internal/domain/commit.go diff --git a/cmd/run.go b/cmd/run.go index f273f5a7..de095d58 100755 --- a/cmd/run.go +++ b/cmd/run.go @@ -8,6 +8,8 @@ import ( "path" "strings" + "github.com/lindell/multi-gitter/internal/domain" + "github.com/lindell/multi-gitter/internal/multigitter" "github.com/spf13/cobra" ) @@ -35,6 +37,8 @@ func init() { RunCmd.Flags().StringSliceP("reviewers", "r", nil, "The username of the reviewers to be added on the pull request.") RunCmd.Flags().IntP("max-reviewers", "M", 0, "If this value is set, reviewers will be randomized") RunCmd.Flags().BoolP("dry-run", "d", false, "Run without pushing changes or creating pull requests") + RunCmd.Flags().StringP("author-name", "", "", "If set, this fields will be used as the name of the committer") + RunCmd.Flags().StringP("author-email", "", "", "If set, this fields will be used as the email of the committer") } func run(cmd *cobra.Command, args []string) error { @@ -47,6 +51,8 @@ func run(cmd *cobra.Command, args []string) error { reviewers, _ := flag.GetStringSlice("reviewers") maxReviewers, _ := flag.GetInt("max-reviewers") dryRun, _ := flag.GetBool("dry-run") + authorName, _ := flag.GetString("author-name") + authorEmail, _ := flag.GetString("author-email") token, err := getToken(flag) if err != nil { @@ -71,6 +77,18 @@ func run(cmd *cobra.Command, args []string) error { } } + // Parse commit author data + var commitAuthor *domain.CommitAuthor + if authorName != "" || authorEmail != "" { + if authorName == "" || authorEmail == "" { + return errors.New("both author-name and author-email has to be set if the other is set") + } + commitAuthor = &domain.CommitAuthor{ + Name: authorName, + Email: authorEmail, + } + } + workingDir, err := os.Getwd() if err != nil { return errors.New("could not get the working directory") @@ -94,6 +112,7 @@ func run(cmd *cobra.Command, args []string) error { Reviewers: reviewers, MaxReviewers: maxReviewers, DryRun: dryRun, + CommitAuthor: commitAuthor, } err = runner.Run(context.Background()) diff --git a/internal/domain/commit.go b/internal/domain/commit.go new file mode 100644 index 00000000..e406d010 --- /dev/null +++ b/internal/domain/commit.go @@ -0,0 +1,7 @@ +package domain + +// CommitAuthor is the data (name and email) used when a commit is made +type CommitAuthor struct { + Name string + Email string +} diff --git a/internal/git/git.go b/internal/git/git.go index 6c446855..8c232a8d 100755 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -4,6 +4,10 @@ import ( "bytes" "fmt" "net/url" + "time" + + "github.com/go-git/go-git/v5/plumbing/object" + "github.com/lindell/multi-gitter/internal/domain" git "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" @@ -17,6 +21,8 @@ type Git struct { Repo string // The "url" to the repo, any format can be used as long as it's pushable NewBranch string // The name of the new branch that new changes will be pushed to + CommitAuthor *domain.CommitAuthor // The author data is used when making commits, if the field is not set, the git configuration will be used + repo *git.Repository // The repository after the clone has been made } @@ -83,7 +89,18 @@ func (g *Git) Commit(commitMessage string) error { } oldHash := oldHead.Hash() - hash, err := w.Commit(commitMessage, &git.CommitOptions{}) + var author *object.Signature + if g.CommitAuthor != nil { + author = &object.Signature{ + Name: g.CommitAuthor.Name, + Email: g.CommitAuthor.Email, + When: time.Now(), + } + } + + hash, err := w.Commit(commitMessage, &git.CommitOptions{ + Author: author, + }) if err != nil { return err } diff --git a/internal/multigitter/run.go b/internal/multigitter/run.go index 1a0be005..841011a8 100755 --- a/internal/multigitter/run.go +++ b/internal/multigitter/run.go @@ -25,7 +25,7 @@ type VersionController interface { MergePullRequest(ctx context.Context, pr domain.PullRequest) error } -// Runner conains fields to be able to do the run +// Runner contains fields to be able to do the run type Runner struct { VersionController VersionController @@ -39,6 +39,7 @@ type Runner struct { Reviewers []string MaxReviewers int // If set to zero, all reviewers will be used DryRun bool + CommitAuthor *domain.CommitAuthor } // Run runs a script for multiple repositories and creates PRs with the changes made @@ -89,9 +90,10 @@ func (r Runner) runSingleRepo(ctx context.Context, repo domain.Repository) error defer os.RemoveAll(tmpDir) sourceController := &git.Git{ - Directory: tmpDir, - Repo: repo.URL(r.Token), - NewBranch: r.FeatureBranch, + Directory: tmpDir, + Repo: repo.URL(r.Token), + NewBranch: r.FeatureBranch, + CommitAuthor: r.CommitAuthor, } err = sourceController.Clone()