/
index.js
197 lines (168 loc) · 5.51 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
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 });
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);
const allowedModes = ["exactly", "minimum", "maximum"];
if (!allowedModes.includes(mode)) {
await exitWithError(
exitType,
octokit,
shouldAddComment,
`Unknown mode input [${mode}]. Must be one of: ${allowedModes.join(
", "
)}`
);
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(
", "
)}`
);
return;
}
// Fetch the labels 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: github.context.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())
);
}
// Is there an error?
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";
}
// 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);
return;
}
// Remove the comment if it exists
if (shouldAddComment) {
const { data: existing } = await octokit.rest.issues.listComments({
...github.context.repo,
issue_number: github.context.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) {
if (shouldAddComment) {
// Is there an existing comment?
const { data: existing } = await octokit.rest.issues.listComments({
...github.context.repo,
issue_number: github.context.issue.number,
});
const generatedComment = existing.find((c) => c.body.includes(matchToken));
const params = {
...github.context.repo,
issue_number: github.context.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;