/
index.js
111 lines (94 loc) · 3.04 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
const core = require("@actions/core");
const github = require("@actions/github");
async function action() {
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 allowedLabels = core
.getInput("labels", { required: true })
.split(",")
.map((l) => l.trim())
.filter((r) => r);
const exitType = core.getInput("exit_type") || "failure";
const shouldAddComment = core.getInput("add_comment") == "true";
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 = allowedLabels.filter((x) => appliedLabels.includes(x));
// 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 errorMessage = `Label error. Requires ${errorMode} ${count} of: ${allowedLabels.join(
", "
)}. Found: ${appliedLabels.join(", ")}`;
await exitWithError(exitType, octokit, shouldAddComment, errorMessage);
return;
}
core.setOutput("status", "success");
} catch (e) {
core.setFailed(e.message);
}
}
async function exitWithError(exitType, octokit, shouldAddComment, message) {
if (shouldAddComment) {
await octokit.rest.issues.createComment({
...github.context.repo,
issue_number: github.context.issue.number,
body: message,
});
}
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;