-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
472 lines (411 loc) · 15.3 KB
/
index.js
File metadata and controls
472 lines (411 loc) · 15.3 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
const github = require("@actions/github");
const core = require("@actions/core");
// Get configuration variables
const secretToken = core.getInput("secret-token");
const epicPrefix = core.getInput("epic-prefix");
const tasksMarker = core.getInput("tasks-marker");
const closeCompletedEpics = core.getInput("close-completed-epics");
// Constants
const taskExpression = /(?<pre> *)- \[(?<closed>.)\] #(?<number>[0-9]+) *(?<title>.*)/;
// Construct Octokit object and get GitHub context
const octokit = new github.getOctokit(secretToken);
const context = github.context;
// Main function
async function run() {
// Safety check - only act on issues
var sourceIssue = context.payload.issue;
if (!sourceIssue)
return;
// Print config
console.log("Config:")
console.log(" - epicPrefix = '" + epicPrefix + "'");
console.log(" - tasksMarker = '" + tasksMarker + "'");
console.log(" - closeCompletedEpics = " + closeCompletedEpics);
// Check config
if (epicPrefix === "") {
core.setFailed("Epic prefix cannot be an empty string.");
return;
}
if (tasksMarker === "") {
core.setFailed("Workload marker cannot be an empty string.");
return;
}
/*
* The issue may be an Epic that has been created / updated, in which case we
* reformat the 'Workload' section to include issue titles etc.
*
* It may also be a normal issue that is referenced within an Epic - in that
* case we just update the corresponding entry in the 'Workload' section,
* marking the task as complete, updating its title, etc.
*
* Check the issue title to find out which is the case, using 'epicPrefix' to
* identify the issue as an actual Epic.
*/
var result = null;
if (sourceIssue.title.startsWith(epicPrefix)) {
try {
result = await updateEpic(sourceIssue);
} catch(err) {
core.setFailed(err);
return;
}
}
else {
try {
result = await updateEpicFromTask(sourceIssue);
} catch(err) {
core.setFailed(err);
return;
}
}
console.log(result);
}
// Update Epic issue
async function updateEpic(epicIssue) {
console.log("Updating Epic issue #" + epicIssue.number + " (" + epicIssue.title + ")...");
/*
* Issues forming the workload for this Epic are expected to be in a section
* of the main issue body called 'Workload', as indicated by a markdown
* heading ('#', '##', etc.).
*/
// Split the Epic body into individual lines
var inWorkload = false
var body = epicIssue.body.split(/\r?\n/g);
var nBodyLines = body.length;
for (var i = 0; i < nBodyLines; ++i) {
// Check for heading, potentially indicating the start of the workload section
if (body[i].startsWith("#")) {
if (body[i].endsWith(tasksMarker)) {
inWorkload = true;
continue;
}
else if (inWorkload)
break;
}
// If we are not in the workload section, no need to do anything else
if (!inWorkload)
continue;
// Does the line start with checkbox markdown indicating a task?
let match = taskExpression.exec(body[i]);
if (match == null)
continue;
// Retrieve task issue
var taskIssue = null;
try {
taskIssue = await octokit.issues.get({
...context.repo,
issue_number: parseInt(match.groups.number)
});
} catch(err) {
core.setFailed(err);
return false;
}
// Did we find the issue?
if (!taskIssue) {
core.setFailed("Error - task #" + match.groups.number + " is referenced in Epic #" + epicIssue.number + " but it doesn't exist.");
return false;
}
// Update the Epic issue body based on the task issue data if it needs it
var result = null;
try {
result = await updateTask(epicIssue.number, body[i], taskIssue.data, false);
} catch(err) {
core.setFailed(err);
return;
}
if (!result) {
console.log("Nothing to update for task #" + taskIssue.data.number + " in Epic #" + epicIssue.number + ".");
continue;
}
// Store the updated line in our body array
body[i] = result.line;
// Comment on the Epic?
if (result.comment) {
try {
await octokit.issues.createComment({
...context.repo,
issue_number: epicIssue.number,
body: result.comment
});
} catch(err) {
core.setFailed(err);
return false;
}
}
console.log("Updated Epic #" + epicIssue.number + " with new information for task #" + taskIssue.data.number);
}
// Commit the updated Epic body text
var newBody = body.join("\r\n");
try {
await octokit.issues.update({
...context.repo,
issue_number: epicIssue.number,
body: newBody
});
} catch(err) {
core.setFailed(err);
return false;
}
// Close the Epic if all tasks are completed?
if (closeCompletedEpics)
await closeEpicIfComplete(epicIssue.number, newBody);
return true;
}
// Update Task issue within Epic
async function updateEpicFromTask(taskIssue) {
console.log("Searching issue #" + taskIssue.number + " (" + taskIssue.title + ") for Epic cross-references...");
/*
* Normal issues may or may not be associated to an Epic. If they are not,
* there is nothing more to do. If they are, then we must update the Epic
* accordingly.
*
* The task may be present in more than one Epic, so consider all referenced
* issues.
*/
var timeline = null;
try {
timeline = await octokit.issues.listEventsForTimeline({
...context.repo,
issue_number: taskIssue.number
});
} catch(err) {
core.setFailed(err);
return false;
}
// Look for 'cross-referenced' events, and check if those relate to Epics
for (event of timeline.data) {
if (event.event != "cross-referenced")
continue;
// If the cross-referencing event is not an issue, continue
if (event.source.type != "issue")
continue;
// Get referencing Epic issue
const epicIssue = event.source.issue;
// Is the cross-referencing issue an Epic?
if (!epicIssue.title.startsWith(epicPrefix))
continue;
console.log("Task issue #" + taskIssue.number + " is cross-referenced by Epic #" + epicIssue.number);
// Update the Epic issue body based on our own data if necessary
var result = null;
try {
result = await updateTaskInEpic(epicIssue.number, epicIssue.body, taskIssue);
} catch(err) {
core.setFailed(err);
return;
}
if (!result) {
console.log("Nothing to update - Epic #" + epicIssue.number + " body remains as-is.");
return false;
}
// Commit the updated Epic
try {
await octokit.issues.update({
...context.repo,
issue_number: epicIssue.number,
body: result.body
});
} catch(err) {
core.setFailed(err);
return false;
}
// Comment on the Epic?
if (result.comment) {
try {
await octokit.issues.createComment({
...context.repo,
issue_number: epicIssue.number,
body: result.comment
});
} catch(err) {
core.setFailed(err);
return false;
}
}
console.log("Updated Epic #" + epicIssue.number + " with new information for task #" + taskIssue.number);
// Close the Epic if all tasks are completed?
if (closeCompletedEpics)
await closeEpicIfComplete(epicIssue.number, result.body);
}
return true;
}
// Update task within supplied body text from issue data given
async function updateTaskInEpic(epicNumber, epicBody, taskIssue) {
var inWorkload = false
var body = epicBody.split(/\r?\n/g);
var nBodyLines = body.length;
for (var i = 0; i < nBodyLines; ++i) {
// Check for heading, potentially indicating the start of the workload section
if (body[i].startsWith("#")) {
if (body[i].endsWith(tasksMarker)) {
inWorkload = true;
continue;
}
else if (inWorkload)
return null;
}
// If we are not in the workload section, no need to do anything else
if (!inWorkload)
continue;
// Does the line start with checkbox markdown indicating a task?
var match = taskExpression.exec(body[i]);
if (!match)
continue;
// Does the taskIssue number match the one on this line?
if (match.groups.number != taskIssue.number)
continue;
// Found the taskIssue in the list, so update as necessary
var result = null;
try {
result = await updateTask(epicNumber, body[i], taskIssue, true);
} catch(err) {
core.setFailed(err);
return;
}
if (!result)
return null;
// Reconstitute and return updated body text
body[i] = result.line;
return {
body: body.join("\r\n"),
comment: result.comment
}
}
return null;
}
// Update task data, returning new line and comment if changes were made, or null if it was up to date
async function updateTask(epicNumber, taskLine, taskIssue, taskIsTruth) {
// Ensure that we're working with a task line
var match = taskExpression.exec(taskLine);
if (!match) {
console.log("...updateTask() - Not a task line? (\""+taskLine+"\")");
return null;
}
// Does the taskIssue number match the one on this line?
if (match.groups.number != taskIssue.number) {
console.log("...updateTask() - Issue numbers don't match (" + match.groups.number + " vs. " + taskIssue.number + ")");
return null;
}
// Check task status and title and update as necessary
var updateTitle = false;
var updateState = false;
const epicIssueClosed = match.groups.closed === "x";
const taskIssueClosed = taskIssue.state === "closed";
if (epicIssueClosed != taskIssueClosed)
updateState = true;
if (match.groups.title != taskIssue.title)
updateTitle = true;
// Return null if no updates were necessary
if (!updateTitle && !updateState)
return null;
var newLine = null;
var comment = null;
// If the current task state in the issue is truth (taskIsTruth === true) then we update the (Epic) line from the issue.
// Otherwise, we update the issue from the (Epic) line.
if (taskIsTruth) {
// Reconstitute the line, create a suitable comment, and return the new data
newLine = match.groups.pre + "- [" + (taskIssueClosed ? "x" : " ") + "] #" + taskIssue.number + " " + taskIssue.title;
if (updateState && updateTitle)
comment = "`EpicBot` refreshed the title for task #" + taskIssue.number + " and marked it as `" + (taskIssueClosed ? "closed" : "open") + "`.";
else if (updateState)
comment = "`EpicBot` marked task #" + taskIssue.number + " as `" + (taskIssueClosed ? "closed" : "open") + "`.";
else if (updateTitle)
comment = "`EpicBot` refreshed the title for task #" + taskIssue.number + ".";
}
else
{
// Update the issue state from the current data, if it is required
if (updateState) {
// The line remains the same, but we will update the issue accordingly
try {
await octokit.issues.update({
...context.repo,
issue_number: taskIssue.number,
state: epicIssueClosed ? "closed" : "open"
});
} catch(err) {
core.setFailed(err);
return false;
}
// Comment on the issue
try {
await octokit.issues.createComment({
...context.repo,
issue_number: taskIssue.number,
body: "`EpicBot` " + (epicIssueClosed ? "closed" : "re-opened") + " this issue following changes in Epic #" + epicNumber + "."
});
} catch(err) {
core.setFailed(err);
return false;
}
}
// Reconstitute the line, and generate a suitable comment
newLine = match.groups.pre + "- [" + (epicIssueClosed ? "x" : " ") + "] #" + taskIssue.number + " " + taskIssue.title;
if (updateState && updateTitle)
comment = "`EpicBot` " + (epicIssueClosed ? "closed" : "opened") + " task #" + taskIssue.number + " following changes in the Epic, and refreshed its title.";
else if (updateState)
"`EpicBot` " + (epicIssueClosed ? "closed" : "opened") + " task #" + taskIssue.number + " following changes in the Epic."
else if (updateTitle)
comment = "`EpicBot` refreshed the title for task #" + taskIssue.number + ".";
}
// Return updated data
return {
line: newLine,
comment: comment
}
}
// Close specifed Epic if all tasks (in the associated body) are complete
async function closeEpicIfComplete(epicNumber, epicBody) {
console.log("Checking if Epic #" + epicNumber + " is complete...");
var inWorkload = false;
var nTasks = 0;
var body = epicBody.split(/\r?\n/g);
for (line of body) {
// Check for heading, potentially indicating the start of the workload section
if (line.startsWith("#")) {
if (line.endsWith(tasksMarker)) {
inWorkload = true;
continue;
}
else if (inWorkload)
break;
}
// If we are not in the workload section, continue
if (!inWorkload)
continue;
// Does the line start with checkbox markdown indicating a task?
let match = taskExpression.exec(line);
if (match == null)
continue;
++nTasks;
// If the task is not complete, return false immediately
if (match.groups.closed != "x")
return false;
}
// Exit here if there are no tasks associated to this epic
if (nTasks == 0)
return false;
console.log("Closing Epic #" + epicNumber + " as all tasks have been completed.");
try {
await octokit.issues.createComment({
...context.repo,
issue_number: epicNumber,
body: "`EpicBot` closed this Epic as all tasks are complete."
});
} catch(err) {
core.setFailed(err);
return false;
}
try {
await octokit.issues.update({
...context.repo,
issue_number: epicNumber,
state: "closed"
});
} catch(err) {
core.setFailed(err);
return false;
}
return true;
}
// Run the action
run()