Skip to content

Commit 325886c

Browse files
committed
Only schedule an auto-merge when PR state is blocked
When passing `--auto` flag, only schedule an auto-merge if the `mergeStateStatus` field is "BLOCKED". This ensures that a PR will always be merged when passing `--auto` even if it doesn't have required checks or if checks have already passed.
1 parent c49c7f4 commit 325886c

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

pkg/cmd/pr/merge/http.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type mergePayload struct {
2424
pullRequestID string
2525
method PullRequestMergeMethod
2626
auto bool
27+
mergeStateStatus string
2728
commitSubject string
2829
setCommitSubject bool
2930
commitBody string
@@ -67,7 +68,7 @@ func mergePullRequest(client *http.Client, payload mergePayload) error {
6768

6869
gql := graphql.NewClient(ghinstance.GraphQLEndpoint(payload.repo.RepoHost()), client)
6970

70-
if payload.auto {
71+
if payload.auto && payload.mergeStateStatus == "BLOCKED" {
7172
var mutation struct {
7273
EnablePullRequestAutoMerge struct {
7374
ClientMutationId string

pkg/cmd/pr/merge/merge.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func mergeRun(opts *MergeOptions) error {
156156

157157
findOptions := shared.FindOptions{
158158
Selector: opts.SelectorArg,
159-
Fields: []string{"id", "number", "state", "title", "lastCommit", "mergeable", "headRepositoryOwner", "headRefName"},
159+
Fields: []string{"id", "number", "state", "title", "lastCommit", "mergeable", "mergeStateStatus", "headRepositoryOwner", "headRefName"},
160160
}
161161
pr, baseRepo, err := opts.Finder.Find(findOptions)
162162
if err != nil {
@@ -202,12 +202,13 @@ func mergeRun(opts *MergeOptions) error {
202202
isPRAlreadyMerged := pr.State == "MERGED"
203203
if !isPRAlreadyMerged {
204204
payload := mergePayload{
205-
repo: baseRepo,
206-
pullRequestID: pr.ID,
207-
method: opts.MergeMethod,
208-
auto: opts.AutoMergeEnable,
209-
commitBody: opts.Body,
210-
setCommitBody: opts.BodySet,
205+
repo: baseRepo,
206+
pullRequestID: pr.ID,
207+
mergeStateStatus: pr.MergeStateStatus,
208+
method: opts.MergeMethod,
209+
auto: opts.AutoMergeEnable,
210+
commitBody: opts.Body,
211+
setCommitBody: opts.BodySet,
211212
}
212213

213214
if opts.InteractiveMode {
@@ -262,7 +263,7 @@ func mergeRun(opts *MergeOptions) error {
262263
}
263264

264265
if isTerminal {
265-
if payload.auto {
266+
if payload.auto && payload.mergeStateStatus == "BLOCKED" {
266267
method := ""
267268
switch payload.method {
268269
case PullRequestMergeMethodRebase:

pkg/cmd/pr/merge/merge_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ func Test_nonDivergingPullRequest(t *testing.T) {
465465
stubCommit(pr, "COMMITSHA1")
466466

467467
prFinder := shared.RunCommandFinder("", pr, baseRepo("OWNER", "REPO", "master"))
468-
prFinder.ExpectFields([]string{"id", "number", "state", "title", "lastCommit", "mergeable", "headRepositoryOwner", "headRefName"})
468+
prFinder.ExpectFields([]string{"id", "number", "state", "title", "lastCommit", "mergeable", "mergeStateStatus", "headRepositoryOwner", "headRefName"})
469469

470470
http.Register(
471471
httpmock.GraphQL(`mutation PullRequestMerge\b`),
@@ -503,7 +503,7 @@ func Test_divergingPullRequestWarning(t *testing.T) {
503503
stubCommit(pr, "COMMITSHA1")
504504

505505
prFinder := shared.RunCommandFinder("", pr, baseRepo("OWNER", "REPO", "master"))
506-
prFinder.ExpectFields([]string{"id", "number", "state", "title", "lastCommit", "mergeable", "headRepositoryOwner", "headRefName"})
506+
prFinder.ExpectFields([]string{"id", "number", "state", "title", "lastCommit", "mergeable", "mergeStateStatus", "headRepositoryOwner", "headRefName"})
507507

508508
http.Register(
509509
httpmock.GraphQL(`mutation PullRequestMerge\b`),
@@ -947,7 +947,7 @@ func TestMergeRun_autoMerge(t *testing.T) {
947947
MergeMethod: PullRequestMergeMethodSquash,
948948
Finder: shared.NewMockFinder(
949949
"https://github.com/OWNER/REPO/pull/123",
950-
&api.PullRequest{ID: "THE-ID", Number: 123},
950+
&api.PullRequest{ID: "THE-ID", Number: 123, MergeStateStatus: "BLOCKED"},
951951
ghrepo.New("OWNER", "REPO"),
952952
),
953953
})

0 commit comments

Comments
 (0)