[BUGFIX] Don't automerge a closed PR #166
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When
automerge
is triggered, it first asks a bunch of questions to determine if it should even bother trying to merge. If those checks pass, it starts polling the Github API to determine if the PR is "mergeable".After the initial run through of what I'll call the "skip factors", it never looks at them again. This is undesirable and problematic for a number of reasons, but particularly for long-running configurations (i.e. when
automerge
is configured to wait several minutes or even hours before giving up). In particular:automerge
job initially starts, it won't know, and may merge the PR when it shouldn't.automerge
job initially starts, it won't know, and may merge the PR after it has been closed (a totally valid API action if the head of the branch is "mergeable," just not an available action through the Github UI).We at ProdPerfect have observed several instances where the
automerge
action is triggered when a pull request is created, but the PR is not immediately mergeable and so it begins its polling; only to miss the addition of labels that should block the merge, or even worse, to miss the closure of the PR itself. There was one incident where the PR was merged a full 30min after it was closed! (Some of our regression test suites take a long time, and we have very longautomerge
timeouts configured on those projects.)This PR addresses the problem by adding the "should automerge be skipped?" check to the "is this PR ready to be automerged?" polling mechanism. We have been leveraging this modification for nearly a month at this point, and this simple fix resolves the observed issues without any noticeable latency overhead.
How to test
Testing this necessarily involves consuming it. While first iterating on this enhancement, I created a test repository equipped with a CI integration that simply
sleep
'd for 60 seconds, and a branch protection rule which required this CI job to pass before allowing commits to be merged intomain
. It also had a Github workflow configured to use this fork of theautomerge
action, with config similar to what our team uses in production:(For testing purposes, the retries and "retry sleep" config were modified.)
Test scenario:
automerge
canceled whenneeds review
label is attached to PRTo confirm that this PR addresses this scenario, first create a commit that points the
automerge
workflow to the officialautomerge
Github action:pascalgn/automerge-action@v0.14.2
Then open a PR, and wait about 20 seconds for the
automerge
action to be triggered and start polling the PR for mergeability. Add theneeds review
label after confirmingautomerge
has started running: you should expect it to have no effect, and that the PR is automerged when the CI build passes.That's the baseline. Now, point the workflow at this fork of the action, and do the same thing. This time, you should expect to see the
automerge
check finish successfully and gracefully soon after you add the label, without actually merging your PR. Now, remove the label; theautomerge
action should trigger again, and this time it should merge your PR as soon as the CI job finishes successfully.Test scenario:
automerge
canceled on PR closureTo confirm that this PR addresses this scenario, first create a commit that points the
automerge
workflow to the officialautomerge
Github action:pascalgn/automerge-action@v0.14.2
Then open a PR, and wait about 20 seconds for the
automerge
action to be triggered and start polling the PR for mergeability. At this point, close the PR, ensuring you do so before the CI build finishes, but after theautomerge
job has started. Remain on the PR page: you should observe the PR go from "closed" to "merged" within a few seconds, when CI finishes and the still-runningautomerge
job does its thing.That's the baseline. Now, point the workflow at this fork of the action, and do the same thing. This time, you should expect to see the
automerge
check finish successfully and gracefully soon after you close the PR, without actually merging your PR; you can view this in the "Checks" tab of the PR (closing the PR takes away the convenient link on the main PR page).