fix(ci): handle merge race in js-autofix poll loop (#65231)

When the bot PR auto-merges, main moves — which the poll loop detects
as 'main moved' and tries to close the PR. But the PR is already
merged, so gh pr close fails with an error.

Fix: when main moves, re-check PR state before closing. If it's
already MERGED, exit cleanly. Also make gh pr close non-fatal (|| true)
as a belt-and-suspenders guard against the same race on the other
close paths.
This commit is contained in:
ethernet 2026-07-15 17:51:34 -04:00 committed by GitHub
parent 75f45a0692
commit 0c1adb4877
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -207,12 +207,17 @@ jobs:
exit 0
fi
# If main moved, close the PR — the next workflow run re-applies
# on the current state.
# If main moved, the PR may have already merged (which moves
# main) or another commit landed. Re-check state first.
CURRENT_SHA=$(gh api "repos/${{ github.repository }}/branches/main" --jq '.commit.sha')
if [ "$CURRENT_SHA" != "$START_SHA" ]; then
STATE=$(gh pr view "$PR_NUM" --json state --jq '.state')
if [ "$STATE" = "MERGED" ]; then
echo "PR #$PR_NUM merged (main moved to $CURRENT_SHA)."
exit 0
fi
echo "Main moved ($START_SHA → $CURRENT_SHA). Closing stale PR."
gh pr close "$PR_NUM" --delete-branch
gh pr close "$PR_NUM" --delete-branch || true
exit 0
fi