Skip to content

Commit ce84025

Browse files
committed
Enhancement: Add github/approve-pull-request action
1 parent 3287c36 commit ce84025

File tree

3 files changed

+63
-11
lines changed

3 files changed

+63
-11
lines changed

.github/workflows/merge.yaml

+1-11
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,9 @@ jobs:
4949
assignee: "ergebnis-bot"
5050

5151
- name: "Approve pull request"
52-
uses: "actions/github-script@v5"
52+
uses: "./.github/actions/github/approve-pull-request"
5353
with:
5454
github-token: "${{ secrets.ERGEBNIS_BOT_TOKEN }}"
55-
script: |
56-
const pullRequest = context.payload.workflow_run.pull_requests[0];
57-
const repository = context.repo;
58-
59-
await github.rest.pulls.createReview({
60-
event: "APPROVE",
61-
owner: repository.owner,
62-
repo: repository.repo,
63-
pull_number: pullRequest.number,
64-
});
6555

6656
- name: "Merge pull request"
6757
uses: "actions/github-script@v5"

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ For a full diff see [`1.2.1...main`][1.2.1...main].
1111
### Added
1212

1313
- Added composite action `github/add-assignee-to-pull-request` ([#59]), by [@localheinz]
14+
- Added composite action `github/approve-pull-request` ([#60]), by [@localheinz]
1415

1516
## [`1.2.1`][1.2.1]
1617

@@ -60,5 +61,6 @@ For a full diff see [`1.0.0...main`][1.0.0...main].
6061
[#52]: https://github.com/ergebnis/.github/pull/52
6162
[#54]: https://github.com/ergebnis/.github/pull/54
6263
[#59]: https://github.com/ergebnis/.github/pull/59
64+
[#60]: https://github.com/ergebnis/.github/pull/60
6365

6466
[@localheinz]: https://github.com/localheinz
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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

Comments
 (0)