|
| 1 | +/* eslint-disable camelcase */ |
| 2 | + |
| 3 | +import fs from "node:fs"; |
| 4 | + |
| 5 | +/** |
| 6 | + * @param {{ github: EXPECTED_ANY, context: EXPECTED_ANY }} params params |
| 7 | + */ |
| 8 | +export async function run({ github, context }) { |
| 9 | + const output = JSON.parse(fs.readFileSync("output.json", "utf8")); |
| 10 | + |
| 11 | + const sha = |
| 12 | + context.eventName === "pull_request" ? context.payload.pull_request.head.sha : context.sha; |
| 13 | + |
| 14 | + const commitUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/commit/${sha}`; |
| 15 | + |
| 16 | + const packages = output.packages.map((pkg) => `${pkg.name}@${pkg.url}`).join(" "); |
| 17 | + |
| 18 | + const botCommentIdentifier = "<!-- posted by pkg.pr.new -->"; |
| 19 | + const body = `${botCommentIdentifier} |
| 20 | +This PR is packaged and the instant preview is available (${commitUrl}). |
| 21 | +
|
| 22 | +Install it locally: |
| 23 | +
|
| 24 | +- npm |
| 25 | +
|
| 26 | +\`\`\`shell |
| 27 | +npm i -D ${packages} |
| 28 | +\`\`\` |
| 29 | +
|
| 30 | +- yarn |
| 31 | +
|
| 32 | +\`\`\`shell |
| 33 | +yarn add -D ${packages} |
| 34 | +\`\`\` |
| 35 | +
|
| 36 | +- pnpm |
| 37 | +
|
| 38 | +\`\`\`shell |
| 39 | +pnpm add -D ${packages} |
| 40 | +\`\`\` |
| 41 | +`; |
| 42 | + |
| 43 | + /** |
| 44 | + * @param {number=} issueNumber PR number |
| 45 | + * @returns {Promise<EXPECTED_ANY>} the existing bot comment, if any |
| 46 | + */ |
| 47 | + async function findBotComment(issueNumber) { |
| 48 | + if (!issueNumber) return null; |
| 49 | + const comments = await github.rest.issues.listComments({ |
| 50 | + owner: context.repo.owner, |
| 51 | + repo: context.repo.repo, |
| 52 | + issue_number: issueNumber, |
| 53 | + }); |
| 54 | + return comments.data.find( |
| 55 | + (comment) => |
| 56 | + // Prevent unintentional overwriting |
| 57 | + comment.user && |
| 58 | + comment.user.login === "github-actions[bot]" && |
| 59 | + comment.body.includes(botCommentIdentifier), |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @param {number=} issueNumber issue number |
| 65 | + * @returns {Promise<void>} |
| 66 | + */ |
| 67 | + async function createOrUpdateComment(issueNumber) { |
| 68 | + if (!issueNumber) { |
| 69 | + console.log("No issue number provided. Cannot post or update comment."); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + const existingComment = await findBotComment(issueNumber); |
| 74 | + |
| 75 | + if (existingComment) { |
| 76 | + await github.rest.issues.updateComment({ |
| 77 | + owner: context.repo.owner, |
| 78 | + repo: context.repo.repo, |
| 79 | + comment_id: existingComment.id, |
| 80 | + body, |
| 81 | + }); |
| 82 | + } else { |
| 83 | + await github.rest.issues.createComment({ |
| 84 | + issue_number: issueNumber, |
| 85 | + owner: context.repo.owner, |
| 86 | + repo: context.repo.repo, |
| 87 | + body, |
| 88 | + }); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @returns {void} |
| 94 | + */ |
| 95 | + function logPublishInfo() { |
| 96 | + console.log(`\n${"=".repeat(50)}`); |
| 97 | + console.log("Publish Information"); |
| 98 | + console.log("=".repeat(50)); |
| 99 | + console.log("\nPublished Packages:"); |
| 100 | + console.log(output.packages); |
| 101 | + console.log("\nTemplates:"); |
| 102 | + console.log(output.templates); |
| 103 | + console.log(`\nCommit URL: ${commitUrl}`); |
| 104 | + console.log(`\n${"=".repeat(50)}`); |
| 105 | + } |
| 106 | + |
| 107 | + if (context.eventName === "pull_request") { |
| 108 | + if (context.issue.number) { |
| 109 | + await createOrUpdateComment(context.issue.number); |
| 110 | + } |
| 111 | + } else if (context.eventName === "push") { |
| 112 | + const { data: prs } = await github.rest.repos.listPullRequestsAssociatedWithCommit({ |
| 113 | + owner: context.repo.owner, |
| 114 | + repo: context.repo.repo, |
| 115 | + commit_sha: sha, |
| 116 | + }); |
| 117 | + |
| 118 | + if (prs.length > 0) { |
| 119 | + await createOrUpdateComment(prs[0].number); |
| 120 | + } else { |
| 121 | + console.log( |
| 122 | + "No open pull request found for this push. Logging publish information to console:", |
| 123 | + ); |
| 124 | + logPublishInfo(); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments