forked from mheap/github-action-required-labels
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
159 lines (135 loc) · 4.33 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
const core = require("@actions/core");
const github = require("@actions/github");
const matchToken = `<!-- reqlabelmessage -->`;
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 providedLabels = 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 = providedLabels.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 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;