|
| 1 | +/** |
| 2 | + * Used in `/.github/workflows/pkg.pr.new.yml` |
| 3 | + */ |
| 4 | +export default async function ({ github, context, output }) { |
| 5 | + // eslint-disable-next-line no-console -- For debugging on github actions. |
| 6 | + console.log("pkg-pr-new publish output:", JSON.stringify(output)); |
| 7 | + |
| 8 | + const sha = output.sha; |
| 9 | + const commitUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/commit/${sha}`; |
| 10 | + |
| 11 | + const pullRequestNumber = await getPullRequestNumber(); |
| 12 | + |
| 13 | + const packages = output.packages.map((p) => { |
| 14 | + let normalizedUrl = p.url; |
| 15 | + if (pullRequestNumber && p.url.endsWith(sha)) { |
| 16 | + normalizedUrl = `${p.url.slice(0, -sha.length)}${pullRequestNumber}`; |
| 17 | + } |
| 18 | + const repoPath = `/${context.repo.owner}/${context.repo.repo}/`; |
| 19 | + normalizedUrl = normalizedUrl.replace(repoPath, "/"); |
| 20 | + |
| 21 | + return { |
| 22 | + name: p.name, |
| 23 | + url: normalizedUrl, |
| 24 | + }; |
| 25 | + }); |
| 26 | + |
| 27 | + const botCommentIdentifier = "<!-- posted by pkg.pr.new-comment.mjs -->"; |
| 28 | + |
| 29 | + const onlineUrl = new URL( |
| 30 | + "https://eslint-online-playground.netlify.app/#eslint-plugin-jsonc", |
| 31 | + ); |
| 32 | + const overrideDeps = {}; |
| 33 | + for (const p of packages) { |
| 34 | + overrideDeps[p.name] = p.url; |
| 35 | + } |
| 36 | + onlineUrl.searchParams.set("overrideDeps", JSON.stringify(overrideDeps)); |
| 37 | + const body = `${botCommentIdentifier} |
| 38 | +
|
| 39 | +- Try the Instant Preview |
| 40 | +
|
| 41 | + [ESLint Online Playground](${onlineUrl}) |
| 42 | +
|
| 43 | +- Preview to Your Local |
| 44 | +
|
| 45 | + \`\`\` |
| 46 | + npm i ${packages.map((p) => p.url).join(" ")} |
| 47 | + \`\`\` |
| 48 | +
|
| 49 | +- Published Instant Preview Packages: |
| 50 | + ${packages.map((p) => `- ${p.name}: ${p.url}`).join("\n ")} |
| 51 | +
|
| 52 | +- [View Commit](${commitUrl})`; |
| 53 | + |
| 54 | + if (pullRequestNumber) { |
| 55 | + await createOrUpdateComment(pullRequestNumber); |
| 56 | + } else { |
| 57 | + /* eslint-disable no-console -- For debugging on github actions. */ |
| 58 | + console.log( |
| 59 | + "No open pull request found for this push. Logging publish information to console:", |
| 60 | + ); |
| 61 | + console.log(`\n${"=".repeat(50)}`); |
| 62 | + console.log(body); |
| 63 | + console.log(`\n${"=".repeat(50)}`); |
| 64 | + /* eslint-enable no-console -- For debugging on github actions. */ |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Get the pull request number from the output. |
| 69 | + */ |
| 70 | + function getPullRequestNumber() { |
| 71 | + return output.number; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Find the bot comment in the issue. |
| 76 | + */ |
| 77 | + async function findBotComment(issueNumber) { |
| 78 | + if (!issueNumber) return null; |
| 79 | + const comments = await github.rest.issues.listComments({ |
| 80 | + owner: context.repo.owner, |
| 81 | + repo: context.repo.repo, |
| 82 | + // eslint-disable-next-line camelcase -- The ID defined in the GitHub API. |
| 83 | + issue_number: issueNumber, |
| 84 | + }); |
| 85 | + return comments.data.find((comment) => |
| 86 | + comment.body.includes(botCommentIdentifier), |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Create or update the bot comment in the issue. |
| 92 | + */ |
| 93 | + async function createOrUpdateComment(issueNumber) { |
| 94 | + const existingComment = await findBotComment(issueNumber); |
| 95 | + if (existingComment) { |
| 96 | + await github.rest.issues.updateComment({ |
| 97 | + owner: context.repo.owner, |
| 98 | + repo: context.repo.repo, |
| 99 | + // eslint-disable-next-line camelcase -- The ID defined in the GitHub API. |
| 100 | + comment_id: existingComment.id, |
| 101 | + body, |
| 102 | + }); |
| 103 | + } else { |
| 104 | + await github.rest.issues.createComment({ |
| 105 | + // eslint-disable-next-line camelcase -- The ID defined in the GitHub API. |
| 106 | + issue_number: issueNumber, |
| 107 | + owner: context.repo.owner, |
| 108 | + repo: context.repo.repo, |
| 109 | + body, |
| 110 | + }); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments