Skip to content

Commit a634da8

Browse files
authored
Merge pull request #21 from QuantStrategyLab/feat/dependabot-head-sha-guard
feat: add Dependabot PR head SHA guard before auto-merge
2 parents 9fe2195 + 8f623f7 commit a634da8

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

.github/workflows/dependabot_auto_merge.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,19 @@ jobs:
2222
run: |
2323
set -euo pipefail
2424
BRANCH_NAME="${{ github.event.workflow_run.head_branch }}"
25-
PR_NUMBER=$(gh pr list --repo "${GITHUB_REPOSITORY}" --state open --head "${BRANCH_NAME}" --json number --jq '.[0].number // empty')
25+
PR_PAYLOAD=$(gh pr list --repo "${GITHUB_REPOSITORY}" --state open --head "${BRANCH_NAME}" --json number,headRefOid --jq '.[0] // {}')
26+
PR_NUMBER=$(python3 -c 'import json,sys; print(json.load(sys.stdin).get("number", ""))' <<<"${PR_PAYLOAD}")
27+
PR_HEAD_SHA=$(python3 -c 'import json,sys; print(json.load(sys.stdin).get("headRefOid", ""))' <<<"${PR_PAYLOAD}")
2628
if [ -z "${PR_NUMBER}" ]; then
2729
echo "No open Dependabot PR found for ${BRANCH_NAME}." >> "$GITHUB_STEP_SUMMARY"
2830
exit 0
2931
fi
32+
if [ "${PR_HEAD_SHA}" != "${{ github.event.workflow_run.head_sha }}" ]; then
33+
echo "Skipping auto-merge: PR #${PR_NUMBER} head ${PR_HEAD_SHA} does not match completed CI head ${{ github.event.workflow_run.head_sha }}." >> "$GITHUB_STEP_SUMMARY"
34+
exit 0
35+
fi
3036
echo "pr_number=${PR_NUMBER}" >> "$GITHUB_OUTPUT"
37+
echo "head_sha=${PR_HEAD_SHA}" >> "$GITHUB_OUTPUT"
3138
3239
- name: Evaluate merge eligibility
3340
id: merge_guard
@@ -81,4 +88,4 @@ jobs:
8188
if: steps.merge_guard.outputs.should_merge == 'true'
8289
env:
8390
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
84-
run: gh pr merge "${{ steps.pr.outputs.pr_number }}" --repo "${GITHUB_REPOSITORY}" --rebase --delete-branch
91+
run: gh pr merge "${{ steps.pr.outputs.pr_number }}" --repo "${GITHUB_REPOSITORY}" --rebase --delete-branch --match-head-commit "${{ steps.pr.outputs.head_sha }}"

0 commit comments

Comments
 (0)