-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
183 lines (155 loc) · 7.19 KB
/
action.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
name: 'Label PR based on approvals'
description: 'Adds a label to a PR when it is approved or changes are requested.'
inputs:
token:
description: 'GitHub token to use. If passed, takes precedence over the `app-id` and `app-private-key` inputs.'
app-id:
description: 'The app ID of the app.'
private-key:
description: 'The private key of the app.'
required-approvals:
description: 'Amount of approvals needed. If passed, skips fetching branch protections.'
label-approved:
description: 'Label to add when PR status is approved.'
default: 'approved'
label-changes-requested:
description: 'Label to add when PR status is changes requested.'
default: 'changes requested'
protection-type:
description: 'Type of protection to check for. Can be either "branch-protection" or "rulesets".'
default: 'branch-protection'
runs:
using: composite
steps:
- uses: actions/checkout@v4
- uses: myparcelnl/actions/get-github-token@v4
id: token
with:
token: ${{ inputs.token }}
app-id: ${{ inputs.app-id }}
private-key: ${{ inputs.private-key }}
- name: 'Get required approvals from branch protection'
id: branch-protection
if: inputs.required-approvals == '' && inputs.protection-type == 'branch-protection'
uses: actions/github-script@v7
with:
github-token: ${{ steps.token.outputs.token }}
#language=javascript
script: |
const branchProtection = await github.rest.repos.getBranchProtection({
owner: context.repo.owner,
repo: context.repo.repo,
branch: context.payload.pull_request.base.ref
});
const requiredApprovals = branchProtection.data.required_pull_request_reviews?.required_approving_review_count ?? 0;
core.setOutput('required-approvals', requiredApprovals);
- name: 'Get required approvals from rule sets'
id: rulesets
if: inputs.required-approvals == '' && inputs.protection-type == 'rulesets'
uses: actions/github-script@v7
with:
github-token: ${{ steps.token.outputs.token }}
#language=javascript
script: |
// Get all rules for the branch
// we can use it directly from octokit since the endpoint is not yet supported by github-script
// https://octokit.github.io/rest.js/v20#repos-get-branch-rules
// When this issue is resolved, we can use `github.rest.repos.getBranchRules` to get the rules
// https://github.com/actions/github-script/issues/345
const rulesets = await github.request('/repos/{owner}/{repo}/rules/branches/{branch}', {
owner: context.repo.owner,
repo: context.repo.repo,
branch: context.payload.pull_request.base.ref,
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
});
// Filter out rules that are not for pull requests, Since we get all rules for the branch
const pullRequestRules = rulesets.data.filter(rule => rule.type === 'pull_request');
const highestAmountOfApprovals = pullRequestRules.reduce((highest, rule) => {
const approvals = rule.parameters.required_approving_review_count ?? 0;
return approvals > highest ? approvals : highest;
}, 0);
const requiredApprovals = highestAmountOfApprovals ?? 0;
core.setOutput('required-approvals', requiredApprovals);
- name: 'Collect pull request reviews'
id: check
uses: actions/github-script@v7
with:
github-token: ${{ steps.token.outputs.token }}
#language=javascript
script: |
const reviews = await github.rest.pulls.listReviews({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number
});
// Filter out duplicate reviews from the same user and keep only the last review from each user
const userReviews = new Map();
reviews.data.forEach(review => {
const { login } = review.user;
if (!userReviews.has(login)) {
userReviews.set(login, review);
} else if (userReviews.get(login).submitted_at < review.submitted_at) {
userReviews.set(login, review);
}
});
const userReviewsArray = Array.from(userReviews.values());
const changesRequested = userReviewsArray.some(review => review.state === 'CHANGES_REQUESTED');
const approvals = userReviewsArray.filter(review => review.state === 'APPROVED');
const requiredApprovals = Number(${{ inputs.required-approvals || steps.branch-protection.outputs.required-approvals || steps.rulesets.outputs.required-approvals }});
const isApproved = !changesRequested && approvals.length >= requiredApprovals;
core.setOutput('approved', isApproved ? 'true' : 'false');
core.setOutput('changes-requested', changesRequested ? 'true' : 'false');
- name: 'Add "approved" label'
uses: actions/github-script@v7
if: steps.check.outputs.approved == 'true' && !contains(github.event.pull_request.labels.*.name, inputs.label-approved)
with:
github-token: ${{ steps.token.outputs.token }}
#language=javascript
script: |
github.rest.issues.addLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: ['${{ inputs.label-approved }}']
});
- name: 'Add "changes requested" label'
uses: actions/github-script@v7
if: steps.check.outputs.changes-requested == 'true' && !contains(github.event.pull_request.labels.*.name, inputs.label-changes-requested)
with:
github-token: ${{ steps.token.outputs.token }}
#language=javascript
script: |
github.rest.issues.addLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: ['${{ inputs.label-changes-requested }}']
});
- name: 'Remove "approved" label'
uses: actions/github-script@v7
if: contains(github.event.pull_request.labels.*.name, inputs.label-approved) && (steps.check.outputs.approved == 'false')
with:
github-token: ${{ steps.token.outputs.token }}
#language=javascript
script: |
github.rest.issues.removeLabel({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
name: '${{ inputs.label-approved }}'
});
- name: 'Remove "changes requested" label'
uses: actions/github-script@v7
if: contains(github.event.pull_request.labels.*.name, inputs.label-changes-requested) && (steps.check.outputs.changes-requested == 'false')
with:
github-token: ${{ steps.token.outputs.token }}
#language=javascript
script: |
github.rest.issues.removeLabel({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
name: '${{ inputs.label-changes-requested }}'
});