forked from Khan/pull-request-comment-trigger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·67 lines (55 loc) · 1.74 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
#!/usr/bin/env node
const core = require("@actions/core");
const { context, GitHub } = require("@actions/github");
async function run() {
const trigger = core.getInput("trigger", { required: true });
const reaction = core.getInput("reaction");
const { GITHUB_TOKEN } = process.env;
if (reaction && !GITHUB_TOKEN) {
core.setFailed('If "reaction" is supplied, GITHUB_TOKEN is required');
return;
}
const body =
context.eventName === "issue_comment"
? context.payload.comment.body
: context.payload.pull_request.body;
core.setOutput('comment_body', body);
if (
context.eventName === "issue_comment" &&
!context.payload.issue.pull_request
) {
// not a pull-request comment, aborting
core.setOutput("triggered", "false");
return;
}
const { owner, repo } = context.repo;
const prefixOnly = core.getInput("prefix_only") === 'true';
if ((prefixOnly && !body.startsWith(trigger)) || !body.includes(trigger)) {
core.setOutput("triggered", "false");
return;
}
core.setOutput("triggered", "true");
if (!reaction) {
return;
}
const client = new GitHub(GITHUB_TOKEN);
if (context.eventName === "issue_comment") {
await client.reactions.createForIssueComment({
owner,
repo,
comment_id: context.payload.comment.id,
content: reaction
});
} else {
await client.reactions.createForIssue({
owner,
repo,
issue_number: context.payload.pull_request.number,
content: reaction
});
}
}
run().catch(err => {
console.error(err);
core.setFailed("Unexpected error");
});