forked from agrc/reminder-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.js
58 lines (47 loc) · 1.27 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
const regex = /\r?\n\r?\n<!-- bot: (?<reminder>{"reminders":.*) -->/;
function getRemindersFromBody(body) {
if (body === null) return [];
const match = body.match(regex);
return match ? JSON.parse(match.groups.reminder).reminders : [];
}
function getPastDueReminders(now, items) {
return items.filter((item) => {
try {
const dueDate = Date.parse(item.reminder.when);
return dueDate < now;
} catch (error) {
console.error('error parsing date', error);
}
return false;
});
}
function createCommentsMetadata(items) {
return items.map((item) => {
return {
issue_number: item.issueNumber,
body: `:wave: @${item.reminder.who}, ${item.reminder.what}`,
};
});
}
function markAsNotified(body, reminderId) {
const reminders = getRemindersFromBody(body);
const activeReminders = reminders.filter(
(reminder) => reminder.id !== reminderId
);
if (activeReminders.length === 0) {
return { body: body.replace(regex, ''), hasActive: false };
}
return {
body: body.replace(
regex,
`\n\n<!-- bot: ${JSON.stringify({ reminders: activeReminders })} -->`
),
hasActive: true,
};
}
module.exports = {
getRemindersFromBody,
getPastDueReminders,
createCommentsMetadata,
markAsNotified,
};