-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.cjs
131 lines (109 loc) · 3.63 KB
/
index.cjs
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
const core = require("@actions/core");
const github = require("@actions/github");
const { parseInputTags } = require("./utils.cjs");
async function run() {
try {
// check if this is running on a pull request
if (!github.context.payload.pull_request) {
return core.setOutput("passed", true);
}
const token = core.getInput("githubToken");
const context = github.context;
const octokit = github.getOctokit(token);
const hasSomeInput = core.getInput("hasSome");
const hasAllInput = core.getInput("hasAll");
const hasNoneInput = core.getInput("hasNone");
const hasNotAllInput = core.getInput("hasNotAll");
const hasSomeLabels = parseInputTags(hasSomeInput);
const hasAllLabels = parseInputTags(hasAllInput);
const hasNoneLabels = parseInputTags(hasNoneInput);
const hasNotAllLabels = parseInputTags(hasNotAllInput);
const allowFailureInput = core.getInput("allowFailure");
const allowFailure = allowFailureInput === "true";
const failMessages = [];
const { data: labelsOnIssue } = await octokit.issues.listLabelsOnIssue({
...context.repo,
issue_number: context.payload.pull_request.number,
});
const prLabels = labelsOnIssue.map((item) => item.name);
const hasSomeResult =
!hasSomeInput || hasSomeLabels.some((label) => prLabels.includes(label));
const hasAllResult =
!hasAllInput || hasAllLabels.every((label) => prLabels.includes(label));
const hasNoneResult =
!hasNoneInput ||
hasNoneLabels.every((label) => !prLabels.includes(label));
const hasNotAllResult =
!hasNotAllInput ||
hasNotAllLabels.some((label) => !prLabels.includes(label));
if (!hasSomeResult) {
failMessages.push(
`The PR needs to have at least one of the following labels to pass this check: ${hasSomeLabels.join(
", "
)}`
);
}
if (!hasAllResult) {
failMessages.push(
`The PR needs to have all of the following labels to pass this check: ${hasAllLabels.join(
", "
)}`
);
}
if (!hasNoneResult) {
failMessages.push(
`The PR needs to have none of the following labels to pass this check: ${hasNoneLabels.join(
", "
)}`
);
}
if (!hasNotAllResult) {
failMessages.push(
`The PR needs to not have at least one of the following labels to pass this check: ${hasNotAllLabels.join(
", "
)}`
);
}
const checks = await octokit.checks.listForRef({
...context.repo,
ref: context.payload.pull_request.head.ref,
});
const checkRunIds = checks.data.check_runs
.filter((check) => check.name === context.job)
.map((check) => check.id);
if (failMessages.length) {
// update old checks
for (const id of checkRunIds) {
await octokit.checks.update({
...context.repo,
check_run_id: id,
conclusion: allowFailure ? "success" : "failure",
output: {
title: "Labels did not pass provided rules",
summary: failMessages.join(". "),
},
});
}
if (!allowFailure) {
core.setFailed(failMessages.join(". "));
}
} else {
// update old checks
for (const id of checkRunIds) {
await octokit.checks.update({
...context.repo,
check_run_id: id,
conclusion: "success",
output: {
title: "Labels follow all the provided rules",
summary: "",
},
});
}
core.setOutput("passed", true);
}
} catch (error) {
core.setFailed(error.message);
}
}
run();