|
| 1 | +# https://docs.github.com/en/actions/creating-actions/creating-a-composite-action |
| 2 | +# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#inputs |
| 3 | +# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runs-for-composite-run-steps-actions |
| 4 | +# https://docs.github.com/en/rest/reference/pulls#create-a-review-for-a-pull-request |
| 5 | +# https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request |
| 6 | +# https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#workflow_run |
| 7 | + |
| 8 | +name: "Approve pull request" |
| 9 | + |
| 10 | +description: "Approves a pull request" |
| 11 | + |
| 12 | +inputs: |
| 13 | + github-token: |
| 14 | + description: "GitHub token of a user with permission to approve a pull request" |
| 15 | + required: true |
| 16 | + |
| 17 | +runs: |
| 18 | + using: "composite" |
| 19 | + |
| 20 | + steps: |
| 21 | + - name: "Determine pull request number" |
| 22 | + uses: "actions/github-script@v5" |
| 23 | + with: |
| 24 | + github-token: "${{ inputs.github-token }}" |
| 25 | + script: | |
| 26 | + if (context.eventName == 'pull_request') { |
| 27 | + core.exportVariable("PULL_REQUEST_NUMBER", context.payload.pull_request.number); |
| 28 | +
|
| 29 | + return; |
| 30 | + } |
| 31 | +
|
| 32 | + if (context.eventName == 'workflow_run') { |
| 33 | + core.exportVariable("PULL_REQUEST_NUMBER", context.payload.workflow_run.pull_requests[0].number); |
| 34 | +
|
| 35 | + return; |
| 36 | + } |
| 37 | +
|
| 38 | + core.setFailed(`Unable to determine the pull request number for event "${context.eventName}"`); |
| 39 | +
|
| 40 | + - name: "Approve pull request" |
| 41 | + uses: "actions/github-script@v5" |
| 42 | + with: |
| 43 | + github-token: "${{ inputs.github-token }}" |
| 44 | + script: | |
| 45 | + if (!process.env.PULL_REQUEST_NUMBER) { |
| 46 | + core.setFailed("The environment variable PULL_REQUEST_NUMBER is not defined.") |
| 47 | +
|
| 48 | + return; |
| 49 | + } |
| 50 | +
|
| 51 | + try { |
| 52 | + await github.rest.pulls.createReview({ |
| 53 | + event: "APPROVE", |
| 54 | + owner: context.repo.owner, |
| 55 | + repo: context.repo.repo, |
| 56 | + pull_number: process.env.PULL_REQUEST_NUMBER, |
| 57 | + }); |
| 58 | + } catch (error) { |
| 59 | + core.setFailed(error.message); |
| 60 | + } |
0 commit comments