Skip to content

Commit 65cf788

Browse files
authored
chore: update job to comment built resources on PR (#228)
* chore: update job to comment built resources on PR * update
1 parent c9b2a95 commit 65cf788

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

.github/workflows/test-build-resources-with-pandoc.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
name: build-resources-with-pandoc
22

33
on:
4+
pull_request:
5+
branches: [master]
46
push:
57
branches:
68
- "*" # matches every branch that doesn't contain a '/'
79
- "*/*" # matches every branch containing a single '/'
810
- "**" # matches every branch
911
- "!master" # excludes master
12+
tags: ["!**"]
1013

1114
jobs:
1215
build-resources-with-pandoc:
@@ -20,6 +23,7 @@ jobs:
2023
args: "./build-resources-with-pandoc.sh"
2124
- name: Archive resources
2225
uses: actions/upload-artifact@v4
26+
id: artifact-upload-step
2327
with:
2428
name: pandoc_resources
2529
path: ./public/resources/
@@ -31,3 +35,15 @@ jobs:
3135
name: error_resources
3236
path: ./documents/
3337
retention-days: 1
38+
- uses: actions/github-script@v7
39+
with:
40+
github-token: ${{ secrets.GITHUB_TOKEN }}
41+
script: |
42+
const { postCustomForArchiveResources } = await import('${{ github.workspace }}/scripts/pr-comment.mjs')
43+
44+
await postCustomForArchiveResources({
45+
github,
46+
context,
47+
core,
48+
url: '${{steps.artifact-upload-step.outputs.artifact-url}}'
49+
})

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
},
2626
"homepage": "https://future-architect.github.io/coding-standards/",
2727
"devDependencies": {
28+
"@actions/github": "^6.0.0",
2829
"@eslint/eslintrc": "^3.2.0",
2930
"@eslint/js": "^9.17.0",
3031
"eslint": "^9.17.0",

scripts/pr-comment.mjs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* Used in `/.github/workflows/test-build-resources-with-pandoc.yml`
3+
* @param {object} params
4+
* @param {import('@actions/github/lib/utils').GitHub} params.github
5+
* @param {import('@actions/github/lib/context').Context} params.context
6+
* @param {string} params.url
7+
*/
8+
export async function postCustomForArchiveResources({ github, context, url }) {
9+
const sha =
10+
context.eventName === "pull_request"
11+
? context.payload.pull_request.head.sha
12+
: context.payload.after;
13+
const commitUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/commit/${sha}`;
14+
15+
const pullRequestNumber = await getPullRequestNumber();
16+
17+
const botCommentIdentifier =
18+
"<!-- posted by scripts/pr-comment.mjs#postCustomForArchiveResources -->";
19+
20+
const body = `${botCommentIdentifier}
21+
22+
## Pandocで生成したリソースの確認
23+
24+
<${url}>
25+
26+
---
27+
28+
[View Commit](${commitUrl})`;
29+
30+
if (pullRequestNumber) {
31+
await createOrUpdateComment(pullRequestNumber);
32+
} else {
33+
console.log(
34+
"No open pull request found for this push. Logging publish information to console:",
35+
);
36+
console.log(`\n${"=".repeat(50)}`);
37+
console.log(body);
38+
console.log(`\n${"=".repeat(50)}`);
39+
}
40+
41+
async function getPullRequestNumber() {
42+
if (context.eventName === "pull_request") {
43+
if (context.issue.number) {
44+
return context.issue.number;
45+
}
46+
} else if (context.eventName === "push") {
47+
const pullRequests = await github.rest.pulls.list({
48+
owner: context.repo.owner,
49+
repo: context.repo.repo,
50+
state: "open",
51+
head: `${context.repo.owner}:${context.ref.replace("refs/heads/", "")}`,
52+
});
53+
54+
if (pullRequests.data.length > 0) {
55+
return pullRequests.data[0].number;
56+
}
57+
}
58+
59+
return null;
60+
}
61+
62+
async function findBotComment(issueNumber) {
63+
const comments = await github.rest.issues.listComments({
64+
owner: context.repo.owner,
65+
repo: context.repo.repo,
66+
issue_number: issueNumber,
67+
});
68+
69+
return comments.data.find((comment) =>
70+
comment.body.includes(botCommentIdentifier),
71+
);
72+
}
73+
74+
async function createOrUpdateComment(issueNumber) {
75+
const existingComment = await findBotComment(issueNumber);
76+
77+
if (existingComment) {
78+
await github.rest.issues.updateComment({
79+
owner: context.repo.owner,
80+
repo: context.repo.repo,
81+
comment_id: existingComment.id,
82+
body,
83+
});
84+
} else {
85+
await github.rest.issues.createComment({
86+
issue_number: issueNumber,
87+
owner: context.repo.owner,
88+
repo: context.repo.repo,
89+
body,
90+
});
91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)