diff --git a/.github/workflows/merge.yaml b/.github/workflows/merge.yaml index 74998b8..184fd7d 100644 --- a/.github/workflows/merge.yaml +++ b/.github/workflows/merge.yaml @@ -49,19 +49,9 @@ jobs: assignee: "ergebnis-bot" - name: "Approve pull request" - uses: "actions/github-script@v5" + uses: "./.github/actions/github/approve-pull-request" with: github-token: "${{ secrets.ERGEBNIS_BOT_TOKEN }}" - script: | - const pullRequest = context.payload.workflow_run.pull_requests[0]; - const repository = context.repo; - - await github.rest.pulls.createReview({ - event: "APPROVE", - owner: repository.owner, - repo: repository.repo, - pull_number: pullRequest.number, - }); - name: "Merge pull request" uses: "actions/github-script@v5" diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e2cc22..cb34413 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ For a full diff see [`1.2.1...main`][1.2.1...main]. ### Added - Added composite action `github/add-assignee-to-pull-request` ([#59]), by [@localheinz] +- Added composite action `github/approve-pull-request` ([#60]), by [@localheinz] ## [`1.2.1`][1.2.1] @@ -60,5 +61,6 @@ For a full diff see [`1.0.0...main`][1.0.0...main]. [#52]: https://github.com/ergebnis/.github/pull/52 [#54]: https://github.com/ergebnis/.github/pull/54 [#59]: https://github.com/ergebnis/.github/pull/59 +[#60]: https://github.com/ergebnis/.github/pull/60 [@localheinz]: https://github.com/localheinz diff --git a/actions/github/approve-pull-request/action.yaml b/actions/github/approve-pull-request/action.yaml new file mode 100644 index 0000000..1d8d998 --- /dev/null +++ b/actions/github/approve-pull-request/action.yaml @@ -0,0 +1,60 @@ +# https://docs.github.com/en/actions/creating-actions/creating-a-composite-action +# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#inputs +# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runs-for-composite-run-steps-actions +# https://docs.github.com/en/rest/reference/pulls#create-a-review-for-a-pull-request +# https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request +# https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#workflow_run + +name: "Approve pull request" + +description: "Approves a pull request" + +inputs: + github-token: + description: "GitHub token of a user with permission to approve a pull request" + required: true + +runs: + using: "composite" + + steps: + - name: "Determine pull request number" + uses: "actions/github-script@v5" + with: + github-token: "${{ inputs.github-token }}" + script: | + if (context.eventName == 'pull_request') { + core.exportVariable("PULL_REQUEST_NUMBER", context.payload.pull_request.number); + + return; + } + + if (context.eventName == 'workflow_run') { + core.exportVariable("PULL_REQUEST_NUMBER", context.payload.workflow_run.pull_requests[0].number); + + return; + } + + core.setFailed(`Unable to determine the pull request number for event "${context.eventName}"`); + + - name: "Approve pull request" + uses: "actions/github-script@v5" + with: + github-token: "${{ inputs.github-token }}" + script: | + if (!process.env.PULL_REQUEST_NUMBER) { + core.setFailed("The environment variable PULL_REQUEST_NUMBER is not defined.") + + return; + } + + try { + await github.rest.pulls.createReview({ + event: "APPROVE", + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: process.env.PULL_REQUEST_NUMBER, + }); + } catch (error) { + core.setFailed(error.message); + }