generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
92 lines (77 loc) · 2.08 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
const core = require('@actions/core');
const github = require('@actions/github');
const { getReminder, addReminderToBody } = require('./utilities');
const LABEL = 'reminder';
function getIssueProps(context) {
return {
owner: context.repository.owner,
repo: context.repository.name,
issue_number: context.issue.number,
};
}
function createComment(octokit, context, body) {
return octokit.rest.issues.createComment({
...getIssueProps(context),
body,
});
}
function updateIssue(octokit, context, reminder) {
const body = addReminderToBody(context.issue.body, reminder);
return octokit.rest.issues.update({
...getIssueProps(context),
body,
});
}
async function run() {
const context = github.context.payload;
const owner = core.getInput('repositoryOwner');
const repository = core.getInput('repository');
const octokit = github.getOctokit(
core.getInput('repoToken', { required: true })
);
let reminder;
context.repository = {
owner,
name: repository.split('/')[1],
};
try {
core.startGroup('parsing reminder');
reminder = getReminder(context);
core.info(JSON.stringify(reminder, null, 1));
if (!reminder) {
core.info('no reminder found');
return;
}
core.endGroup();
} catch (error) {
core.startGroup('create error comment');
await createComment(
octokit,
context,
`@${context.sender.login} we had trouble parsing your reminder. Try:\n\n\`/remind me [what] [when]\``
);
core.endGroup();
core.setFailed(error);
return;
}
core.startGroup('add label');
core.info(JSON.stringify(getIssueProps(context), null, 1));
await octokit.rest.issues.addLabels({
...getIssueProps(context),
labels: [LABEL],
});
core.endGroup();
core.startGroup('update issue');
await updateIssue(octokit, context, reminder);
core.endGroup();
core.startGroup('add reminder comment');
await createComment(
octokit,
context,
`@${
context.sender.login
} set a reminder for **${reminder.when.toLocaleDateString()}**`
);
core.endGroup();
}
run();