|
| 1 | +name: Auto-merge reusable workflow |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + head_branch: |
| 7 | + description: 'Branch name to find PR for' |
| 8 | + required: false |
| 9 | + type: string |
| 10 | + |
| 11 | +permissions: |
| 12 | + contents: write |
| 13 | + pull-requests: write |
| 14 | + |
| 15 | +jobs: |
| 16 | + auto-merge: |
| 17 | + runs-on: ubuntu-latest |
| 18 | + steps: |
| 19 | + - name: Auto-merge matching PR |
| 20 | + uses: actions/github-script@v7 |
| 21 | + with: |
| 22 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 23 | + script: | |
| 24 | + // Use the provided input; caller should supply `head_branch` when invoking. |
| 25 | + const head_branch = "${{ inputs.head_branch }}"; |
| 26 | + const owner = context.repo.owner; |
| 27 | + const repo = context.repo.repo; |
| 28 | +
|
| 29 | + if (!head_branch) { |
| 30 | + core.info('No head_branch provided; nothing to do.'); |
| 31 | + return; |
| 32 | + } |
| 33 | +
|
| 34 | + core.info(`Auto-merge triggered for branch: ${head_branch}`); |
| 35 | +
|
| 36 | + const prs = await github.rest.pulls.list({ |
| 37 | + owner, |
| 38 | + repo, |
| 39 | + head: `${owner}:${head_branch}`, |
| 40 | + state: 'open', |
| 41 | + }); |
| 42 | +
|
| 43 | + if (!prs.data || prs.data.length === 0) { |
| 44 | + core.info(`No open pull request found for branch ${head_branch}`); |
| 45 | + return; |
| 46 | + } |
| 47 | +
|
| 48 | + const pr = prs.data[0]; |
| 49 | +
|
| 50 | + if (pr.draft) { |
| 51 | + core.info(`Pull request #${pr.number} is a draft; skipping merge.`); |
| 52 | + return; |
| 53 | + } |
| 54 | +
|
| 55 | + try { |
| 56 | + const res = await github.rest.pulls.merge({ |
| 57 | + owner, |
| 58 | + repo, |
| 59 | + pull_number: pr.number, |
| 60 | + merge_method: 'merge', |
| 61 | + commit_title: head_branch, |
| 62 | + commit_message: '', |
| 63 | + }); |
| 64 | +
|
| 65 | + if (res.data.merged) { |
| 66 | + core.info(`Successfully merged PR #${pr.number} (branch ${head_branch}).`); |
| 67 | + } else { |
| 68 | + core.info(`Merge attempt returned: ${JSON.stringify(res.data)}`); |
| 69 | + } |
| 70 | + } catch (err) { |
| 71 | + core.setFailed(`Failed to merge PR #${pr.number}: ${err}`); |
| 72 | + } |
| 73 | +
|
| 74 | + - name: Done |
| 75 | + run: echo "auto-merge finished" |
0 commit comments