-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
66 lines (53 loc) · 2.07 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
const core = require("@actions/core");
const github = require("@actions/github");
const DEFAULT_COMMENT_IDENTIFIER = "4YE2JbpAewMX4rxmRnWyoSXoAfaiZH19QDB2IR3OSJTxmjSu"
async function checkForExistingComment(octokit, repo, owner, issue_number, commentIdentifier) {
const existingComments = await octokit.issues.listComments({
repo, owner, issue_number
});
let existingCommentId = undefined;
if (Array.isArray(existingComments.data))
existingComments.data.forEach(({ body, id }) => {
if (body.includes(commentIdentifier))
existingCommentId = id;
})
return existingCommentId;
}
async function run() {
try {
const ctx = github.context;
const commentMessage = core.getInput('message');
const commentId = core.getInput('COMMENT_IDENTIFIER') ? core.getInput('COMMENT_IDENTIFIER') : DEFAULT_COMMENT_IDENTIFIER;
const githubToken =core.getInput('GITHUB_TOKEN');
const issue_id = core.getInput('ISSUE_ID') ? core.getInput('ISSUE_ID') : ctx.payload.pull_request.number;
const { owner, repo } = ctx.repo;
if (!issue_id) {
core.setFailed("Action must run on a Pull Request.");
return;
}
const octokit = new github.GitHub(githubToken);
// Suffix comment with hidden value to check for updating later.
const commentIdSuffix = `\n\n\n<hidden purpose="for-rewritable-pr-comment-action-use" value="${commentId}"></hidden>`;
// If comment already exists, get the comment ID.
const existingCommentId = await checkForExistingComment(octokit, repo, owner, issue_id, commentIdSuffix)
const commentBody = commentMessage + commentIdSuffix;
let comment = undefined;
if (existingCommentId) {
comment = await octokit.issues.updateComment({
repo, owner,
comment_id: existingCommentId,
body: commentBody
})
} else {
comment = await octokit.issues.createComment({
repo, owner,
issue_number: issue_id,
body: commentBody
});
}
core.setOutput("comment-id",comment.data.id);
} catch (e) {
core.setFailed(e.message);
}
}
run().then();