Skip to content

Commit

Permalink
chore: update job to comment built resources on PR
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Jan 26, 2025
1 parent 02d2c06 commit 88ab187
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
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: [main]
push:
branches:
- "*" # matches every branch that doesn't contain a '/'
- "*/*" # matches every branch containing a single '/'
- "**" # matches every branch
- "!main" # 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}}'
})
7 changes: 7 additions & 0 deletions .stackblitz/codeflow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"bot": {
"issues": {
"enabled": false
}
}
}
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,
});
}
}
}

0 comments on commit 88ab187

Please sign in to comment.