generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 8
/
utilities.js
75 lines (58 loc) · 1.63 KB
/
utilities.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
const parseReminder = require('parse-reminder');
///{ who: 'me',
// what: 'call the doctor',
// when: 2017-09-12T12:00:00.000Z }
function getReminder(context, referenceDate = null) {
const body = context.comment.body;
let remindLine = null;
let inCode = false;
const lines = body.split('\n');
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
// handle code blocks
if (line.startsWith('```')) {
inCode = !inCode;
continue;
}
if (inCode) continue;
// find /remind at the beginning of the line.
if (line.startsWith('/remind ')) {
remindLine = line;
break;
}
}
if (remindLine === null) {
return null;
}
const reminder = parseReminder(remindLine.slice(1), referenceDate);
if (!reminder) {
throw new Error(`Unable to parse reminder: remind ${body}`);
}
if (reminder.who === 'me') {
reminder.who = context.sender.login;
}
return reminder;
}
function addReminderToBody(body, reminder) {
const regex = /\r?\n\r?\n<!-- bot: (?<reminder>{"reminders":.*) -->/;
// body is null instead of empty on no comment issues and pr's #83
if (!body) {
body = '';
}
const match = body.match(regex);
const reminders = match ? JSON.parse(match.groups.reminder).reminders : [];
let id = 1;
if (reminders.length > 0) {
id = reminders[reminders.length - 1].id + 1;
}
reminders.push({
id,
...reminder,
});
const comment = `\n\n<!-- bot: ${JSON.stringify({ reminders })} -->`;
if (match) {
return body.replace(regex, comment);
}
return `${body}${comment}`;
}
module.exports = { getReminder, addReminderToBody };