Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update job to comment built resources on PR #228

Merged
merged 2 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/test-build-resources-with-pandoc.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
name: build-resources-with-pandoc

on:
pull_request:
branches: [master]
push:
branches:
- "*" # matches every branch that doesn't contain a '/'
- "*/*" # matches every branch containing a single '/'
- "**" # matches every branch
- "!master" # excludes master
tags: ["!**"]

jobs:
build-resources-with-pandoc:
Expand All @@ -20,6 +23,7 @@ jobs:
args: "./build-resources-with-pandoc.sh"
- name: Archive resources
uses: actions/upload-artifact@v4
id: artifact-upload-step
with:
name: pandoc_resources
path: ./public/resources/
Expand All @@ -31,3 +35,15 @@ jobs:
name: error_resources
path: ./documents/
retention-days: 1
- uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { postCustomForArchiveResources } = await import('${{ github.workspace }}/scripts/pr-comment.mjs')

await postCustomForArchiveResources({
github,
context,
core,
url: '${{steps.artifact-upload-step.outputs.artifact-url}}'
})
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
},
"homepage": "https://future-architect.github.io/coding-standards/",
"devDependencies": {
"@actions/github": "^6.0.0",
"@eslint/eslintrc": "^3.2.0",
"@eslint/js": "^9.17.0",
"eslint": "^9.17.0",
Expand Down
93 changes: 93 additions & 0 deletions scripts/pr-comment.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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,
});
}
}
}
Loading