-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathindex.js
224 lines (193 loc) · 6.18 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
const core = require("@actions/core");
const github = require("@actions/github");
let matchToken;
async function action() {
// Use a guaranteed-unique (but persistent) string to match "our" comment
// https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
const matchTokenId = [
process.env.GITHUB_WORKFLOW,
process.env.GITHUB_JOB,
process.env.GITHUB_ACTION,
].join("/");
matchToken = `<!-- ${matchTokenId} -->\n`;
try {
const token = core.getInput("token", { required: true });
const octokit = github.getOctokit(token);
// Process inputs for use later
const mode = core.getInput("mode", { required: true });
const count = parseInt(core.getInput("count", { required: true }), 10);
const exitType = core.getInput("exit_type") || "failure";
const shouldAddComment = core.getInput("add_comment") == "true";
const labelsAreRegex = core.getInput("use_regex") == "true";
let providedLabels = core.getInput("labels", { required: true });
core.debug(`gather labels: ${providedLabels}`);
if (labelsAreRegex) {
// If labels are regex they must be provided as new line delimited
providedLabels = providedLabels.split("\n");
} else {
// Comma separated are allowed for exact string matches
// This may be removed in the next major version
providedLabels = providedLabels
.split("\n")
.join(",")
.split(",")
.map((l) => l.trim());
}
// Remove any empty labels
providedLabels = providedLabels.filter((r) => r);
let issue_number = github.context.issue.number;
if (github.context.eventName === "merge_group" && !issue_number) {
// Parse out of the ref for merge queue
// e.g. refs/heads/gh-readonly-queue/main/pr-17-a3c310584587d4b97c2df0cb46fe050cc46a15d6
const lastPart = github.context.ref.split("/").pop();
issue_number = lastPart.match(/pr-(\d+)-/)[1];
core.info(
`merge_group event detected and issue_number parsed as ${issue_number}`,
);
}
const allowedModes = ["exactly", "minimum", "maximum"];
if (!allowedModes.includes(mode)) {
await exitWithError(
exitType,
octokit,
shouldAddComment,
`Unknown mode input [${mode}]. Must be one of: ${allowedModes.join(
", ",
)}`,
issue_number,
);
return;
}
const allowedExitCodes = ["success", "failure"];
if (!allowedExitCodes.includes(exitType)) {
await exitWithError(
exitType,
octokit,
shouldAddComment,
`Unknown exit_code input [${exitType}]. Must be one of: ${allowedExitCodes.join(
", ",
)}`,
issue_number,
);
return;
}
core.debug(`fetch the labels for ${issue_number} using the API`);
// We use the API rather than read event.json in case earlier steps
// added a label
const labels = (
await octokit.rest.issues.listLabelsOnIssue({
...github.context.repo,
issue_number,
})
).data;
const appliedLabels = labels.map((label) => label.name);
// How many labels overlap?
let intersection = [];
if (labelsAreRegex) {
intersection = appliedLabels.filter((appliedLabel) =>
providedLabels.some((providedLabel) =>
new RegExp(providedLabel, "i").test(appliedLabel),
),
);
} else {
const lowerCasedAppliedLabels = appliedLabels.map((label) =>
label.toLowerCase(),
);
intersection = providedLabels.filter((x) =>
lowerCasedAppliedLabels.includes(x.toLowerCase()),
);
}
core.debug(`detect errors...`);
let errorMode;
if (mode === "exactly" && intersection.length !== count) {
errorMode = "exactly";
} else if (mode === "minimum" && intersection.length < count) {
errorMode = "at least";
} else if (mode === "maximum" && intersection.length > count) {
errorMode = "at most";
}
core.debug(`if so, add a comment (if enabled) and fail the run...`);
if (errorMode !== undefined) {
const comment = core.getInput("message");
const errorMessage = tmpl(comment, {
mode,
count,
errorString: errorMode,
provided: providedLabels.join(", "),
applied: appliedLabels.join(", "),
});
await exitWithError(
exitType,
octokit,
shouldAddComment,
errorMessage,
issue_number,
);
return;
}
core.debug(`remove the comment if it exists...`);
if (shouldAddComment) {
const { data: existing } = await octokit.rest.issues.listComments({
...github.context.repo,
issue_number: issue_number,
});
const generatedComment = existing.find((c) =>
c.body.includes(matchToken),
);
if (generatedComment) {
await octokit.rest.issues.deleteComment({
...github.context.repo,
comment_id: generatedComment.id,
});
}
}
core.setOutput("labels", intersection.join(","));
core.setOutput("status", "success");
} catch (e) {
core.setFailed(e.message);
}
}
function tmpl(t, o) {
return t.replace(/\{\{\s*(.*?)\s*\}\}/g, function (item, param) {
return o[param];
});
}
async function exitWithError(
exitType,
octokit,
shouldAddComment,
message,
issue_number,
) {
if (shouldAddComment) {
// Is there an existing comment?
const { data: existing } = await octokit.rest.issues.listComments({
...github.context.repo,
issue_number: issue_number,
});
const generatedComment = existing.find((c) => c.body.includes(matchToken));
const params = {
...github.context.repo,
issue_number: issue_number,
body: `${matchToken}${message}`,
};
// If so, update it
let method = "createComment";
if (generatedComment) {
method = "updateComment";
params.comment_id = generatedComment.id;
}
await octokit.rest.issues[method](params);
}
core.setOutput("status", "failure");
if (exitType === "success") {
core.warning(message);
return;
}
core.setFailed(message);
}
/* istanbul ignore next */
if (require.main === module) {
action();
}
module.exports = action;