forked from agrc/reminder-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
139 lines (110 loc) · 3.22 KB
/
index.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
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
const core = require('@actions/core');
const github = require('@actions/github');
const {
getRemindersFromBody,
getPastDueReminders,
createCommentsMetadata,
markAsNotified,
} = require('./utilities');
const LABEL = 'reminder';
function getIssueProps(context) {
return {
owner: context.repository.owner,
repo: context.repository.name,
};
}
async function run() {
try {
const context = github.context.payload;
const owner = core.getInput('repositoryOwner');
const repository = core.getInput('repository');
const octokit = github.getOctokit(
core.getInput('repoToken', { required: true })
);
context.repository = {
owner,
name: repository.split('/')[1],
};
let issues = [];
core.startGroup('get open reminder issues');
for await (const response of octokit.paginate.iterator(
octokit.rest.issues.listForRepo,
{
...getIssueProps(context),
state: 'open',
labels: [LABEL],
}
)) {
issues = issues.concat(response.data);
}
if (issues.length < 1) {
core.info('no open issues found with the reminder label');
return;
}
core.endGroup();
const reminders = [];
core.startGroup('get all reminders from issues');
issues.forEach((issue) => {
const remindersFromIssue = getRemindersFromBody(issue.body);
core.info(
`${remindersFromIssue.length} found for issue #${issue.number}`
);
remindersFromIssue.forEach((reminder) => {
reminders.push({
issueNumber: issue.number,
reminder,
body: issue.body,
});
});
});
if (reminders.length < 1) {
core.info('no reminders found');
return;
}
core.endGroup();
core.startGroup('filter reminders for past due');
const pastDueReminders = getPastDueReminders(Date.now(), reminders);
if (reminders.length < 1) {
core.info('no past due reminders found');
return;
}
core.endGroup();
core.startGroup('notify past due reminders');
core.info(`sending ${reminders.length} past due notifications`);
const metadata = createCommentsMetadata(pastDueReminders);
for (let i = 0; i < metadata.length; i++) {
const data = metadata[i];
await octokit.rest.issues.createComment({
...data,
...getIssueProps(context),
});
}
core.endGroup();
core.startGroup('removing notification metadata');
for (let i = 0; i < pastDueReminders.length; i++) {
const { body, reminder, issueNumber } = pastDueReminders[i];
const { body: newBody, hasActive } = markAsNotified(body, reminder.id);
const updateData = {
body: newBody,
issue_number: issueNumber,
...getIssueProps(context),
};
core.info(JSON.stringify(updateData, null, 1));
await octokit.rest.issues.update(updateData);
if (!hasActive) {
const data = {
issue_number: issueNumber,
name: LABEL,
...getIssueProps(context),
};
core.info(JSON.stringify(data, null, 1));
await octokit.rest.issues.removeLabel(data);
}
}
core.endGroup();
} catch (error) {
core.setFailed(error);
throw error;
}
}
run();