|
| 1 | +name: Auto-merge Dependabot when green |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened, labeled] |
| 6 | + check_suite: |
| 7 | + types: [completed] |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: write |
| 12 | + pull-requests: write |
| 13 | + |
| 14 | +jobs: |
| 15 | + enable-automerge: |
| 16 | + name: enable-automerge |
| 17 | + # Solo si el PR es de dependabot o si el actor es dependabot |
| 18 | + if: > |
| 19 | + (github.event.pull_request.user.login == 'dependabot[bot]') || |
| 20 | + (github.actor == 'dependabot[bot]') |
| 21 | + runs-on: ubuntu-latest |
| 22 | + steps: |
| 23 | + - name: Find PR for this context (fallback when dispatched) |
| 24 | + id: find_pr |
| 25 | + uses: actions/github-script@v7 |
| 26 | + with: |
| 27 | + script: | |
| 28 | + const {owner, repo} = context.repo; |
| 29 | + // Si viene de PR, úsalo |
| 30 | + if (context.payload.pull_request) { |
| 31 | + return { number: context.payload.pull_request.number }; |
| 32 | + } |
| 33 | + // Si viene de check_suite/completed o dispatch, intenta localizar PR abierto |
| 34 | + const head = (context.ref || '').replace('refs/heads/',''); |
| 35 | + const { data: prs } = await github.rest.pulls.list({ owner, repo, state: 'open', head: `${owner}:${head}` }); |
| 36 | + const pr = prs.find(p => p.user.login === 'dependabot[bot]'); |
| 37 | + return pr ? { number: pr.number } : {}; |
| 38 | + - name: Enable auto-merge (squash) if checks are green |
| 39 | + if: steps.find_pr.outputs.result != '' |
| 40 | + uses: actions/github-script@v7 |
| 41 | + with: |
| 42 | + script: | |
| 43 | + const {owner, repo} = context.repo; |
| 44 | + const prNumber = JSON.parse(core.getInput('result'))?.number || core.getInput('result')?.number || null; |
| 45 | + if (!prNumber) { |
| 46 | + core.info('No PR found for this ref; exiting.'); |
| 47 | + return; |
| 48 | + } |
| 49 | + // Obtén el PR para su node_id (GraphQL requiere ID) |
| 50 | + const { data: pr } = await github.rest.pulls.get({ owner, repo, pull_number: prNumber }); |
| 51 | + // Habilita auto-merge por GraphQL (método squash) |
| 52 | + const mutation = ` |
| 53 | + mutation($pr:ID!){ |
| 54 | + enablePullRequestAutoMerge(input:{pullRequestId:$pr, mergeMethod:SQUASH}) { clientMutationId } |
| 55 | + }`; |
| 56 | + await github.graphql(mutation, { pr: pr.node_id }); |
| 57 | + core.info(`Auto-merge enabled on PR #${prNumber}`); |
0 commit comments