|
| 1 | +# PRs are only eligible for release if they are merged and have |
| 2 | +# the `release-candidate` label. |
| 3 | +# |
| 4 | +# The only events allowed to trigger this action are: |
| 5 | +# - push (in which case the commit sha is used to find the corresponding |
| 6 | +# PR) |
| 7 | +# - workflow_dispatch (in which case the PR is found from the inputs |
| 8 | +# on the event) |
| 9 | + |
| 10 | +name: PR Check |
| 11 | +description: Check that a PR is eligible for release |
| 12 | + |
| 13 | +outputs: |
| 14 | + message: |
| 15 | + description: The body of the pull request that is being released. |
| 16 | + value: ${{ steps.check_pr.outputs.message }} |
| 17 | + ref: |
| 18 | + description: The ref of the pull request that is being released. |
| 19 | + value: ${{ steps.check_pr.outputs.ref }} |
| 20 | + |
| 21 | +runs: |
| 22 | + using: composite |
| 23 | + steps: |
| 24 | + - name: "Check PR Eligibility" |
| 25 | + id: check_pr |
| 26 | + # 60a0d83039c74a4aee543508d2ffcb1c3799cdea => 'v7' tag as of 2025-07-28 |
| 27 | + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea |
| 28 | + with: |
| 29 | + script: | |
| 30 | + let pr; |
| 31 | +
|
| 32 | + // was this triggered by a push event? |
| 33 | + if (context.eventName == 'push') { |
| 34 | + // if so, we need to find the PR that corresponds to the commit |
| 35 | + // that was pushed. |
| 36 | + // |
| 37 | + // because only maintainers can push to protected branches, |
| 38 | + // we can assume the user has the correct permissions to do |
| 39 | + // this. |
| 40 | + const { data: listing } = await github.rest.repos.listPullRequestsAssociatedWithCommit({ |
| 41 | + owner: context.repo.owner, |
| 42 | + repo: context.repo.repo, |
| 43 | + commit_sha: context.payload.after, |
| 44 | + }); |
| 45 | +
|
| 46 | + if (listing.length == 0) { |
| 47 | + throw new Error(`Workflow aborted: No pull request found for the pushed commit (${context.payload.after}).`); |
| 48 | + } |
| 49 | +
|
| 50 | + const response = await github.rest.pulls.get({ |
| 51 | + owner: context.repo.owner, |
| 52 | + repo: context.repo.repo, |
| 53 | + pull_number: listing[0].number, |
| 54 | + }); |
| 55 | +
|
| 56 | + pr = response.data; |
| 57 | +
|
| 58 | + // if it wasn't triggered by a push event, was it triggered by |
| 59 | + // a workflow_dispatch event? |
| 60 | + } else if (context.eventName == 'workflow_dispatch') { |
| 61 | + // it is technically possible for users with only write access |
| 62 | + // to trigger workflows; we need to make sure that the user |
| 63 | + // who triggered this has either admin or maintain access to the |
| 64 | + // repository. |
| 65 | + const username = context.triggering_actor || context.actor; |
| 66 | +
|
| 67 | + const { data: perms } = await github.rest.repos.getCollaboratorPermissionLevel({ |
| 68 | + owner: context.repo.owner, |
| 69 | + repo: context.repo.repo, |
| 70 | + username, |
| 71 | + }); |
| 72 | +
|
| 73 | + if (perms.role_name !== 'admin' && perms.role_name !== 'maintain') { |
| 74 | + throw new Error(`User ${username} must have 'admin' or 'maintain' role to initiate the release process. (${perms.role_name})`); |
| 75 | + } |
| 76 | +
|
| 77 | + // if so, we grab the PR with the number that was passed in with |
| 78 | + // the inputs. |
| 79 | + const number = context.payload.inputs.pr; |
| 80 | + if (!number) { |
| 81 | + throw new Error('Workflow aborted: No pull request number provided. (need `pr` input)'); |
| 82 | + } |
| 83 | +
|
| 84 | + const response = await github.rest.pulls.get({ |
| 85 | + owner: context.repo.owner, |
| 86 | + repo: context.repo.repo, |
| 87 | + pull_number: number, |
| 88 | + }); |
| 89 | +
|
| 90 | + pr = response.data; |
| 91 | +
|
| 92 | + // workflow was triggered by an unrecognized/unsupported event |
| 93 | + } else { |
| 94 | + throw new Error(`Workflow aborted: Unsupported event type: ${context.eventName}.`); |
| 95 | + } |
| 96 | +
|
| 97 | + if (!pr) { |
| 98 | + throw new Error('No pull request found for the triggered event.'); |
| 99 | + } |
| 100 | +
|
| 101 | + if (!pr.merged) { |
| 102 | + throw new Error('Pull request is not merged.'); |
| 103 | + } |
| 104 | +
|
| 105 | + if (!pr.labels.some(label => label.name == 'release-candidate')) { |
| 106 | + throw new Error('Pull request is not a release candidate.'); |
| 107 | + } |
| 108 | +
|
| 109 | + console.log('body: >>', pr.body, '<<'); |
| 110 | + console.log('ref: >>', pr.merge_commit_sha, '<<'); |
| 111 | +
|
| 112 | + core.setOutput('message', pr.body); |
| 113 | + core.setOutput('ref', pr.merge_commit_sha); |
0 commit comments