-
Notifications
You must be signed in to change notification settings - Fork 79
/
main.ts
137 lines (118 loc) · 4.41 KB
/
main.ts
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
import path from 'path';
import fs from 'fs';
import * as github from '@actions/github';
import * as core from '@actions/core';
import { GetResponseDataTypeFromEndpointMethod } from '@octokit/types';
// See https://docs.github.com/en/rest/reactions#reaction-types
const REACTIONS = ['+1', '-1', 'laugh', 'confused', 'heart', 'hooray', 'rocket', 'eyes'] as const;
type Reaction = typeof REACTIONS[number];
async function run() {
try {
const message: string = core.getInput('message');
const filePath: string = core.getInput('filePath');
const github_token: string = core.getInput('GITHUB_TOKEN');
const pr_number: string = core.getInput('pr_number');
const comment_tag: string = core.getInput('comment_tag');
const reactions: string = core.getInput('reactions');
const mode: string = core.getInput('mode');
const create_if_not_exists: boolean = core.getInput('create_if_not_exists') === 'true';
if (!message && !filePath) {
core.setFailed('Either "filePath" or "message" should be provided as input');
return;
}
let content: string = message;
let _filePath: string;
if (!message && filePath) {
const { GITHUB_WORKSPACE } = process.env;
if (!GITHUB_WORKSPACE) {
core.setFailed('"GITHUB_WORKSPACE" env variable is not defined.');
return;
}
_filePath = path.join(GITHUB_WORKSPACE, filePath);
content = fs.readFileSync(_filePath, 'utf8');
}
const context = github.context;
const issue_number = parseInt(pr_number) || context.payload.pull_request?.number || context.payload.issue?.number;
const octokit = github.getOctokit(github_token);
if (!issue_number) {
core.setFailed('No issue/pull request in input neither in current context.');
return;
}
async function addReactions(comment_id: number, reactions: string) {
const validReactions = <Reaction[]>reactions
.replace(/\s/g, '')
.split(',')
.filter((reaction) => REACTIONS.includes(<Reaction>reaction));
await Promise.allSettled(
validReactions.map(async (content) => {
await octokit.rest.reactions.createForIssueComment({
...context.repo,
comment_id,
content,
});
}),
);
}
const comment_tag_pattern = comment_tag
? `<!-- thollander/actions-comment-pull-request "${comment_tag}" -->`
: null;
const body = comment_tag_pattern ? `${content}\n${comment_tag_pattern}` : content;
if (comment_tag_pattern) {
type ListCommentsResponseDataType = GetResponseDataTypeFromEndpointMethod<
typeof octokit.rest.issues.listComments
>;
let comment: ListCommentsResponseDataType[0] | undefined;
for await (const { data: comments } of octokit.paginate.iterator(octokit.rest.issues.listComments, {
...context.repo,
issue_number,
})) {
comment = comments.find((comment) => comment?.body?.includes(comment_tag_pattern));
if (comment) break;
}
if (comment) {
if (mode === 'upsert') {
await octokit.rest.issues.updateComment({
...context.repo,
comment_id: comment.id,
body,
});
await addReactions(comment.id, reactions);
return;
} else if (mode === 'recreate') {
await octokit.rest.issues.deleteComment({
...context.repo,
comment_id: comment.id,
});
const { data: newComment } = await octokit.rest.issues.createComment({
...context.repo,
issue_number,
body,
});
await addReactions(newComment.id, reactions);
return;
} else {
core.setFailed(`Mode ${mode} is unknown. Please use 'upsert' or 'recreate'.`);
return;
}
} else if (create_if_not_exists) {
core.info('No comment has been found with asked pattern. Creating a new comment.');
} else {
core.info(
'Not creating comment as the pattern has not been found. Use `create_if_not_exists: true` to create a new comment anyway.',
);
return;
}
}
const { data: comment } = await octokit.rest.issues.createComment({
...context.repo,
issue_number,
body,
});
await addReactions(comment.id, reactions);
} catch (error) {
if (error instanceof Error) {
core.setFailed(error.message);
}
}
}
run();