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.
This commit is contained in:
Cristian Dominguez 2021-05-24 18:14:10 -03:00
parent c49c7f4de2
commit 325886c0e4
3 changed files with 14 additions and 12 deletions

View file

@ -24,6 +24,7 @@ type mergePayload struct {
pullRequestID string
method PullRequestMergeMethod
auto bool
mergeStateStatus string
commitSubject string
setCommitSubject bool
commitBody string
@ -67,7 +68,7 @@ func mergePullRequest(client *http.Client, payload mergePayload) error {
gql := graphql.NewClient(ghinstance.GraphQLEndpoint(payload.repo.RepoHost()), client)
if payload.auto {
if payload.auto && payload.mergeStateStatus == "BLOCKED" {
var mutation struct {
EnablePullRequestAutoMerge struct {
ClientMutationId string

View file

@ -156,7 +156,7 @@ func mergeRun(opts *MergeOptions) error {
findOptions := shared.FindOptions{
Selector: opts.SelectorArg,
Fields: []string{"id", "number", "state", "title", "lastCommit", "mergeable", "headRepositoryOwner", "headRefName"},
Fields: []string{"id", "number", "state", "title", "lastCommit", "mergeable", "mergeStateStatus", "headRepositoryOwner", "headRefName"},
}
pr, baseRepo, err := opts.Finder.Find(findOptions)
if err != nil {
@ -202,12 +202,13 @@ func mergeRun(opts *MergeOptions) error {
isPRAlreadyMerged := pr.State == "MERGED"
if !isPRAlreadyMerged {
payload := mergePayload{
repo: baseRepo,
pullRequestID: pr.ID,
method: opts.MergeMethod,
auto: opts.AutoMergeEnable,
commitBody: opts.Body,
setCommitBody: opts.BodySet,
repo: baseRepo,
pullRequestID: pr.ID,
mergeStateStatus: pr.MergeStateStatus,
method: opts.MergeMethod,
auto: opts.AutoMergeEnable,
commitBody: opts.Body,
setCommitBody: opts.BodySet,
}
if opts.InteractiveMode {
@ -262,7 +263,7 @@ func mergeRun(opts *MergeOptions) error {
}
if isTerminal {
if payload.auto {
if payload.auto && payload.mergeStateStatus == "BLOCKED" {
method := ""
switch payload.method {
case PullRequestMergeMethodRebase:

View file

@ -465,7 +465,7 @@ func Test_nonDivergingPullRequest(t *testing.T) {
stubCommit(pr, "COMMITSHA1")
prFinder := shared.RunCommandFinder("", pr, baseRepo("OWNER", "REPO", "master"))
prFinder.ExpectFields([]string{"id", "number", "state", "title", "lastCommit", "mergeable", "headRepositoryOwner", "headRefName"})
prFinder.ExpectFields([]string{"id", "number", "state", "title", "lastCommit", "mergeable", "mergeStateStatus", "headRepositoryOwner", "headRefName"})
http.Register(
httpmock.GraphQL(`mutation PullRequestMerge\b`),
@ -503,7 +503,7 @@ func Test_divergingPullRequestWarning(t *testing.T) {
stubCommit(pr, "COMMITSHA1")
prFinder := shared.RunCommandFinder("", pr, baseRepo("OWNER", "REPO", "master"))
prFinder.ExpectFields([]string{"id", "number", "state", "title", "lastCommit", "mergeable", "headRepositoryOwner", "headRefName"})
prFinder.ExpectFields([]string{"id", "number", "state", "title", "lastCommit", "mergeable", "mergeStateStatus", "headRepositoryOwner", "headRefName"})
http.Register(
httpmock.GraphQL(`mutation PullRequestMerge\b`),
@ -947,7 +947,7 @@ func TestMergeRun_autoMerge(t *testing.T) {
MergeMethod: PullRequestMergeMethodSquash,
Finder: shared.NewMockFinder(
"https://github.com/OWNER/REPO/pull/123",
&api.PullRequest{ID: "THE-ID", Number: 123},
&api.PullRequest{ID: "THE-ID", Number: 123, MergeStateStatus: "BLOCKED"},
ghrepo.New("OWNER", "REPO"),
),
})