Skip to content

Commit 5e8097f

Browse files
ci: verify all PR checks before triggering auto-merge
Fixes #6550. The `auto-merge` job in `pr-quality-check.yaml` relies on native `needs` gates, which only evaluate jobs inside the same workflow file. Consequently, failures in external workflows (such as `tests.yaml`) were not visible to this job, allowing PRs to auto-merge despite failing tests. This change adds a pre-check step using `gh pr checks` that: 1. Verifies that the evaluated HEAD commit matches the current commit. Intended to address race conditions that may occur due to new pushes. 2. Filters out the active workflow run using `github.run_id`. 3. Verifies all external PR checks are in a passing state (SUCCESS, SKIPPED, or NEUTRAL).
1 parent f899f17 commit 5e8097f

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

.github/workflows/pr-quality-check.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,45 @@ jobs:
136136
permissions:
137137
contents: write
138138
pull-requests: write
139+
checks: read
139140
steps:
141+
- name: Verify all PR workflow checks passed
142+
env:
143+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
144+
EXPECTED_SHA: ${{ github.event.pull_request.head.sha }}
145+
run: |
146+
declare -r gh_pr_number='${{ github.event.pull_request.number }}'
147+
declare -r gh_repository='${{ github.repository }}'
148+
declare -r gh_current_run_id='${{ github.run_id }}'
149+
150+
# 1. Fetch current PR HEAD SHA to ensure no new commit was pushed while this ran
151+
CURRENT_SHA=$(gh pr view "${gh_pr_number}" \
152+
--repo "${gh_repository}" \
153+
--json headRefOid --jq '.headRefOid')
154+
155+
if [ "${CURRENT_SHA}" != "${EXPECTED_SHA}" ]; then
156+
echo "A new commit (${CURRENT_SHA}) was pushed after this run started (${EXPECTED_SHA}). Aborting auto-merge."
157+
exit 1
158+
fi
159+
160+
# 2. Allowlist filter: select any check whose state is NOT in [SUCCESS, SKIPPED, NEUTRAL],
161+
# filtering out the current workflow run by checking if its link contains gh_current_run_id.
162+
NON_PASSING_CHECKS=$(gh pr checks "${gh_pr_number}" \
163+
--repo "${gh_repository}" \
164+
--json workflow,name,state,link |
165+
jq --compact --arg current_run_id "${gh_current_run_id}" \
166+
'.[] | select(
167+
(.link | contains($current_run_id) | not)
168+
and
169+
(.state | test("^(SUCCESS|SKIPPED|NEUTRAL)$") | not)
170+
)')
171+
172+
if [ -n "${NON_PASSING_CHECKS}" ]; then
173+
echo "Cannot auto-merge. The following checks are not passing:"
174+
echo "${NON_PASSING_CHECKS}" | jq -r '"workflow: \(.workflow), name: \(.name), state: \(.state)""'
175+
exit 1
176+
fi
177+
140178
- name: Enable auto-merge via squash
141179
env:
142180
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)