|
| 1 | +name: Check merge queue |
| 2 | +description: Check whether a pull request can skip the merge queue when it's |
| 3 | + up-to-date with the base branch. |
| 4 | + |
| 5 | +inputs: |
| 6 | + head-ref: |
| 7 | + description: The name of the head ref (the pull request branch). |
| 8 | + required: true |
| 9 | + default: ${{ github.event.merge_group.head_ref }} |
| 10 | + |
| 11 | + base-ref: |
| 12 | + description: The name of the base ref (e.g., main, master). |
| 13 | + required: true |
| 14 | + default: main |
| 15 | + |
| 16 | + github-token: |
| 17 | + description: The GitHub token to use for authentication. |
| 18 | + required: true |
| 19 | + default: ${{ github.token }} |
| 20 | + |
| 21 | +outputs: |
| 22 | + up-to-date: |
| 23 | + value: ${{ steps.up-to-date.outputs.up-to-date }} |
| 24 | + description: Whether the pull request is up-to-date with the base branch |
| 25 | + and is first in the merge queue, allowing it to skip the merge queue. |
| 26 | + |
| 27 | +runs: |
| 28 | + using: composite |
| 29 | + steps: |
| 30 | + - name: Checkout repository |
| 31 | + uses: actions/checkout@v6 |
| 32 | + with: |
| 33 | + fetch-depth: 0 |
| 34 | + |
| 35 | + - name: Get pull request details |
| 36 | + id: pr-details |
| 37 | + uses: actions/github-script@v8 |
| 38 | + env: |
| 39 | + HEAD_REF: ${{ inputs.head-ref }} |
| 40 | + with: |
| 41 | + github-token: ${{ inputs.github-token }} |
| 42 | + script: | |
| 43 | + const { HEAD_REF } = process.env; |
| 44 | + const match = HEAD_REF.match(/\/pr-([0-9]+)-/u); |
| 45 | + if (!match) { |
| 46 | + return core.setFailed(`Could not extract pull request number from head ref: "${HEAD_REF}".`); |
| 47 | + } |
| 48 | +
|
| 49 | + const number = parseInt(match[1], 10); |
| 50 | + const result = await github.graphql(` |
| 51 | + query($owner: String!, $name: String!, $number: Int!) { |
| 52 | + repository(owner: $owner, name: $name) { |
| 53 | + pullRequest(number: $number) { |
| 54 | + headRefName |
| 55 | + mergeQueueEntry { position } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + `, { |
| 60 | + owner: context.repo.owner, |
| 61 | + name: context.repo.repo, |
| 62 | + number, |
| 63 | + }); |
| 64 | +
|
| 65 | + if (!result.repository.pullRequest) { |
| 66 | + return core.setFailed(`Pull request #${number} not found in repository "${context.repo.owner}/${context.repo.repo}".`); |
| 67 | + } |
| 68 | +
|
| 69 | + const position = result.repository.pullRequest.mergeQueueEntry?.position; |
| 70 | + if (!position) { |
| 71 | + return core.setFailed(`Pull request #${number} is not in the merge queue.`); |
| 72 | + } |
| 73 | +
|
| 74 | + core.setOutput('pr-number', number); |
| 75 | + core.setOutput('pr-branch', result.repository.pullRequest.headRefName); |
| 76 | + core.setOutput('merge-queue-position', position); |
| 77 | +
|
| 78 | + - name: Check if pull request is up-to-date with base branch |
| 79 | + id: up-to-date |
| 80 | + shell: bash |
| 81 | + env: |
| 82 | + BASE_REF: ${{ inputs.base-ref }} |
| 83 | + PR_BRANCH: ${{ steps.pr-details.outputs.pr-branch }} |
| 84 | + MERGE_QUEUE_POSITION: ${{ steps.pr-details.outputs.merge-queue-position }} |
| 85 | + run: | |
| 86 | + set -euo pipefail |
| 87 | + if git merge-base --is-ancestor "origin/${BASE_REF}" "origin/${PR_BRANCH}" && [ "${MERGE_QUEUE_POSITION}" -eq 1 ]; then |
| 88 | + echo "up-to-date=true" >> "$GITHUB_OUTPUT" |
| 89 | + else |
| 90 | + echo "up-to-date=false" >> "$GITHUB_OUTPUT" |
| 91 | + fi |
0 commit comments