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