-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathindex.js
More file actions
181 lines (145 loc) · 5.21 KB
/
index.js
File metadata and controls
181 lines (145 loc) · 5.21 KB
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
// @ts-check
const core = require("@actions/core");
const github = require("@actions/github");
const TASK_LIST_ITEM = /(?:^|\n)\s*-\s+\[(?<checkMark>[ xX])\]\s+(?<text>(?!~).*)/g;
const COMMENT_START = "<!--";
const COMMENT_END = "-->";
const RADIO_TAG_ITEM = /<!-- TaskRadio (?<radioTag>\S+) -->/g
async function action() {
const bodyList = [];
const token = core.getInput("token");
const octokit = github.getOctokit(token);
const skipRegexPattern = core.getInput("skipDescriptionRegex");
const skipRegexFlags = core.getInput("skipDescriptionRegexFlags");
const skipDescriptionRegex = !!skipRegexPattern ? new RegExp(skipRegexPattern, skipRegexFlags) : false;
const issueNumber =
parseInt(core.getInput("issueNumber")) || github.context.issue?.number;
core.debug(`issue number: ${issueNumber}`);
if (!issueNumber) {
core.setFailed("Could not determine issue number");
return;
}
const { data: issue } = await octokit.rest.issues.get({
...github.context.repo,
issue_number: issueNumber,
});
if (issue.body) {
bodyList.push(issue.body);
}
if (core.getInput("skipComments") != "true") {
const { data: comments } = await octokit.rest.issues.listComments({
...github.context.repo,
issue_number: issueNumber,
});
for (let comment of comments) {
bodyList.push(comment.body);
}
}
/**
* @typedef {Object} ChecklistItem
* @property {string} text
* @property {string[]} radioGroups
* @property {boolean} isComplete
*/
/** @type ChecklistItem[][] */
let checklistBodies = []
// Collect check list items
for (let body of bodyList) {
// Check each comment for a checklist
let multilineComment = false;
if (typeof body === "undefined") continue
/** @type ChecklistItem[] */
let checklistItems = []
// Break into lines to do comment detection
for (let line of body.split("\n")) {
// NOTE: Assume we never start nor end a multiline comment in the middle of a line... for now
if (line.lastIndexOf(COMMENT_START) > line.lastIndexOf(COMMENT_END)) {
multilineComment = true;
}
if (line.lastIndexOf(COMMENT_START) < line.lastIndexOf(COMMENT_END)) {
multilineComment = false;
}
if (!multilineComment) {
/**
* @typedef {Object} TaskItem
* @property {string} checkMark
* @property {string} text
*/
for (let match of line.matchAll(TASK_LIST_ITEM)) {
if (typeof match.groups === "undefined") continue
/** @type TaskItem */
let item = (({ checkMark, text }) => ({ checkMark: checkMark || "", text: text || "" }))(match.groups)
let is_complete = ["x", "X"].includes(item.checkMark);
if (skipRegexPattern && skipDescriptionRegex && skipDescriptionRegex.test(item.text)) {
console.log("Skipping task list item: " + item.text);
continue;
}
if (is_complete) {
console.log("Completed task list item: " + item.text);
} else {
console.log("Incomplete task list item: " + item.text);
}
checklistItems.push({
text: item.text,
radioGroups: [...item.text.matchAll(RADIO_TAG_ITEM)].map((radioMatch) => radioMatch.groups && radioMatch.groups.radioTag || "").filter((tag) => tag),
isComplete: is_complete
})
}
}
}
if (checklistItems.length > 0) checklistBodies.push(checklistItems)
}
/**
* @typedef {Object.<string, ChecklistItem[]>} ChecklistRadioGroup
*/
/** @type ChecklistItem[] */
let incompleteItems = []
/** @type ChecklistItem[][] */
let radioConflictItems = []
for (let items of checklistBodies) {
/** @type ChecklistRadioGroup */
let radioGroupedItems = {}
for (let item of items) {
if (item.radioGroups.length == 0 && !item.isComplete) {
incompleteItems.push(item)
continue
}
for (let radioGroup of item.radioGroups) {
if (typeof radioGroupedItems[radioGroup] === "undefined") radioGroupedItems[radioGroup] = []
radioGroupedItems[radioGroup].push(item)
}
}
for (let group in radioGroupedItems) {
let completedItems = radioGroupedItems[group].filter((item) => item.isComplete)
if (completedItems.length == 0) incompleteItems.push(...radioGroupedItems[group])
if (completedItems.length > 1) radioConflictItems.push(completedItems)
}
}
if (incompleteItems.length > 0) {
core.setFailed(
"The following items are not marked as completed: " +
incompleteItems.map((item) => item.text).join(", ")
);
}
if (radioConflictItems.length > 0) {
for (let items of radioConflictItems) {
core.setFailed(
"The following items cannot be marked as completed simultaneously: " +
items.map((item) => item.text).join(", ")
)
}
return
}
const requireChecklist = core.getInput("requireChecklist");
if (requireChecklist != "false" && checklistBodies.length == 0) {
core.setFailed(
"No task list was present and requireChecklist is turned on"
);
return;
}
console.log("There are no incomplete task list items");
}
if (require.main === module) {
action();
}
module.exports = action;