|
| 1 | +name: Auto Merge Dependabot PR |
| 2 | + |
| 3 | +"on": |
| 4 | + workflow_run: |
| 5 | + workflows: ["CI"] |
| 6 | + types: [completed] |
| 7 | + |
| 8 | +jobs: |
| 9 | + auto-merge: |
| 10 | + if: github.event.workflow_run.conclusion == 'success' && startsWith(github.event.workflow_run.head_branch, 'dependabot/') |
| 11 | + runs-on: ubuntu-latest |
| 12 | + timeout-minutes: 10 |
| 13 | + permissions: |
| 14 | + contents: write |
| 15 | + pull-requests: write |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Resolve Dependabot PR |
| 19 | + id: pr |
| 20 | + env: |
| 21 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 22 | + run: | |
| 23 | + set -euo pipefail |
| 24 | + BRANCH_NAME="${{ github.event.workflow_run.head_branch }}" |
| 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}") |
| 28 | + if [ -z "${PR_NUMBER}" ]; then |
| 29 | + echo "No open Dependabot PR found for ${BRANCH_NAME}." >> "$GITHUB_STEP_SUMMARY" |
| 30 | + exit 0 |
| 31 | + 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 |
| 36 | + echo "pr_number=${PR_NUMBER}" >> "$GITHUB_OUTPUT" |
| 37 | + echo "head_sha=${PR_HEAD_SHA}" >> "$GITHUB_OUTPUT" |
| 38 | +
|
| 39 | + - name: Evaluate merge eligibility |
| 40 | + id: merge_guard |
| 41 | + if: steps.pr.outputs.pr_number != '' |
| 42 | + env: |
| 43 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 44 | + run: | |
| 45 | + set -euo pipefail |
| 46 | + gh pr view "${{ steps.pr.outputs.pr_number }}" --repo "${GITHUB_REPOSITORY}" --json number,isDraft,author,url,body,labels > pr.json |
| 47 | + python3 - <<'PY' |
| 48 | + import json |
| 49 | + import os |
| 50 | + from pathlib import Path |
| 51 | +
|
| 52 | + pr = json.loads(Path("pr.json").read_text(encoding="utf-8")) |
| 53 | + author = (pr.get("author") or {}).get("login") |
| 54 | + labels = {item.get("name", "") for item in pr.get("labels", [])} |
| 55 | + body = pr.get("body") or "" |
| 56 | + is_major = "update-type: version-update:semver-major" in body |
| 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 |
| 60 | + if should_merge: |
| 61 | + reason = "ready" |
| 62 | + elif is_major: |
| 63 | + reason = "major_update" |
| 64 | + else: |
| 65 | + reason = "not_eligible_dependabot_pr" |
| 66 | +
|
| 67 | + summary_lines = [ |
| 68 | + "## Auto-Merge Gate", |
| 69 | + f"- PR: {pr['url']}", |
| 70 | + f"- Author: `{author or '<unknown>'}`", |
| 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'}`", |
| 75 | + f"- Reason: `{reason}`", |
| 76 | + ] |
| 77 | + Path("pr-summary.md").write_text("\n".join(summary_lines).strip() + "\n", encoding="utf-8") |
| 78 | + with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as output: |
| 79 | + print(f"should_merge={'true' if should_merge else 'false'}", file=output) |
| 80 | + print(f"reason={reason}", file=output) |
| 81 | + PY |
| 82 | +
|
| 83 | + - name: Append merge summary |
| 84 | + if: steps.pr.outputs.pr_number != '' |
| 85 | + run: cat pr-summary.md >> "$GITHUB_STEP_SUMMARY" |
| 86 | + |
| 87 | + - name: Merge Dependabot PR |
| 88 | + if: steps.merge_guard.outputs.should_merge == 'true' |
| 89 | + env: |
| 90 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 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