|
| 1 | +name: Auto-merge on successful release pipeline |
| 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 | + // Support both automatic workflow_run triggers and explicit workflow_call inputs. |
| 25 | + const run = context.payload.workflow_run; |
| 26 | + let head_branch = ''; |
| 27 | + if (run && run.head_branch) { |
| 28 | + head_branch = run.head_branch; |
| 29 | + } else if (context.payload && context.payload.inputs && context.payload.inputs.head_branch) { |
| 30 | + head_branch = context.payload.inputs.head_branch; |
| 31 | + } |
| 32 | +
|
| 33 | + const owner = context.repo.owner; |
| 34 | + const repo = context.repo.repo; |
| 35 | +
|
| 36 | + if (!head_branch) { |
| 37 | + core.info('No head_branch provided; nothing to do.'); |
| 38 | + return; |
| 39 | + } |
| 40 | +
|
| 41 | + core.info(`Auto-merge triggered for branch: ${head_branch}`); |
| 42 | +
|
| 43 | + // Find an open PR whose head ref matches the branch |
| 44 | + const prs = await github.rest.pulls.list({ |
| 45 | + owner, |
| 46 | + repo, |
| 47 | + head: `${owner}:${head_branch}`, |
| 48 | + state: 'open', |
| 49 | + }); |
| 50 | +
|
| 51 | + if (!prs.data || prs.data.length === 0) { |
| 52 | + core.info(`No open pull request found for branch ${head_branch}`); |
| 53 | + return; |
| 54 | + } |
| 55 | +
|
| 56 | + const pr = prs.data[0]; |
| 57 | +
|
| 58 | + if (pr.draft) { |
| 59 | + core.info(`Pull request #${pr.number} is a draft; skipping merge.`); |
| 60 | + return; |
| 61 | + } |
| 62 | +
|
| 63 | + try { |
| 64 | + const res = await github.rest.pulls.merge({ |
| 65 | + owner, |
| 66 | + repo, |
| 67 | + pull_number: pr.number, |
| 68 | + merge_method: 'merge', |
| 69 | + commit_title: head_branch, |
| 70 | + commit_message: '', |
| 71 | + }); |
| 72 | +
|
| 73 | + if (res.data.merged) { |
| 74 | + core.info(`Successfully merged PR #${pr.number} (branch ${head_branch}).`); |
| 75 | + } else { |
| 76 | + core.info(`Merge attempt returned: ${JSON.stringify(res.data)}`); |
| 77 | + } |
| 78 | + } catch (err) { |
| 79 | + core.setFailed(`Failed to merge PR #${pr.number}: ${err}`); |
| 80 | + } |
| 81 | +
|
| 82 | + - name: Done |
| 83 | + run: echo "auto-merge finished" |
0 commit comments