Skip to content

Commit c1ff953

Browse files
authored
Merge pull request #77 from QuantStrategyLab/feat/dependabot-head-sha-guard
fix: guard Dependabot auto-merge with head SHA check
2 parents 1242eb7 + a291f34 commit c1ff953

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

.github/workflows/dependabot_auto_merge.yml

Lines changed: 13 additions & 6 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
@@ -61,10 +68,10 @@ jobs:
6168
"## Auto-Merge Gate",
6269
f"- PR: {pr['url']}",
6370
f"- Author: `{author or '<unknown>'}`",
64-
f"- Draft: `{ 'yes' if pr.get('isDraft') else 'no' }`",
65-
f"- Dependabot label: `{ 'yes' if 'dependencies' in labels else 'no' }`",
66-
f"- Major update: `{ 'yes' if is_major else 'no' }`",
67-
f"- Final merge decision: `{ 'merge' if should_merge else 'skip' }`",
71+
f"- Draft: `{'yes' if pr.get('isDraft') else 'no'}`",
72+
f"- Dependabot label: `{'yes' if 'dependencies' in labels else 'no'}`",
73+
f"- Major update: `{'yes' if is_major else 'no'}`",
74+
f"- Final merge decision: `{'merge' if should_merge else 'skip'}`",
6875
f"- Reason: `{reason}`",
6976
]
7077
Path("pr-summary.md").write_text("\n".join(summary_lines).strip() + "\n", encoding="utf-8")
@@ -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)