Skip to content

Commit f8099a8

Browse files
authored
Merge pull request #83 from wayofdev/feat/auto-approve-action
feat: composite actions to work with github PRs
2 parents a6bf8fb + d769688 commit f8099a8

File tree

10 files changed

+293
-9
lines changed

10 files changed

+293
-9
lines changed

.github/workflows/build-image.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jobs:
4949
runs-on: ${{ inputs.os }}
5050
steps:
5151
- name: 📦 Check out the codebase
52-
uses: actions/checkout@v4
52+
uses: actions/checkout@v4.1.7
5353

5454
- name: 🛠️ Install goss and dgoss
5555
uses: e1himself/goss-installation-action@v1.2.1

.github/workflows/create-changesets-release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131

3232
steps:
3333
- name: 📦 Check out the codebase
34-
uses: actions/checkout@v4
34+
uses: actions/checkout@v4.1.7
3535
with:
3636
fetch-depth: 0
3737

.github/workflows/integrate.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ jobs:
1919
pull-requests: read
2020
steps:
2121
- name: 📦 Check out the codebase
22-
uses: actions/checkout@v4.1.5
22+
uses: actions/checkout@v4.1.7
2323

2424
- name: 🧐 Lint commits using "commitlint"
25-
uses: wagoid/commitlint-github-action@v6.0.1
25+
uses: wagoid/commitlint-github-action@v6.1.1
2626
with:
2727
configFile: ${{ github.workspace }}/.github/.commitlint.config.mjs
2828
failOnWarnings: false
@@ -37,7 +37,7 @@ jobs:
3737
group: coding-standards-${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
3838
steps:
3939
- name: 📦 Check out the codebase
40-
uses: actions/checkout@v4.1.5
40+
uses: actions/checkout@v4.1.7
4141

4242
- name: 🧐 Lint YAML files
4343
uses: ibiqlik/action-yamllint@v3.1.1
@@ -54,7 +54,7 @@ jobs:
5454
group: markdown-linting-${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
5555
steps:
5656
- name: 📦 Check out the codebase
57-
uses: actions/checkout@v4.1.5
57+
uses: actions/checkout@v4.1.7
5858

5959
- name: 🧐 Lint Markdown files
6060
uses: DavidAnson/markdownlint-cli2-action@v16.0.0

.github/workflows/shellcheck.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
runs-on: ${{ inputs.os }}
2626
steps:
2727
- name: 📦 Check out the codebase
28-
uses: actions/checkout@v4
28+
uses: actions/checkout@v4.1.7
2929
with:
3030
fetch-depth: 0
3131

.github/workflows/triage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ name: 🏷️ Add labels
1616

1717
jobs:
1818
label:
19-
uses: wayofdev/gh-actions/.github/workflows/apply-labels.yml@v3.1.0
19+
uses: wayofdev/gh-actions/.github/workflows/apply-labels.yml@v3.1.1
2020
with:
2121
os: ubuntu-latest
2222
secrets:

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ repos:
1010
- id: fix-encoding-pragma
1111

1212
- repo: https://github.com/commitizen-tools/commitizen
13-
rev: v3.24.0
13+
rev: v3.29.0
1414
hooks:
1515
- id: commitizen
1616
stages:
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
3+
# https://docs.github.com/en/actions/creating-actions/creating-a-composite-action
4+
# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#inputs
5+
# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runs-for-composite-run-steps-actions
6+
# https://docs.github.com/en/rest/issues/assignees#add-assignees-to-an-issue
7+
# https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request
8+
# https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#workflow_run
9+
10+
name: 🙋‍♂️ Add assignee to pull request
11+
12+
description: Adds an assignee to a pull request
13+
14+
inputs:
15+
assignee:
16+
description: Username of user to add as an assignee to a pull request
17+
required: true
18+
github-token:
19+
description: GitHub token of a user with permission to add assignees to a pull request
20+
required: true
21+
22+
runs:
23+
using: 'composite'
24+
25+
steps:
26+
- name: 🤔 Determine pull request number
27+
uses: actions/github-script@v7.0.1
28+
with:
29+
github-token: "${{ inputs.github-token }}"
30+
script: |
31+
if (
32+
context.eventName == 'pull_request' ||
33+
context.eventName == 'pull_request_target'
34+
) {
35+
core.exportVariable("PULL_REQUEST_NUMBER", context.payload.pull_request.number);
36+
37+
return;
38+
}
39+
40+
if (context.eventName == 'workflow_run') {
41+
core.exportVariable("PULL_REQUEST_NUMBER", context.payload.workflow_run.pull_requests[0].number);
42+
43+
return;
44+
}
45+
46+
core.setFailed(`Unable to determine the pull request number for event "${context.eventName}"`);
47+
48+
- name: 🙋‍♂️ Add assignee to pull request
49+
uses: actions/github-script@v7.0.1
50+
env:
51+
ASSIGNEE: "${{ inputs.assignee }}"
52+
with:
53+
github-token: "${{ inputs.github-token }}"
54+
script: |
55+
if (!process.env.PULL_REQUEST_NUMBER) {
56+
core.setFailed("The environment variable PULL_REQUEST_NUMBER is not defined.")
57+
58+
return;
59+
}
60+
61+
const assignees = [
62+
process.env.ASSIGNEE,
63+
];
64+
65+
try {
66+
await github.rest.issues.addAssignees({
67+
assignees: assignees,
68+
issue_number: process.env.PULL_REQUEST_NUMBER,
69+
owner: context.repo.owner,
70+
repo: context.repo.repo,
71+
});
72+
} catch (error) {
73+
core.setFailed(error.message);
74+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
3+
# https://docs.github.com/en/actions/creating-actions/creating-a-composite-action
4+
# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#inputs
5+
# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runs-for-composite-run-steps-actions
6+
# https://docs.github.com/en/rest/pulls/reviews#create-a-review-for-a-pull-request
7+
# https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request
8+
# https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#workflow_run
9+
10+
name: ✅ Approve pull request
11+
12+
description: Approves a pull request
13+
14+
inputs:
15+
github-token:
16+
description: GitHub token of a user with permission to approve a pull request
17+
required: true
18+
19+
runs:
20+
using: 'composite'
21+
22+
steps:
23+
- name: 🤔 Determine pull request number
24+
uses: actions/github-script@v7.0.1
25+
with:
26+
github-token: "${{ inputs.github-token }}"
27+
script: |
28+
if (
29+
context.eventName == 'pull_request' ||
30+
context.eventName == 'pull_request_target'
31+
) {
32+
core.exportVariable("PULL_REQUEST_NUMBER", context.payload.pull_request.number);
33+
34+
return;
35+
}
36+
37+
if (context.eventName == 'workflow_run') {
38+
core.exportVariable("PULL_REQUEST_NUMBER", context.payload.workflow_run.pull_requests[0].number);
39+
40+
return;
41+
}
42+
43+
core.setFailed(`Unable to determine the pull request number for event "${context.eventName}"`);
44+
45+
- name: ✅ Approve pull request
46+
uses: actions/github-script@v7.0.1
47+
with:
48+
github-token: "${{ inputs.github-token }}"
49+
script: |
50+
if (!process.env.PULL_REQUEST_NUMBER) {
51+
core.setFailed("The environment variable PULL_REQUEST_NUMBER is not defined.");
52+
53+
return;
54+
}
55+
56+
try {
57+
await github.rest.pulls.createReview({
58+
event: "APPROVE",
59+
owner: context.repo.owner,
60+
repo: context.repo.repo,
61+
pull_number: process.env.PULL_REQUEST_NUMBER,
62+
});
63+
} catch (error) {
64+
core.setFailed(error.message);
65+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
3+
# https://docs.github.com/en/actions/creating-actions/creating-a-composite-action
4+
# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#inputs
5+
# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runs-for-composite-run-steps-actions
6+
# https://docs.github.com/en/rest/pulls/pulls#merge-a-pull-request
7+
# https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request
8+
# https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#workflow_run
9+
10+
name: 📥 Merge pull request
11+
12+
description: Merges a pull request
13+
14+
inputs:
15+
github-token:
16+
description: GitHub token of a user with permission to merge a pull request
17+
required: true
18+
merge-method:
19+
default: merge
20+
description: 'Which merge method to use, one "merge", "rebase", "squash"'
21+
required: true
22+
23+
runs:
24+
using: 'composite'
25+
26+
steps:
27+
- name: 🤔 Determine pull request number
28+
uses: actions/github-script@v7.0.1
29+
with:
30+
github-token: "${{ inputs.github-token }}"
31+
script: |
32+
if (
33+
context.eventName == 'pull_request' ||
34+
context.eventName == 'pull_request_target'
35+
) {
36+
core.exportVariable("PULL_REQUEST_NUMBER", context.payload.pull_request.number);
37+
38+
return;
39+
}
40+
41+
if (context.eventName == 'workflow_run') {
42+
core.exportVariable("PULL_REQUEST_NUMBER", context.payload.workflow_run.pull_requests[0].number);
43+
44+
return;
45+
}
46+
47+
core.setFailed(`Unable to determine the pull request number for event "${context.eventName}"`);
48+
49+
- name: 📥 Merge pull request
50+
uses: actions/github-script@v7.0.1
51+
env:
52+
MERGE_METHOD: "${{ inputs.merge-method }}"
53+
with:
54+
github-token: "${{ inputs.github-token }}"
55+
script: |
56+
if (!process.env.PULL_REQUEST_NUMBER) {
57+
core.setFailed("The environment variable PULL_REQUEST_NUMBER is not defined.");
58+
59+
return;
60+
}
61+
62+
try {
63+
await github.rest.pulls.merge({
64+
merge_method: process.env.MERGE_METHOD,
65+
owner: context.repo.owner,
66+
pull_number: process.env.PULL_REQUEST_NUMBER,
67+
repo: context.repo.repo,
68+
});
69+
} catch (error) {
70+
core.setFailed(error.message);
71+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
3+
# https://docs.github.com/en/actions/creating-actions/creating-a-composite-action
4+
# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#inputs
5+
# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runs-for-composite-run-steps-actions
6+
# https://docs.github.com/en/rest/pulls/review-requests#request-reviewers-for-a-pull-request
7+
# https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request
8+
# https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#workflow_run
9+
10+
name: Request review from reviewer for pull request
11+
12+
description: Requests a review from a reviewer for a pull request
13+
14+
inputs:
15+
github-token:
16+
description: GitHub token of a user with permission to request reviewers for a pull request
17+
required: true
18+
reviewer:
19+
description: Username of user to request review from for a pull request
20+
required: true
21+
22+
runs:
23+
using: 'composite'
24+
25+
steps:
26+
- name: 🤔 Determine pull request number
27+
uses: actions/github-script@v7.0.1
28+
with:
29+
github-token: "${{ inputs.github-token }}"
30+
script: |
31+
if (
32+
context.eventName == 'pull_request' ||
33+
context.eventName == 'pull_request_target'
34+
) {
35+
core.exportVariable("PULL_REQUEST_NUMBER", context.payload.pull_request.number);
36+
37+
return;
38+
}
39+
40+
if (context.eventName == 'workflow_run') {
41+
core.exportVariable("PULL_REQUEST_NUMBER", context.payload.workflow_run.pull_requests[0].number);
42+
43+
return;
44+
}
45+
46+
core.setFailed(`Unable to determine the pull request number for event "${context.eventName}"`);
47+
48+
- name: "Request reviewer"
49+
uses: "actions/github-script@v7.0.1"
50+
env:
51+
REVIEWER: "${{ inputs.reviewer }}"
52+
with:
53+
github-token: "${{ inputs.github-token }}"
54+
script: |
55+
if (!process.env.PULL_REQUEST_NUMBER) {
56+
core.setFailed("The environment variable PULL_REQUEST_NUMBER is not defined.");
57+
58+
return;
59+
}
60+
61+
const reviewers = [
62+
process.env.REVIEWER,
63+
];
64+
65+
try {
66+
await github.rest.pulls.requestReviewers({
67+
owner: context.repo.owner,
68+
pull_number: process.env.PULL_REQUEST_NUMBER,
69+
repo: context.repo.repo,
70+
reviewers: reviewers,
71+
});
72+
} catch (error) {
73+
core.setFailed(error.message);
74+
}

0 commit comments

Comments
 (0)