Skip to content

Commit dc0e7b2

Browse files
authored
Merge pull request #31 from QuantStrategyLab/feat/dependabot-head-sha-guard
feat: add Dependabot PR head SHA guard before auto-merge
2 parents 121e305 + d4fcc7b commit dc0e7b2

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

.github/workflows/dependabot_auto_merge.yml

Lines changed: 13 additions & 9 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
@@ -47,18 +54,15 @@ jobs:
4754
labels = {item.get("name", "") for item in pr.get("labels", [])}
4855
body = pr.get("body") or ""
4956
is_major = "update-type: version-update:semver-major" in body
50-
should_merge = (
51-
author in {"dependabot[bot]", "app/dependabot"}
52-
and not pr.get("isDraft")
53-
and "dependencies" in labels
54-
and not is_major
55-
)
57+
dependabot_authors = {"dependabot[bot]", "app/dependabot"}
58+
is_dependabot = author in dependabot_authors and "dependencies" in labels
59+
should_merge = is_dependabot and not pr.get("isDraft") and not is_major
5660
if should_merge:
5761
reason = "ready"
5862
elif is_major:
5963
reason = "major_update"
6064
else:
61-
reason = "not_dependabot_or_draft"
65+
reason = "not_eligible_dependabot_pr"
6266
6367
summary_lines = [
6468
"## Auto-Merge Gate",
@@ -84,4 +88,4 @@ jobs:
8488
if: steps.merge_guard.outputs.should_merge == 'true'
8589
env:
8690
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
87-
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)