|
| 1 | +name: Dependabot Auto-Merge |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + types: |
| 6 | + - opened |
| 7 | + - reopened |
| 8 | + - synchronize |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: read |
| 12 | + pull-requests: read |
| 13 | + |
| 14 | +jobs: |
| 15 | + validate-dependabot-pr: |
| 16 | + if: | |
| 17 | + github.event.pull_request.user.login == 'dependabot[bot]' && |
| 18 | + github.event.pull_request.head.repo.full_name == github.repository && |
| 19 | + startsWith(github.event.pull_request.head.ref, 'dependabot/') |
| 20 | + runs-on: ubuntu-latest |
| 21 | + permissions: |
| 22 | + contents: read |
| 23 | + pull-requests: read |
| 24 | + outputs: |
| 25 | + eligible: ${{ steps.eligibility.outputs.eligible }} |
| 26 | + update_type: ${{ steps.metadata.outputs.update-type }} |
| 27 | + steps: |
| 28 | + - name: Fetch Dependabot metadata |
| 29 | + id: metadata |
| 30 | + uses: dependabot/fetch-metadata@v3 |
| 31 | + with: |
| 32 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 33 | + |
| 34 | + - name: Validate PR eligibility |
| 35 | + id: eligibility |
| 36 | + env: |
| 37 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 38 | + REPO: ${{ github.repository }} |
| 39 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 40 | + DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} |
| 41 | + UPDATE_TYPE: ${{ steps.metadata.outputs.update-type }} |
| 42 | + ECOSYSTEM: ${{ steps.metadata.outputs.package-ecosystem }} |
| 43 | + TARGET_BRANCH: ${{ steps.metadata.outputs.target-branch }} |
| 44 | + run: | |
| 45 | + echo "eligible=false" >> "$GITHUB_OUTPUT" |
| 46 | +
|
| 47 | + if [ "$UPDATE_TYPE" != "version-update:semver-patch" ] && [ "$UPDATE_TYPE" != "version-update:semver-minor" ]; then |
| 48 | + echo "Skipping: update type is '$UPDATE_TYPE', only semver patch/minor are eligible." |
| 49 | + exit 0 |
| 50 | + fi |
| 51 | +
|
| 52 | + if [ "$ECOSYSTEM" != "npm_and_yarn" ]; then |
| 53 | + echo "Skipping: only npm updates are eligible (got '$ECOSYSTEM')." |
| 54 | + exit 0 |
| 55 | + fi |
| 56 | +
|
| 57 | + if [ "$TARGET_BRANCH" != "$DEFAULT_BRANCH" ]; then |
| 58 | + echo "Skipping: target branch '$TARGET_BRANCH' is not default branch '$DEFAULT_BRANCH'." |
| 59 | + exit 0 |
| 60 | + fi |
| 61 | +
|
| 62 | + disallowed=0 |
| 63 | + file_count=0 |
| 64 | + while IFS= read -r file; do |
| 65 | + [ -z "$file" ] && continue |
| 66 | + file_count=$((file_count + 1)) |
| 67 | + if [[ "$file" =~ (^|/)package\.json$|(^|/)package-lock\.json$|(^|/)npm-shrinkwrap\.json$|(^|/)yarn\.lock$|(^|/)pnpm-lock\.yaml$ ]]; then |
| 68 | + echo "Allowed changed file: $file" |
| 69 | + else |
| 70 | + echo "Disallowed changed file: $file" |
| 71 | + disallowed=1 |
| 72 | + fi |
| 73 | + done < <(gh api "repos/$REPO/pulls/$PR_NUMBER/files" --paginate --jq '.[].filename') |
| 74 | +
|
| 75 | + if [ "$file_count" -eq 0 ]; then |
| 76 | + echo "Skipping: no changed files were detected." |
| 77 | + exit 0 |
| 78 | + fi |
| 79 | +
|
| 80 | + if [ "$disallowed" -ne 0 ]; then |
| 81 | + echo "Skipping: PR includes files outside dependency manifests/lockfiles." |
| 82 | + exit 0 |
| 83 | + fi |
| 84 | +
|
| 85 | + echo "eligible=true" >> "$GITHUB_OUTPUT" |
| 86 | +
|
| 87 | + enable-automerge: |
| 88 | + needs: validate-dependabot-pr |
| 89 | + if: needs.validate-dependabot-pr.outputs.eligible == 'true' |
| 90 | + runs-on: ubuntu-latest |
| 91 | + permissions: |
| 92 | + contents: write |
| 93 | + pull-requests: write |
| 94 | + steps: |
| 95 | + - name: Select allowed merge method |
| 96 | + id: merge-method |
| 97 | + env: |
| 98 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 99 | + REPO: ${{ github.repository }} |
| 100 | + run: | |
| 101 | + allow_merge_commit=$(gh api "repos/$REPO" --jq '.allow_merge_commit') |
| 102 | + allow_squash_merge=$(gh api "repos/$REPO" --jq '.allow_squash_merge') |
| 103 | + allow_rebase_merge=$(gh api "repos/$REPO" --jq '.allow_rebase_merge') |
| 104 | +
|
| 105 | + if [ "$allow_squash_merge" = "true" ]; then |
| 106 | + echo "flag=--squash" >> "$GITHUB_OUTPUT" |
| 107 | + elif [ "$allow_rebase_merge" = "true" ]; then |
| 108 | + echo "flag=--rebase" >> "$GITHUB_OUTPUT" |
| 109 | + elif [ "$allow_merge_commit" = "true" ]; then |
| 110 | + echo "flag=--merge" >> "$GITHUB_OUTPUT" |
| 111 | + else |
| 112 | + echo "No merge methods are enabled in repository settings." >&2 |
| 113 | + exit 1 |
| 114 | + fi |
| 115 | +
|
| 116 | + - name: Enable auto-merge for minor and patch updates |
| 117 | + env: |
| 118 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 119 | + PR_URL: ${{ github.event.pull_request.html_url }} |
| 120 | + MERGE_FLAG: ${{ steps.merge-method.outputs.flag }} |
| 121 | + UPDATE_TYPE: ${{ needs.validate-dependabot-pr.outputs.update_type }} |
| 122 | + run: | |
| 123 | + gh pr review "$PR_URL" --approve --body "Auto-approving Dependabot $UPDATE_TYPE update." |
| 124 | + gh pr merge --auto "$MERGE_FLAG" "$PR_URL" |
0 commit comments