|
| 1 | +name: Dependabot Auto Merge |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: |
| 6 | + - opened |
| 7 | + - reopened |
| 8 | + - synchronize |
| 9 | + - labeled |
| 10 | + - unlabeled |
| 11 | + - ready_for_review |
| 12 | + pull_request_review: |
| 13 | + types: [submitted] |
| 14 | + check_suite: |
| 15 | + types: [completed] |
| 16 | + status: {} |
| 17 | + |
| 18 | +permissions: |
| 19 | + contents: write |
| 20 | + pull-requests: write |
| 21 | + |
| 22 | +jobs: |
| 23 | + auto-merge: |
| 24 | + if: >- |
| 25 | + github.actor == 'dependabot[bot]' || github.actor == 'dependabot' |
| 26 | + runs-on: ubuntu-latest |
| 27 | + steps: |
| 28 | + - name: Checkout |
| 29 | + uses: actions/checkout@v4 |
| 30 | + - name: Set up Node |
| 31 | + uses: actions/setup-node@v4 |
| 32 | + with: |
| 33 | + node-version: 22.x |
| 34 | + - name: Enable auto-merge if criteria met |
| 35 | + env: |
| 36 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 37 | + run: | |
| 38 | + PR_NUMBER=${{ github.event.pull_request.number }} |
| 39 | + # Fetch metadata |
| 40 | + echo "Evaluating PR #$PR_NUMBER from Dependabot" |
| 41 | + # Require all required checks to pass before auto-merge |
| 42 | + gh pr view $PR_NUMBER --json mergeStateStatus,reviewDecision,headRefName,labels,author || exit 0 |
| 43 | + MERGE_STATE=$(gh pr view $PR_NUMBER --json mergeStateStatus -q .mergeStateStatus) |
| 44 | + if [ "$MERGE_STATE" != "CLEAN" ] && [ "$MERGE_STATE" != "HAS_HOOKS" ]; then |
| 45 | + echo "Merge state not clean ($MERGE_STATE). Skipping."; exit 0; fi |
| 46 | + # Optional: skip major version bumps unless label allow-major is present |
| 47 | + TITLE=$(gh pr view $PR_NUMBER --json title -q .title) |
| 48 | + if echo "$TITLE" | grep -Eqi '\\bmajor\\b'; then |
| 49 | + if ! gh pr view $PR_NUMBER --json labels -q '.labels[].name' | grep -q '^allow-major$'; then |
| 50 | + echo "Detected potential major bump. Add label 'allow-major' to permit auto-merge."; exit 0; fi |
| 51 | + fi |
| 52 | + # Approve if not already approved (Dependabot PRs sometimes need an approval) |
| 53 | + if gh pr view $PR_NUMBER --json reviewDecision -q .reviewDecision | grep -q "REVIEW_REQUIRED"; then |
| 54 | + gh pr review $PR_NUMBER --approve --body "Auto-approval for Dependabot update" || true |
| 55 | + fi |
| 56 | + # Attempt enable auto-merge (squash by default) |
| 57 | + gh pr merge $PR_NUMBER --auto --squash --delete-branch || true |
0 commit comments