generated from actions-cool/action-js-template
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.js
177 lines (161 loc) · 5.01 KB
/
main.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const core = require('@actions/core');
const github = require('@actions/github');
const { Octokit } = require('@octokit/rest');
// ************************************************
const token = core.getInput('token');
const octokit = new Octokit({ auth: `token ${token}` });
const context = github.context;
const defaultBody = '<!-- Created by actions-cool/maintain-one-comment -->';
const { dealStringToArr, THANKS } = require('actions-util');
// ************************************************
async function run() {
try {
const owner = context.repo.owner;
const repo = context.repo.repo;
// 维护评论
const body = core.getInput('body');
const emojis = core.getInput('emojis');
let updateMode = core.getInput('update-mode');
if (updateMode !== 'append') {
updateMode = 'replace';
}
const commentAuth = core.getInput('comment-auth');
const bodyInclude = core.getInput('body-include') || defaultBody;
const doDelete = core.getInput('delete') === 'true';
// 手动 number
const inputNumber = core.getInput('number');
let number;
if (inputNumber) {
number = inputNumber;
} else if (context.eventName.includes('issue')) {
number = context.payload.issue.number;
} else if (context.eventName.includes('pull_request')) {
number = context.payload.pull_request.number;
} else {
core.info(
`Now eventName: ${context.eventName}. And input number is empty. This Action only support issue and pull_request related!`,
);
return false;
}
async function listComments(page = 1) {
let { data: comments } = await octokit.issues.listComments({
owner,
repo,
issue_number: number,
per_page: 100,
page,
});
if (comments.length >= 100) {
comments = comments.concat(await listComments(page + 1));
}
return comments;
}
const commentList = await listComments();
core.info(`Actions: [find-comments][${number}] success!`);
let comments = [];
commentList.forEach(item => {
const a = commentAuth ? item.user.login === commentAuth : true;
const b = bodyInclude ? item.body.includes(bodyInclude) : true;
if (a && b) {
comments.push({
id: item.id,
auth: item.user.login,
body: item.body,
});
}
});
core.info(`filter-comments: ${JSON.stringify(comments)}`);
core.info(`filter-comments-length: ${comments.length}`);
let newCommentId;
if (comments.length === 0) {
const commentBody = `${body}\n${bodyInclude}`;
const { data } = await octokit.issues.createComment({
owner,
repo,
issue_number: number,
body: commentBody,
});
core.info(`Actions: [create-comment][${commentBody}] success!`);
core.setOutput('comment-id', data.id);
newCommentId = data.id;
if (emojis) {
dealStringToArr(emojis).forEach(async item => {
if (testEmoji(item)) {
await octokit.reactions.createForIssueComment({
owner,
repo,
comment_id: data.id,
content: item,
});
core.info(`Actions: [create-emoji][${item}] success!`);
}
});
}
} else if (comments.length === 1) {
let commentId = comments[0].id;
if (!body) {
await octokit.issues.deleteComment({
owner,
repo,
comment_id: commentId,
});
core.info(`Actions: [delete-comment][${commentId}] success!`);
return false;
}
const comment = await octokit.issues.getComment({
owner,
repo,
comment_id: commentId,
});
const comment_body = comment.data.body;
let params = {
owner,
repo,
comment_id: commentId,
};
let commentBody;
if (updateMode === 'append') {
commentBody = `${comment_body}\n${body}`;
} else {
commentBody = body;
}
params.body = `${commentBody}\n${bodyInclude}`;
await octokit.issues.updateComment(params);
core.setOutput('comment-id', commentId);
core.info(`Actions: [update-comment][${params.body}] success!`);
} else {
let length = comments.length;
core.info(`The comments length is ${length}.`);
}
if (doDelete) {
for (const { id } of comments) {
await octokit.issues.deleteComment({
owner,
repo,
comment_id: id,
});
}
if (newCommentId) {
await octokit.issues.deleteComment({
owner,
repo,
comment_id: newCommentId,
});
}
}
core.info(THANKS);
} catch (error) {
core.setFailed(error.message);
}
}
const ALLEMOJIS = ['+1', '-1', 'laugh', 'confused', 'heart', 'hooray', 'rocket', 'eyes'];
function testEmoji(con) {
if (ALLEMOJIS.includes(con)) {
return true;
} else {
core.info(`This emoji: ${con} not supported!`);
return false;
}
}
// ************************************************
run();