|
| 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