|
| 1 | +name: Auto-Merge Dependabot PRs |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, reopened, labeled, synchronize] |
| 6 | + |
| 7 | +jobs: |
| 8 | + auto-merge: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + steps: |
| 11 | + - name: Check if PR is eligible for auto-merge |
| 12 | + id: check |
| 13 | + uses: actions/github-script@v6 |
| 14 | + with: |
| 15 | + script: | |
| 16 | + const prNumber = context.payload.pull_request.number; |
| 17 | + const { data: pr } = await github.rest.pulls.get({ |
| 18 | + owner: context.repo.owner, |
| 19 | + repo: context.repo.repo, |
| 20 | + pull_number: prNumber |
| 21 | + }); |
| 22 | + |
| 23 | + // Only proceed if the PR is created by Dependabot. |
| 24 | + if (pr.user.login !== 'dependabot[bot]') { |
| 25 | + core.info("PR is not from Dependabot. Skipping auto-merge."); |
| 26 | + core.setOutput("shouldMerge", "false"); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + // Check if the PR has the 'automerge' label. |
| 31 | + const labels = pr.labels.map(label => label.name); |
| 32 | + if (!labels.includes('automerge')) { |
| 33 | + core.info("PR does not have the 'automerge' label. Skipping auto-merge."); |
| 34 | + core.setOutput("shouldMerge", "false"); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + // Retrieve the combined status for the PR's latest commit. |
| 39 | + const { data: combinedStatus } = await github.rest.repos.getCombinedStatusForRef({ |
| 40 | + owner: context.repo.owner, |
| 41 | + repo: context.repo.repo, |
| 42 | + ref: pr.head.sha |
| 43 | + }); |
| 44 | + core.info(`Combined status state: ${combinedStatus.state}`); |
| 45 | + |
| 46 | + // Only merge if all status checks have passed. |
| 47 | + if (combinedStatus.state !== 'success') { |
| 48 | + core.info("Not all status checks have passed. Skipping auto-merge."); |
| 49 | + core.setOutput("shouldMerge", "false"); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + core.info("All conditions met. PR is eligible for auto-merge."); |
| 54 | + core.setOutput("shouldMerge", "true"); |
| 55 | + result-encoding: string |
| 56 | + |
| 57 | + - name: Auto-Merge PR |
| 58 | + if: steps.check.outputs.shouldMerge == 'true' |
| 59 | + uses: actions/github-script@v6 |
| 60 | + with: |
| 61 | + script: | |
| 62 | + const prNumber = context.payload.pull_request.number; |
| 63 | + const mergeResponse = await github.rest.pulls.merge({ |
| 64 | + owner: context.repo.owner, |
| 65 | + repo: context.repo.repo, |
| 66 | + pull_number: prNumber, |
| 67 | + merge_method: "squash" |
| 68 | + }); |
| 69 | + core.info(`Merge response: ${JSON.stringify(mergeResponse.data)}`); |
0 commit comments