diff --git a/README.md b/README.md index 6a9df09..0c88e13 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,8 @@ APIs used to retrieve the upstream versions can have some limitations for unauth GitHub and GitLab APIs in particular use rate limiting, so requests made by `bumper` could fail after a few usages or when bumping a lot of packages. You can configure `bumper` to use your API keys to avoid those limits. +It's also possible to configure the value used as the commit author. + Configuration file is expected to be present at `$XDG_CONFIG_HOME/bumper/config.yaml` or `$HOME/.config/bumper/config.yaml`. The format is as follows: ```yaml @@ -88,6 +90,8 @@ check: apiKeys: gitlab.com: gitlab_com_api_key other.gitlab.instance: other_api_key +commit: + author: John Doe ``` **Warning**: All configuration fields are optional and the file isn't checked for additional keys! This means that `bumper` will not fail if you make a typo or other mistake. diff --git a/bumper/check_test.go b/bumper/check_test.go index 22fa4f1..a1e1668 100644 --- a/bumper/check_test.go +++ b/bumper/check_test.go @@ -12,9 +12,9 @@ import ( ) var ( - configProvider, _ = config.NewYAML(config.Source(strings.NewReader("{empty: {}, check: {providers: {version: 2.0.0}}}"))) - emptyConfig = configProvider.Get("empty") - configWithVersion = configProvider.Get("check") + checkConfigProvider, _ = config.NewYAML(config.Source(strings.NewReader("{empty: {}, check: {providers: {version: 2.0.0}}}"))) + emptyCheckConfig = checkConfigProvider.Get("empty") + checkConfigWithVersion = checkConfigProvider.Get("check") ) type fakeVersionProvider struct { @@ -37,7 +37,7 @@ func TestCheckAction_Success(t *testing.T) { verProvFactory := func(url string, providersConfig config.Value) upstream.VersionProvider { return &fakeVersionProvider{version: providersConfig.Get("version").String()} } - action := NewCheckAction(verProvFactory, configWithVersion) + action := NewCheckAction(verProvFactory, checkConfigWithVersion) pkg := pack.Package{ Srcinfo: &pack.Srcinfo{ URL: "foo", @@ -59,7 +59,7 @@ func TestCheckAction_Success(t *testing.T) { func TestCheckAction_Skip(t *testing.T) { verProvFactory := func(url string, providersConfig config.Value) upstream.VersionProvider { return nil } - action := NewCheckAction(verProvFactory, emptyConfig) + action := NewCheckAction(verProvFactory, emptyCheckConfig) pkg := pack.Package{ Srcinfo: &pack.Srcinfo{ URL: "foo", FullVersion: &pack.FullVersion{ @@ -77,7 +77,7 @@ func TestCheckAction_Skip(t *testing.T) { func TestCheckAction_FailNoProvider(t *testing.T) { verProvFactory := func(url string, providersConfig config.Value) upstream.VersionProvider { return nil } - action := NewCheckAction(verProvFactory, emptyConfig) + action := NewCheckAction(verProvFactory, emptyCheckConfig) pkg := pack.Package{Srcinfo: &pack.Srcinfo{URL: "foo"}} result := action.Execute(&pkg) @@ -92,7 +92,7 @@ func TestCheckAction_FailProviderFailed(t *testing.T) { verProvFactory := func(url string, providersConfig config.Value) upstream.VersionProvider { return &fakeVersionProvider{err: fmt.Errorf(expectedErr)} } - action := NewCheckAction(verProvFactory, emptyConfig) + action := NewCheckAction(verProvFactory, emptyCheckConfig) pkg := pack.Package{Srcinfo: &pack.Srcinfo{URL: "foo"}} result := action.Execute(&pkg) @@ -109,7 +109,7 @@ func TestCheckAction_FailChecksMultipleURLs(t *testing.T) { checkedURLs = append(checkedURLs, url) return &fakeVersionProvider{err: fmt.Errorf(expectedErr)} } - action := NewCheckAction(verProvFactory, emptyConfig) + action := NewCheckAction(verProvFactory, emptyCheckConfig) pkg := pack.Package{ Srcinfo: &pack.Srcinfo{ URL: "first.url", diff --git a/bumper/commit.go b/bumper/commit.go index 6870d97..08d009e 100644 --- a/bumper/commit.go +++ b/bumper/commit.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/bcyran/bumper/pack" + "go.uber.org/config" ) const expectedGitStatus = " M .SRCINFO\x00 M PKGBUILD\x00" @@ -27,10 +28,11 @@ func (result *commitActionResult) String() string { type CommitAction struct { commandRunner CommandRunner + commitConfig config.Value } -func NewCommitAction(commandRunner CommandRunner) *CommitAction { - return &CommitAction{commandRunner: commandRunner} +func NewCommitAction(commandRunner CommandRunner, commitConfig config.Value) *CommitAction { + return &CommitAction{commandRunner: commandRunner, commitConfig: commitConfig} } func (action *CommitAction) Execute(pkg *pack.Package) ActionResult { @@ -86,6 +88,14 @@ func (action *CommitAction) commit(pkg *pack.Package) error { } commitMessage := fmt.Sprintf("Bump version to %s", pkg.UpstreamVersion) - _, err = action.commandRunner(pkg.Path, "git", "commit", "--message", commitMessage) + commitArgs := []string{"commit", "--message", commitMessage} + + var commitAuthor string + action.commitConfig.Get("author").Populate(&commitAuthor) // nolint:errcheck + if commitAuthor != "" { + commitArgs = append(commitArgs, "--author", commitAuthor) + } + + _, err = action.commandRunner(pkg.Path, "git", commitArgs...) return err } diff --git a/bumper/commit_test.go b/bumper/commit_test.go index 4120e03..f1d914b 100644 --- a/bumper/commit_test.go +++ b/bumper/commit_test.go @@ -2,12 +2,20 @@ package bumper import ( "fmt" + "strings" "testing" "github.com/bcyran/bumper/internal/testutils" "github.com/bcyran/bumper/pack" "github.com/bcyran/bumper/upstream" "github.com/stretchr/testify/assert" + "go.uber.org/config" +) + +var ( + commitConfigProvider, _ = config.NewYAML(config.Source(strings.NewReader("{empty: {}, commit: {author: John Doe }}"))) + emptyCommitConfig = commitConfigProvider.Get("empty") + commitConfigWithAuthor = commitConfigProvider.Get("commit") ) func TestCommitAction_Success(t *testing.T) { @@ -27,7 +35,7 @@ func TestCommitAction_Success(t *testing.T) { fakeCommandRunner, commandRuns := testutils.MakeFakeCommandRunner(&commandRetvals) // execute the action with our mocked command runner - action := NewCommitAction(fakeCommandRunner) + action := NewCommitAction(fakeCommandRunner, emptyCommitConfig) result := action.Execute(pkg) // result assertions @@ -54,6 +62,38 @@ func TestCommitAction_Success(t *testing.T) { assert.Equal(t, expectedCommitCommand, (*commandRuns)[2]) } +func TestCommitAction_SuccessWithAuthor(t *testing.T) { + // our Package struct + pkg := &pack.Package{ + Path: "/foo/bar/baz", + UpstreamVersion: upstream.Version("1.2.3"), + IsOutdated: true, + } + + // mock return values for commands + commandRetvals := []testutils.CommandRunnerRetval{ + {Stdout: []byte(" M .SRCINFO\x00 M PKGBUILD\x00"), Err: nil}, // git status + {Stdout: []byte{}, Err: nil}, // git add + {Stdout: []byte{}, Err: nil}, // git commit + } + fakeCommandRunner, commandRuns := testutils.MakeFakeCommandRunner(&commandRetvals) + + // execute the action with our mocked command runner + action := NewCommitAction(fakeCommandRunner, commitConfigWithAuthor) + result := action.Execute(pkg) + + // result assertions + assert.Equal(t, ActionSuccessStatus, result.GetStatus()) + assert.Equal(t, "committed", result.String()) + + // expect valid git commit command + expectedCommitMessage := fmt.Sprintf("Bump version to %s", pkg.UpstreamVersion) + expectedCommitCommand := testutils.CommandRunnerParams{ + Cwd: pkg.Path, Command: "git", Args: []string{"commit", "--message", expectedCommitMessage, "--author", "John Doe "}, + } + assert.Equal(t, expectedCommitCommand, (*commandRuns)[2]) +} + func TestCommitAction_Skip(t *testing.T) { // our Package struct pkg := &pack.Package{ @@ -69,7 +109,7 @@ func TestCommitAction_Skip(t *testing.T) { fakeCommandRunner, _ := testutils.MakeFakeCommandRunner(&commandRetvals) // execute the action with our mocked command runner - action := NewCommitAction(fakeCommandRunner) + action := NewCommitAction(fakeCommandRunner, emptyCommitConfig) result := action.Execute(pkg) assert.Equal(t, ActionSkippedStatus, result.GetStatus()) @@ -91,7 +131,7 @@ func TestCommitAction_Fail(t *testing.T) { fakeCommandRunner, _ := testutils.MakeFakeCommandRunner(&commandRetvals) // execute the action with our mocked command runner - action := NewCommitAction(fakeCommandRunner) + action := NewCommitAction(fakeCommandRunner, emptyCommitConfig) result := action.Execute(pkg) assert.Equal(t, ActionFailedStatus, result.GetStatus()) diff --git a/cmd/bumper/bumper.go b/cmd/bumper/bumper.go index 935dca6..3ad45b5 100644 --- a/cmd/bumper/bumper.go +++ b/cmd/bumper/bumper.go @@ -123,7 +123,7 @@ func createActions(doActions DoActions, bumperConfig config.Provider) []bumper.A } if doActions.commit { - actions = append(actions, bumper.NewCommitAction(bumper.ExecCommand)) + actions = append(actions, bumper.NewCommitAction(bumper.ExecCommand, bumperConfig.Get("commit"))) } else { return actions }