Skip to content

Commit

Permalink
config: add configuration for PR merge method
Browse files Browse the repository at this point in the history
The GitHub pull request API support squash, rebase, and merge. As
explored in issue #195, with signed commits required on the target
branch, it's not possible to use the rebase due to a known GitHub
implementation issue.

This change adds a new per-repo string configuration attribute,
`mergeMethod`, `"rebase"` by default. The current behaviour is captured by
this default value.
  • Loading branch information
wwade authored and Eitan Joffe committed Feb 20, 2022
1 parent 8b4f671 commit 4fa1d71
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions .spr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ requireChecks: true
requireApproval: true
githubRemote: origin
githubBranch: master
mergeMethod: rebase
21 changes: 21 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/ejoffe/rake"
"github.com/ejoffe/spr/git"
"github.com/shurcooL/githubv4"
)

type Config struct {
Expand All @@ -28,6 +29,7 @@ type RepoConfig struct {

GitHubRemote string `default:"origin" yaml:"githubRemote"`
GitHubBranch string `default:"master" yaml:"githubBranch"`
MergeMethod string `default:"rebase" yaml:"mergeMethod"`
}

type UserConfig struct {
Expand Down Expand Up @@ -121,6 +123,25 @@ func GitHubRemoteSource(config *Config, gitcmd git.GitInterface) *remoteSource {
}
}

func (c Config) MergeMethod() (githubv4.PullRequestMergeMethod, error) {
var mergeMethod githubv4.PullRequestMergeMethod
var err error
switch strings.ToLower(c.Repo.MergeMethod) {
case "merge":
mergeMethod = githubv4.PullRequestMergeMethodMerge
case "squash":
mergeMethod = githubv4.PullRequestMergeMethodSquash
case "rebase", "":
mergeMethod = githubv4.PullRequestMergeMethodRebase
default:
err = fmt.Errorf(
`unknown merge method %q, choose from "merge", "squash", or "rebase"`,
c.Repo.MergeMethod,
)
}
return mergeMethod, err
}

type remoteSource struct {
gitcmd git.GitInterface
config *Config
Expand Down
44 changes: 44 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/ejoffe/spr/git/mockgit"
"github.com/shurcooL/githubv4"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -72,6 +73,7 @@ func TestDefaultConfig(t *testing.T) {
RequireApproval: true,
GitHubRemote: "origin",
GitHubBranch: "master",
MergeMethod: "rebase",
},
User: &UserConfig{
ShowPRLink: true,
Expand Down Expand Up @@ -100,6 +102,7 @@ func TestGitHubRemoteSource(t *testing.T) {
RequireApproval: false,
GitHubRemote: "",
GitHubBranch: "",
MergeMethod: "",
},
User: &UserConfig{
ShowPRLink: false,
Expand All @@ -119,3 +122,44 @@ func TestGitHubRemoteSource(t *testing.T) {
source.Load(nil)
assert.Equal(t, expect, actual)
}

func TestMergeMethodHelper(t *testing.T) {
for _, tc := range []struct {
configValue string
expected githubv4.PullRequestMergeMethod
}{
{
configValue: "rebase",
expected: githubv4.PullRequestMergeMethodRebase,
},
{
configValue: "",
expected: githubv4.PullRequestMergeMethodRebase,
},
{
configValue: "Merge",
expected: githubv4.PullRequestMergeMethodMerge,
},
{
configValue: "SQUASH",
expected: githubv4.PullRequestMergeMethodSquash,
},
} {
tcName := tc.configValue
if tcName == "" {
tcName = "<EMPTY>"
}
t.Run(tcName, func(t *testing.T) {
config := &Config{Repo: &RepoConfig{MergeMethod: tc.configValue}}
actual, err := config.MergeMethod()
assert.NoError(t, err)
assert.Equal(t, tc.expected, actual)
})
}
t.Run("invalid", func(t *testing.T) {
config := &Config{Repo: &RepoConfig{MergeMethod: "magic"}}
actual, err := config.MergeMethod()
assert.Error(t, err)
assert.Empty(t, actual)
})
}
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ MERGED #60 Feature 3

To merge only part of the stack use the **--upto** flag with the top pull request number in the stack that you would like to merge.

By default merges are done using the rebase merge method, this can be changed using the mergeMethod configuration.

```shell
> git spr merge --upto 59
MERGED #58 Feature 1
Expand All @@ -160,6 +162,7 @@ User specific configuration is saved to .spr.yml in the user home directory.
| githubRemote | str | origin | github remote name to use |
| githubBranch | str | master | github branch for pull request target |
| githubHost | str | github.com | github host, can be updated for github enterprise usecase |
| mergeMethod | str | rebase | merge method, valid values: [rebase, squash, merge] |

| User Config | Type | Default | Description |
| ------------------- | ---- | ------- | ----------------------------------------------------------------- |
Expand Down

0 comments on commit 4fa1d71

Please sign in to comment.