-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathpr-comment.mjs
93 lines (78 loc) · 2.57 KB
/
pr-comment.mjs
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
/**
* Used in `/.github/workflows/test-build-resources-with-pandoc.yml`
* @param {object} params
* @param {import('@actions/github/lib/utils').GitHub} params.github
* @param {import('@actions/github/lib/context').Context} params.context
* @param {string} params.url
*/
export async function postCustomForArchiveResources({ github, context, url }) {
const sha =
context.eventName === "pull_request"
? context.payload.pull_request.head.sha
: context.payload.after;
const commitUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/commit/${sha}`;
const pullRequestNumber = await getPullRequestNumber();
const botCommentIdentifier =
"<!-- posted by scripts/pr-comment.mjs#postCustomForArchiveResources -->";
const body = `${botCommentIdentifier}
## Pandocで生成したリソースの確認
<${url}>
---
[View Commit](${commitUrl})`;
if (pullRequestNumber) {
await createOrUpdateComment(pullRequestNumber);
} else {
console.log(
"No open pull request found for this push. Logging publish information to console:",
);
console.log(`\n${"=".repeat(50)}`);
console.log(body);
console.log(`\n${"=".repeat(50)}`);
}
async function getPullRequestNumber() {
if (context.eventName === "pull_request") {
if (context.issue.number) {
return context.issue.number;
}
} else if (context.eventName === "push") {
const pullRequests = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: "open",
head: `${context.repo.owner}:${context.ref.replace("refs/heads/", "")}`,
});
if (pullRequests.data.length > 0) {
return pullRequests.data[0].number;
}
}
return null;
}
async function findBotComment(issueNumber) {
const comments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
});
return comments.data.find((comment) =>
comment.body.includes(botCommentIdentifier),
);
}
async function createOrUpdateComment(issueNumber) {
const existingComment = await findBotComment(issueNumber);
if (existingComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body,
});
} else {
await github.rest.issues.createComment({
issue_number: issueNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body,
});
}
}
}