Skip to content

Commit

Permalink
Replace merge --upto flag with more intuitive --count flag
Browse files Browse the repository at this point in the history
fixes: #233, #231

commit-id:ffbbcf0e
  • Loading branch information
ejoffe authored and Eitan Joffe committed Jun 4, 2022
1 parent 5a9c693 commit d454c59
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 34 deletions.
13 changes: 7 additions & 6 deletions cmd/spr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ VERSION: {{.Version}}
Usage: "Merge all mergeable pull requests",
Action: func(c *cli.Context) error {

if c.IsSet("upto") {
upto := c.Int("upto")
stackedpr.MergePullRequests(ctx, &upto)
if c.IsSet("count") {
count := c.Uint("count")
stackedpr.MergePullRequests(ctx, &count)
} else {
stackedpr.MergePullRequests(ctx, nil)
}
Expand All @@ -164,9 +164,10 @@ VERSION: {{.Version}}
},
Flags: []cli.Flag{
detailFlag,
&cli.IntFlag{
Name: "upto",
Usage: "Only merge prs upto and including the provided pr number",
&cli.UintFlag{
Name: "count",
Aliases: []string{"c"},
Usage: "Merge a specified number of pull requests from the bottom of the stack",
},
},
},
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ require (
github.com/google/uuid v1.2.0
github.com/jessevdk/go-flags v1.5.0
github.com/kr/pretty v0.2.0 // indirect
github.com/mvdan/xurls v1.1.0 // indirect
github.com/rs/zerolog v1.22.0
github.com/shurcooL/githubv4 v0.0.0-20201206200315-234843c633fa
github.com/shurcooL/graphql v0.0.0-20200928012149-18c5c3165e3a // indirect
Expand All @@ -19,5 +18,4 @@ require (
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c
mvdan.cc/xurls v1.1.0
)
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfn
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mvdan/xurls v1.1.0 h1:OpuDelGQ1R1ueQ6sSryzi6P+1RtBpfQHM8fJwlE45ww=
github.com/mvdan/xurls v1.1.0/go.mod h1:tQlNn3BED8bE/15hnSL2HLkDeLWpNPAwtw7wkEq44oU=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down Expand Up @@ -407,8 +405,6 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
mvdan.cc/xurls v1.1.0 h1:kj0j2lonKseISJCiq1Tfk+iTv65dDGCl0rTbanXJGGc=
mvdan.cc/xurls v1.1.0/go.mod h1:TNWuhvo+IqbUCmtUIb/3LJSQdrzel8loVpgFm0HikbI=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,18 @@ MERGED #60 Feature 3
[✅❌✅✅] 61: Feature 4
```

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.
To merge only part of the stack use the **--count** flag with the number of pull requests in the stack that you would like to merge. Pull requests will be merged from the bottom of the stack upwards.

```shell
> git spr merge --upto 59
> git spr merge --count 2
MERGED #58 Feature 1
MERGED #59 Feature 2
[✅❌✅✅] 61: Feature 4
[✅✅✅✅] 60: Feature 3
```

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

Configuration
-------------
When the script is run for the first time two config files are created.
Expand Down
4 changes: 2 additions & 2 deletions spr/spr.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func (sd *stackediff) UpdatePullRequests(ctx context.Context, reviewers []string
// pull request. This one merge in effect merges all the commits in the stack.
// We than close all the pull requests which are below the merged request, as
// their commits have already been merged.
func (sd *stackediff) MergePullRequests(ctx context.Context, upto *int) {
func (sd *stackediff) MergePullRequests(ctx context.Context, count *uint) {
sd.profiletimer.Step("MergePullRequests::Start")
githubInfo := sd.github.GetInfo(ctx, sd.gitcmd)
sd.profiletimer.Step("MergePullRequests::getGitHubInfo")
Expand All @@ -233,7 +233,7 @@ func (sd *stackediff) MergePullRequests(ctx context.Context, upto *int) {
prIndex--
break
}
if upto != nil && pr.Number == *upto {
if count != nil && (prIndex+1) == int(*count) {
break
}
}
Expand Down
107 changes: 91 additions & 16 deletions spr/spr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ func TestSPRBasicFlowFourCommits(t *testing.T) {
Subject: "test commit 4",
}

// 'git spr -s' :: StatusPullRequest
// 'git spr status' :: StatusPullRequest
githubmock.ExpectGetInfo()
s.StatusPullRequests(ctx)
assert.Equal("pull request stack is empty\n", output.String())
output.Reset()

// 'git spr -u' :: UpdatePullRequest :: commits=[c1]
// 'git spr update' :: UpdatePullRequest :: commits=[c1]
githubmock.ExpectGetInfo()
gitmock.ExpectFetch()
gitmock.ExpectLogAndRespond([]*git.Commit{&c1})
Expand All @@ -88,7 +88,7 @@ func TestSPRBasicFlowFourCommits(t *testing.T) {
assert.Equal("[✔✔✔✔] 1 : test commit 1\n", output.String())
output.Reset()

// 'git spr -u' :: UpdatePullRequest :: commits=[c1, c2]
// 'git spr update' :: UpdatePullRequest :: commits=[c1, c2]
githubmock.ExpectGetInfo()
gitmock.ExpectFetch()
gitmock.ExpectLogAndRespond([]*git.Commit{&c2, &c1})
Expand All @@ -107,7 +107,7 @@ func TestSPRBasicFlowFourCommits(t *testing.T) {
assert.Equal("[✔✔✔✔] 1 : test commit 1", lines[2])
output.Reset()

// 'git spr -u' :: UpdatePullRequest :: commits=[c1, c2, c3, c4]
// 'git spr update' :: UpdatePullRequest :: commits=[c1, c2, c3, c4]
githubmock.ExpectGetInfo()
gitmock.ExpectFetch()
gitmock.ExpectLogAndRespond([]*git.Commit{&c4, &c3, &c2, &c1})
Expand Down Expand Up @@ -140,7 +140,7 @@ func TestSPRBasicFlowFourCommits(t *testing.T) {
}, lines[:6])
output.Reset()

// 'git spr -m' :: MergePullRequest :: commits=[a1, a2, a3, a4]
// 'git spr merge' :: MergePullRequest :: commits=[a1, a2, a3, a4]
githubmock.ExpectGetInfo()
githubmock.ExpectUpdatePullRequest(c4, nil)
githubmock.ExpectMergePullRequest(c4, githubv4.PullRequestMergeMethodRebase)
Expand All @@ -160,6 +160,77 @@ func TestSPRBasicFlowFourCommits(t *testing.T) {
output.Reset()
}

func TestSPRMergeCount(t *testing.T) {
s, gitmock, githubmock, _, output := makeTestObjects(t)
assert := require.New(t)
ctx := context.Background()

c1 := git.Commit{
CommitID: "00000001",
CommitHash: "c100000000000000000000000000000000000000",
Subject: "test commit 1",
}
c2 := git.Commit{
CommitID: "00000002",
CommitHash: "c200000000000000000000000000000000000000",
Subject: "test commit 2",
}
c3 := git.Commit{
CommitID: "00000003",
CommitHash: "c300000000000000000000000000000000000000",
Subject: "test commit 3",
}
c4 := git.Commit{
CommitID: "00000004",
CommitHash: "c400000000000000000000000000000000000000",
Subject: "test commit 4",
}

// 'git spr update' :: UpdatePullRequest :: commits=[c1, c2, c3, c4]
githubmock.ExpectGetInfo()
gitmock.ExpectFetch()
gitmock.ExpectLogAndRespond([]*git.Commit{&c4, &c3, &c2, &c1})
gitmock.ExpectPushCommits([]*git.Commit{&c1, &c2, &c3, &c4})
// For the first "create" call we should call GetAssignableUsers
githubmock.ExpectCreatePullRequest(c1, nil)
githubmock.ExpectGetAssignableUsers()
githubmock.ExpectAddReviewers([]string{mockclient.NobodyUserID})
githubmock.ExpectCreatePullRequest(c2, &c1)
githubmock.ExpectAddReviewers([]string{mockclient.NobodyUserID})
githubmock.ExpectCreatePullRequest(c3, &c2)
githubmock.ExpectAddReviewers([]string{mockclient.NobodyUserID})
githubmock.ExpectCreatePullRequest(c4, &c3)
githubmock.ExpectAddReviewers([]string{mockclient.NobodyUserID})
githubmock.ExpectUpdatePullRequest(c1, nil)
githubmock.ExpectUpdatePullRequest(c2, &c1)
githubmock.ExpectUpdatePullRequest(c3, &c2)
githubmock.ExpectUpdatePullRequest(c4, &c3)
githubmock.ExpectGetInfo()
s.UpdatePullRequests(ctx, []string{mockclient.NobodyLogin})
lines := strings.Split(output.String(), "\n")
fmt.Printf("OUT: %s\n", output.String())
assert.Equal([]string{
"[✔✔✔✔] 1 : test commit 4",
"[✔✔✔✔] 1 : test commit 3",
"[✔✔✔✔] 1 : test commit 2",
"[✔✔✔✔] 1 : test commit 1",
}, lines[:4])
output.Reset()

// 'git spr merge --count 2' :: MergePullRequest :: commits=[a1, a2, a3, a4]
githubmock.ExpectGetInfo()
githubmock.ExpectUpdatePullRequest(c2, nil)
githubmock.ExpectMergePullRequest(c2, githubv4.PullRequestMergeMethodRebase)
githubmock.ExpectCommentPullRequest(c1)
githubmock.ExpectClosePullRequest(c1)
s.MergePullRequests(ctx, uintptr(2))
lines = strings.Split(output.String(), "\n")
assert.Equal("MERGED 1 : test commit 1", lines[0])
assert.Equal("MERGED 1 : test commit 2", lines[1])
fmt.Printf("OUT: %s\n", output.String())
output.Reset()
}

func TestSPRAmendCommit(t *testing.T) {
s, gitmock, githubmock, _, output := makeTestObjects(t)
assert := require.New(t)
Expand All @@ -176,13 +247,13 @@ func TestSPRAmendCommit(t *testing.T) {
Subject: "test commit 2",
}

// 'git spr -s' :: StatusPullRequest
// 'git spr state' :: StatusPullRequest
githubmock.ExpectGetInfo()
s.StatusPullRequests(ctx)
assert.Equal("pull request stack is empty\n", output.String())
output.Reset()

// 'git spr -u' :: UpdatePullRequest :: commits=[c1, c2]
// 'git spr update' :: UpdatePullRequest :: commits=[c1, c2]
githubmock.ExpectGetInfo()
gitmock.ExpectFetch()
gitmock.ExpectLogAndRespond([]*git.Commit{&c2, &c1})
Expand All @@ -201,7 +272,7 @@ func TestSPRAmendCommit(t *testing.T) {

// amend commit c2
c2.CommitHash = "c201000000000000000000000000000000000000"
// 'git spr -u' :: UpdatePullRequest :: commits=[c1, c2]
// 'git spr update' :: UpdatePullRequest :: commits=[c1, c2]
githubmock.ExpectGetInfo()
gitmock.ExpectFetch()
gitmock.ExpectLogAndRespond([]*git.Commit{&c2, &c1})
Expand All @@ -219,7 +290,7 @@ func TestSPRAmendCommit(t *testing.T) {
// amend commit c1
c1.CommitHash = "c101000000000000000000000000000000000000"
c2.CommitHash = "c202000000000000000000000000000000000000"
// 'git spr -u' :: UpdatePullRequest :: commits=[c1, c2]
// 'git spr update' :: UpdatePullRequest :: commits=[c1, c2]
githubmock.ExpectGetInfo()
gitmock.ExpectFetch()
gitmock.ExpectLogAndRespond([]*git.Commit{&c2, &c1})
Expand All @@ -234,7 +305,7 @@ func TestSPRAmendCommit(t *testing.T) {
assert.Equal("[✔✔✔✔] 1 : test commit 1", lines[1])
output.Reset()

// 'git spr -m' :: MergePullRequest :: commits=[a1, a2]
// 'git spr merge' :: MergePullRequest :: commits=[a1, a2]
githubmock.ExpectGetInfo()
githubmock.ExpectUpdatePullRequest(c2, nil)
githubmock.ExpectMergePullRequest(c2, githubv4.PullRequestMergeMethodRebase)
Expand Down Expand Up @@ -276,13 +347,13 @@ func TestSPRReorderCommit(t *testing.T) {
Subject: "test commit 4",
}

// 'git spr -s' :: StatusPullRequest
// 'git spr status' :: StatusPullRequest
githubmock.ExpectGetInfo()
s.StatusPullRequests(ctx)
assert.Equal("pull request stack is empty\n", output.String())
output.Reset()

// 'git spr -u' :: UpdatePullRequest :: commits=[c1, c2, c3, c4]
// 'git spr update' :: UpdatePullRequest :: commits=[c1, c2, c3, c4]
githubmock.ExpectGetInfo()
gitmock.ExpectFetch()
gitmock.ExpectLogAndRespond([]*git.Commit{&c4, &c3, &c2, &c1})
Expand All @@ -305,7 +376,7 @@ func TestSPRReorderCommit(t *testing.T) {
assert.Equal("[✔✔✔✔] 1 : test commit 1", lines[3])
output.Reset()

// 'git spr -u' :: UpdatePullRequest :: commits=[c2, c4, c1, c3]
// 'git spr update' :: UpdatePullRequest :: commits=[c2, c4, c1, c3]
githubmock.ExpectGetInfo()
gitmock.ExpectFetch()
gitmock.ExpectLogAndRespond([]*git.Commit{&c3, &c1, &c4, &c2})
Expand Down Expand Up @@ -363,13 +434,13 @@ func TestSPRDeleteCommit(t *testing.T) {
Subject: "test commit 4",
}

// 'git spr -s' :: StatusPullRequest
// 'git spr status' :: StatusPullRequest
githubmock.ExpectGetInfo()
s.StatusPullRequests(ctx)
assert.Equal("pull request stack is empty\n", output.String())
output.Reset()

// 'git spr -u' :: UpdatePullRequest :: commits=[c1, c2, c3, c4]
// 'git spr update' :: UpdatePullRequest :: commits=[c1, c2, c3, c4]
githubmock.ExpectGetInfo()
gitmock.ExpectFetch()
gitmock.ExpectLogAndRespond([]*git.Commit{&c4, &c3, &c2, &c1})
Expand All @@ -393,7 +464,7 @@ func TestSPRDeleteCommit(t *testing.T) {
assert.Equal("[✔✔✔✔] 1 : test commit 1", lines[3])
output.Reset()

// 'git spr -u' :: UpdatePullRequest :: commits=[c2, c4, c1, c3]
// 'git spr update' :: UpdatePullRequest :: commits=[c2, c4, c1, c3]
githubmock.ExpectGetInfo()
gitmock.ExpectFetch()
gitmock.ExpectLogAndRespond([]*git.Commit{&c4, &c1})
Expand Down Expand Up @@ -610,3 +681,7 @@ Date: Wed May 21 19:53:12 1980 -0700
}
}
}

func uintptr(a uint) *uint {
return &a
}

0 comments on commit d454c59

Please sign in to comment.