diff --git a/.github/workflows/test-build-resources-with-pandoc.yml b/.github/workflows/test-build-resources-with-pandoc.yml index df6d8689..e8d0acbe 100644 --- a/.github/workflows/test-build-resources-with-pandoc.yml +++ b/.github/workflows/test-build-resources-with-pandoc.yml @@ -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: @@ -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/ @@ -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}}' + }) diff --git a/package.json b/package.json index 94e69613..e4d6bb79 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/scripts/pr-comment.mjs b/scripts/pr-comment.mjs new file mode 100644 index 00000000..dfe20018 --- /dev/null +++ b/scripts/pr-comment.mjs @@ -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 = + ""; + + 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, + }); + } + } +}