|
| 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 | + permissions: |
| 13 | + contents: write |
| 14 | + pull-requests: write |
| 15 | + |
| 16 | + steps: |
| 17 | + - name: Resolve Dependabot PR |
| 18 | + id: pr |
| 19 | + env: |
| 20 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 21 | + run: | |
| 22 | + BRANCH_NAME="${{ github.event.workflow_run.head_branch }}" |
| 23 | + PR_NUMBER=$(gh pr list --state open --head "${BRANCH_NAME}" --json number --jq '.[0].number // empty') |
| 24 | + if [ -z "${PR_NUMBER}" ]; then |
| 25 | + echo "No open Dependabot PR found for ${BRANCH_NAME}." >> "$GITHUB_STEP_SUMMARY" |
| 26 | + exit 0 |
| 27 | + fi |
| 28 | + echo "pr_number=${PR_NUMBER}" >> "$GITHUB_OUTPUT" |
| 29 | +
|
| 30 | + - name: Evaluate merge eligibility |
| 31 | + id: merge_guard |
| 32 | + if: steps.pr.outputs.pr_number != '' |
| 33 | + env: |
| 34 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 35 | + run: | |
| 36 | + gh pr view "${{ steps.pr.outputs.pr_number }}" --json number,isDraft,author,url,labels > pr.json |
| 37 | + python3 - <<'PY' |
| 38 | + import json |
| 39 | + import os |
| 40 | + from pathlib import Path |
| 41 | +
|
| 42 | + pr = json.loads(Path("pr.json").read_text(encoding="utf-8")) |
| 43 | + author = (pr.get("author") or {}).get("login") |
| 44 | + labels = {item.get("name", "") for item in pr.get("labels", [])} |
| 45 | + should_merge = author == "dependabot[bot]" and not pr.get("isDraft") |
| 46 | + reason = "ready" if should_merge else "not_dependabot_or_draft" |
| 47 | +
|
| 48 | + summary_lines = [ |
| 49 | + "## Auto-Merge Gate", |
| 50 | + f"- PR: {pr['url']}", |
| 51 | + f"- Author: `{author or '<unknown>'}`", |
| 52 | + f"- Draft: `{ 'yes' if pr.get('isDraft') else 'no' }`", |
| 53 | + f"- Dependabot label: `{ 'yes' if 'dependencies' in labels else 'no' }`", |
| 54 | + f"- Final merge decision: `{ 'merge' if should_merge else 'skip' }`", |
| 55 | + f"- Reason: `{reason}`", |
| 56 | + ] |
| 57 | + Path("pr-summary.md").write_text("\n".join(summary_lines).strip() + "\n", encoding="utf-8") |
| 58 | + with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as output: |
| 59 | + print(f"should_merge={'true' if should_merge else 'false'}", file=output) |
| 60 | + print(f"reason={reason}", file=output) |
| 61 | + PY |
| 62 | +
|
| 63 | + - name: Append merge summary |
| 64 | + if: steps.pr.outputs.pr_number != '' |
| 65 | + run: cat pr-summary.md >> "$GITHUB_STEP_SUMMARY" |
| 66 | + |
| 67 | + - name: Merge Dependabot PR |
| 68 | + if: steps.merge_guard.outputs.should_merge == 'true' |
| 69 | + env: |
| 70 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 71 | + run: gh pr merge "${{ steps.pr.outputs.pr_number }}" --rebase --delete-branch |
0 commit comments