|
| 1 | +name: Dependabot auto-merge |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + types: |
| 6 | + - opened |
| 7 | + - reopened |
| 8 | + - synchronize |
| 9 | + - ready_for_review |
| 10 | + |
| 11 | +permissions: |
| 12 | + contents: write |
| 13 | + pull-requests: write |
| 14 | + |
| 15 | +concurrency: |
| 16 | + group: dependabot-auto-merge-${{ github.event.pull_request.number }} |
| 17 | + cancel-in-progress: true |
| 18 | + |
| 19 | +jobs: |
| 20 | + dependabot: |
| 21 | + name: approve and auto-merge |
| 22 | + runs-on: ubuntu-latest |
| 23 | + if: github.event.pull_request.user.login == 'dependabot[bot]' && github.event.pull_request.draft == false |
| 24 | + steps: |
| 25 | + - name: Fetch Dependabot metadata |
| 26 | + id: dependabot-metadata |
| 27 | + uses: dependabot/fetch-metadata@25dd0e34f4fe68f24cc83900b1fe3fe149efef98 # v3.1.0 |
| 28 | + with: |
| 29 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 30 | + |
| 31 | + - name: Verify dependency update scope |
| 32 | + env: |
| 33 | + PACKAGE_ECOSYSTEM: ${{ steps.dependabot-metadata.outputs.package-ecosystem }} |
| 34 | + DEPENDENCY_NAMES: ${{ steps.dependabot-metadata.outputs.dependency-names }} |
| 35 | + UPDATED_DEPENDENCIES_JSON: ${{ steps.dependabot-metadata.outputs.updated-dependencies-json }} |
| 36 | + run: | |
| 37 | + set -euo pipefail |
| 38 | +
|
| 39 | + case "$PACKAGE_ECOSYSTEM" in |
| 40 | + github-actions|npm|deno) ;; |
| 41 | + *) |
| 42 | + echo "::error::Unsupported Dependabot ecosystem: $PACKAGE_ECOSYSTEM" |
| 43 | + exit 1 |
| 44 | + ;; |
| 45 | + esac |
| 46 | +
|
| 47 | + if [ -z "$DEPENDENCY_NAMES" ]; then |
| 48 | + echo "::error::Dependabot metadata did not include dependency names." |
| 49 | + exit 1 |
| 50 | + fi |
| 51 | +
|
| 52 | + dependency_count="$(jq 'length' <<<"$UPDATED_DEPENDENCIES_JSON")" |
| 53 | + if [ "$dependency_count" -eq 0 ]; then |
| 54 | + echo "::error::Dependabot metadata did not include updated dependencies." |
| 55 | + exit 1 |
| 56 | + fi |
| 57 | +
|
| 58 | + - name: Approve Dependabot PR |
| 59 | + env: |
| 60 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 61 | + PR_URL: ${{ github.event.pull_request.html_url }} |
| 62 | + run: | |
| 63 | + set -euo pipefail |
| 64 | +
|
| 65 | + review_decision="$(gh pr view "$PR_URL" --json reviewDecision -q .reviewDecision)" |
| 66 | + if [ "$review_decision" = "APPROVED" ]; then |
| 67 | + echo "PR is already approved." |
| 68 | + else |
| 69 | + gh pr review --approve "$PR_URL" |
| 70 | + fi |
| 71 | +
|
| 72 | + - name: Enable auto-merge |
| 73 | + env: |
| 74 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 75 | + PR_URL: ${{ github.event.pull_request.html_url }} |
| 76 | + run: gh pr merge --auto --squash --delete-branch "$PR_URL" |
0 commit comments