|
| 1 | +name: Dependabot security auto-merge |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: ["Tests"] |
| 6 | + types: [completed] |
| 7 | + |
| 8 | +jobs: |
| 9 | + automerge: |
| 10 | + # Only proceed if CI passed and the workflow was triggered by a pull request |
| 11 | + if: github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'pull_request' |
| 12 | + concurrency: |
| 13 | + group: dependabot-automerge-${{ github.event.workflow_run.head_sha }} |
| 14 | + cancel-in-progress: true |
| 15 | + runs-on: ubuntu-latest |
| 16 | + permissions: |
| 17 | + pull-requests: write |
| 18 | + contents: write |
| 19 | + |
| 20 | + steps: |
| 21 | + - name: Get PR number for this workflow run |
| 22 | + id: get-pr |
| 23 | + env: |
| 24 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 25 | + HEAD_SHA: ${{ github.event.workflow_run.head_sha }} |
| 26 | + run: | |
| 27 | + # Prefer PR number from the workflow_run payload when available |
| 28 | + PR_NUMBER="${{ github.event.workflow_run.pull_requests[0].number }}" |
| 29 | +
|
| 30 | + # Fallback: search all open PRs (with pagination) for matching HEAD_SHA |
| 31 | + if [ -z "$PR_NUMBER" ]; then |
| 32 | + PR_NUMBER=$(gh api repos/${{ github.repository }}/pulls --paginate \ |
| 33 | + --jq ".[] | select(.head.sha == \"$HEAD_SHA\") | .number" | head -1) |
| 34 | + fi |
| 35 | +
|
| 36 | + echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT |
| 37 | +
|
| 38 | + - name: Get PR author and changed files |
| 39 | + id: pr-info |
| 40 | + if: steps.get-pr.outputs.pr_number != '' |
| 41 | + env: |
| 42 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 43 | + PR_NUMBER: ${{ steps.get-pr.outputs.pr_number }} |
| 44 | + run: | |
| 45 | + AUTHOR=$(gh api repos/${{ github.repository }}/pulls/$PR_NUMBER \ |
| 46 | + --jq '.user.login') |
| 47 | + FILES=$(gh api repos/${{ github.repository }}/pulls/$PR_NUMBER/files \ |
| 48 | + --jq '[.[].filename] | join(" ")') |
| 49 | + BODY=$(gh api repos/${{ github.repository }}/pulls/$PR_NUMBER \ |
| 50 | + --jq '.body') |
| 51 | + if echo "$BODY" | grep -qE '(GHSA-[A-Za-z0-9-]+|CVE-[0-9]{4}-[0-9]+|dependabot-automerge-start)'; then |
| 52 | + IS_SECURITY=true |
| 53 | + else |
| 54 | + IS_SECURITY=false |
| 55 | + fi |
| 56 | + echo "author=$AUTHOR" >> $GITHUB_OUTPUT |
| 57 | + echo "files=$FILES" >> $GITHUB_OUTPUT |
| 58 | + echo "is_security=$IS_SECURITY" >> $GITHUB_OUTPUT |
| 59 | +
|
| 60 | + - name: Auto-approve and merge security-only lock file updates |
| 61 | + if: | |
| 62 | + steps.pr-info.outputs.author == 'dependabot[bot]' && |
| 63 | + steps.pr-info.outputs.files == 'package-lock.json' && |
| 64 | + steps.pr-info.outputs.is_security == 'true' |
| 65 | + env: |
| 66 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 67 | + PR_NUMBER: ${{ steps.get-pr.outputs.pr_number }} |
| 68 | + run: | |
| 69 | + gh pr review $PR_NUMBER --approve --repo ${{ github.repository }} \ |
| 70 | + --body "Auto-approving: security patch, package-lock.json only." |
| 71 | + gh pr merge $PR_NUMBER --squash --repo ${{ github.repository }} |
0 commit comments