Skip to content

Commit

Permalink
feat: Allow providing commit author via config.yaml (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcyran committed Mar 9, 2023
1 parent e6557f9 commit fd9ccd9
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 15 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -88,6 +90,8 @@ check:
apiKeys:
gitlab.com: gitlab_com_api_key
other.gitlab.instance: other_api_key
commit:
author: John Doe <[email protected]>
```
**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.
Expand Down
16 changes: 8 additions & 8 deletions bumper/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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",
Expand All @@ -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{
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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",
Expand Down
16 changes: 13 additions & 3 deletions bumper/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/bcyran/bumper/pack"
"go.uber.org/config"
)

const expectedGitStatus = " M .SRCINFO\x00 M PKGBUILD\x00"
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
46 changes: 43 additions & 3 deletions bumper/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>}}")))
emptyCommitConfig = commitConfigProvider.Get("empty")
commitConfigWithAuthor = commitConfigProvider.Get("commit")
)

func TestCommitAction_Success(t *testing.T) {
Expand All @@ -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
Expand All @@ -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 <[email protected]>"},
}
assert.Equal(t, expectedCommitCommand, (*commandRuns)[2])
}

func TestCommitAction_Skip(t *testing.T) {
// our Package struct
pkg := &pack.Package{
Expand All @@ -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())
Expand All @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion cmd/bumper/bumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit fd9ccd9

Please sign in to comment.