forked from dulong41/label-when-approved-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
226 lines (197 loc) · 7.27 KB
/
index.js
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import { info, setOutput, getInput, warning, setFailed } from '@actions/core';
import { getOctokit, context as _context } from '@actions/github';
function getRequiredEnv(key) {
const value = process.env[key];
if (value === undefined) {
const message = `${key} was not defined.`;
throw new Error(message);
}
return value;
}
function verboseOutput(name, value) {
info(`Setting output: ${name}: ${value}`);
setOutput(name, value);
}
async function setLabel(octokit, owner, repo, pullRequestNumber, label) {
info(`Setting label "${label}"`);
await octokit.request('POST /repos/{owner}/{repo}/issues/{issue_number}/labels', {
owner,
repo,
issue_number: pullRequestNumber,
labels: [label]
});
}
async function addComment(octokit, owner, repo, pullRequestNumber, comment) {
info(`Adding comment "${comment}"`);
await octokit.request('POST /repos/{owner}/{repo}/issues/{issue_number}/comments', {
owner,
repo,
issue_number: pullRequestNumber,
body: comment
});
}
async function removeLabel(octokit, owner, repo, pullRequestNumber, label) {
info(`Removing label "${label}"`);
await octokit.request('DELETE /repos/{owner}/{repo}/issues/{issue_number}/labels/{name}', {
owner,
repo,
issue_number: pullRequestNumber,
name: label
});
}
async function processReviews(reviews, committers, requireCommittersApproval, numOfApprovals) {
const approved = [];
const changesRequested = [];
let isApproved = false;
const reviewStates = {};
for (const review of reviews) {
if (review.state === 'APPROVED' || review.state === 'CHANGES_REQUESTED') {
if (requireCommittersApproval && committers.includes(review.user.login)) {
reviewStates[review.user.login] = review.state;
} else if (!requireCommittersApproval) {
reviewStates[review.user.login] = review.state;
}
}
}
info('Reviews:');
for (const user in reviewStates) {
info(`\t${user}: ${reviewStates[user].toLowerCase()}`);
}
for (const user in reviewStates) {
if (reviewStates[user] === 'APPROVED') {
approved.push(user);
} else if (reviewStates[user] === 'CHANGES_REQUESTED') {
changesRequested.push(user);
}
}
// Did we get enough reviews?
if (approved.length >= numOfApprovals) {
isApproved = true;
}
// Are there changes requested?
if (changesRequested.length > 0) {
isApproved = false;
}
// Are we good to go?
return isApproved;
}
async function getReviews(octokit, owner, repo, pullRequestNumber, requireCommittersApproval) {
const { data: reviews } = await octokit.request('GET /repos/{owner}/{repo}/pulls/{pull_number}/reviews', {
owner,
repo,
pull_number: pullRequestNumber
});
const reviewers = reviews && reviews.length > 0 ? reviews.map((review) => review.user.login) : [];
const reviewersAlreadyChecked = [];
const committers = [];
if (requireCommittersApproval) {
info(`\nChecking reviewers permissions`);
for (const reviewer of reviewers) {
if (!reviewersAlreadyChecked.includes(reviewer)) {
const r = await octokit.request('GET /repos/{owner}/{repo}/collaborators/{username}/permission', {
owner,
repo,
username: reviewer
});
if (r.data.permission === 'admin' || r.data.permission === 'write') {
committers.push(reviewer);
}
info(`\t${reviewer}: ${r.data.permission}`);
reviewersAlreadyChecked.push(reviewer);
}
}
}
return { reviews, committers };
}
async function run() {
const token = getInput('token', { required: true });
const userLabel = getInput('label') || 'not set';
const requireCommittersApproval = getInput('require_committers_approval') === 'true';
const removeLabelWhenApprovalMissing = getInput('remove_label_when_approval_missing') === 'true';
const comment = getInput('comment') || '';
const pullRequestNumberInput = getInput('pullRequestNumber') || 'not set';
const numOfApprovals = parseInt(getInput('numOfApprovals') || 1);
const octokit = getOctokit(token);
const context = _context;
const repository = getRequiredEnv('GITHUB_REPOSITORY');
const eventName = getRequiredEnv('GITHUB_EVENT_NAME');
const [owner, repo] = repository.split('/');
let pullRequestNumber;
//
try {
info(
`\n############### Set Label When Approved Begin ##################\n` +
`label: "${userLabel}"\n` +
`requireCommittersApproval: ${requireCommittersApproval}\n` +
`comment: ${comment}\n` +
`pullRequestNumber: ${pullRequestNumberInput}\n` +
`numOfApprovals: ${numOfApprovals}\n`
);
// Workflow event setup
if (eventName === 'pull_request_review') {
pullRequestNumber = context.payload.pull_request ? context.payload.pull_request.number : undefined;
if (pullRequestNumber === undefined) {
throw Error('Could not find PR number from context, exiting');
}
} else if (eventName === 'workflow_run') {
if (pullRequestNumberInput === 'not set') {
warning(
`If action is triggered by "workflow_run" then input "pullRequestNumber" is required.\n` +
`It might be missing because the pull request might have been already merged or a fixup pushed to` +
`the PR branch. None of the outputs will be set as we cannot find the right PR.`
);
return;
} else {
pullRequestNumber = parseInt(pullRequestNumberInput);
}
} else {
throw Error(
`This action is only useful in "pull_request_review" or "workflow_run" triggered runs and you used it in "${eventName}"`
);
}
// Get the PR
const { data: pullRequest } = await octokit.request('GET /repos/{owner}/{repo}/pulls/{pull_number}', {
owner,
repo,
pull_number: pullRequestNumber
});
// Get the labels
info('Grabbing labels');
const labelNames = pullRequest.labels.map((label) => label.name);
// Get the reviews
info('Grabbing reviews');
const { reviews, committers } = await getReviews(
octokit,
owner,
repo,
pullRequest.number,
requireCommittersApproval
);
// Check if the PR is approved
const isApproved = await processReviews(reviews, committers, requireCommittersApproval, numOfApprovals);
// Add or remove the label
let shouldLabelBeSet = false;
let shouldLabelBeRemoved = false;
if (userLabel !== 'not set') {
shouldLabelBeSet = isApproved && !labelNames.includes(userLabel);
shouldLabelBeRemoved = !isApproved && labelNames.includes(userLabel) && removeLabelWhenApprovalMissing;
if (shouldLabelBeSet) {
await setLabel(octokit, owner, repo, pullRequest.number, userLabel);
if (comment !== '') {
await addComment(octokit, owner, repo, pullRequest.number, comment);
}
} else if (shouldLabelBeRemoved) {
await removeLabel(octokit, owner, repo, pullRequest.number, userLabel);
}
}
// Set outputs
verboseOutput('isApproved', String(isApproved));
verboseOutput('shouldLabelBeSet', String(shouldLabelBeSet));
verboseOutput('shouldLabelBeRemoved', String(shouldLabelBeRemoved));
} catch (error) {
setFailed(error.message);
}
}
run()
.then(() => info('\n############### Set Label When Approved End ##################\n'))
.catch((e) => setFailed(e.message));