From 9e065ecf18c87df4ab97ff72c206730a49106bd9 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Sun, 26 Jan 2025 16:29:16 +0900 Subject: [PATCH 1/5] chore: update pkg.pr.new job --- .github/workflows/pkg.pr.new.yml | 18 ++++- tools/pkg.pr.new-comment.mjs | 121 +++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 tools/pkg.pr.new-comment.mjs diff --git a/.github/workflows/pkg.pr.new.yml b/.github/workflows/pkg.pr.new.yml index 9a56335e..60cf0ca5 100644 --- a/.github/workflows/pkg.pr.new.yml +++ b/.github/workflows/pkg.pr.new.yml @@ -1,5 +1,10 @@ name: Publish to pkg.pr.new -on: [push, pull_request] +on: + pull_request: + branches: [main] + push: + branches: [main] + tags: ["!**"] jobs: build: @@ -13,4 +18,13 @@ jobs: run: pnpm install - name: Build run: pnpm run build - - run: pnpx pkg-pr-new publish --compact + - run: pnpx pkg-pr-new publish --compact --json output.json --comment=off + - uses: actions/github-script@v7 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const fs = require('fs'); + const output = JSON.parse(fs.readFileSync('output.json', 'utf8')); + const { default: process } = await import('${{ github.workspace }}/tools/pkg.pr.new-comment.mjs') + + await process({github, context, core, output}) diff --git a/tools/pkg.pr.new-comment.mjs b/tools/pkg.pr.new-comment.mjs new file mode 100644 index 00000000..a58051c4 --- /dev/null +++ b/tools/pkg.pr.new-comment.mjs @@ -0,0 +1,121 @@ +/** + * Used in `/.github/workflows/pkg.pr.new.yml` + */ +export default async function ({ github, context, output }) { + // eslint-disable-next-line no-console -- For debugging on github actions. + console.log("pkg-pr-new publish output:", JSON.stringify(output)); + + const sha = + context.event_name === "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 packages = output.packages.map((p) => { + const normalizedUrl = + pullRequestNumber && p.url.endsWith(sha) + ? `${p.url.slice(0, -sha.length)}${pullRequestNumber}` + : p.url; + return { + name: p.name, + url: normalizedUrl, + }; + }); + + const botCommentIdentifier = ""; + + const onlineUrl = new URL( + "https://eslint-online-playground.netlify.app/#eslint-plugin-svelte%20with%20typescript", + ); + const overrideDeps = {}; + for (const p of packages) { + overrideDeps[p.name] = p.url; + } + onlineUrl.searchParams.set("overrideDeps", JSON.stringify(overrideDeps)); + const body = `${botCommentIdentifier} + +## Try the Instant Preview in Online Playground + +[ESLint Online Playground](${onlineUrl}) + +## Install the Instant Preview to Your Local + +\`\`\` +npm i ${packages.map((p) => p.url).join(" ")} +\`\`\` + +## Published Instant Preview Packages: + +${packages.map((p) => `- ${p.name}: ${p.url}`).join("\n")} + +[View Commit](${commitUrl})`; + + if (pullRequestNumber) { + await createOrUpdateComment(pullRequestNumber); + } else { + /* eslint-disable no-console -- For debugging on github actions. */ + 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)}`); + /* eslint-enable no-console -- For debugging on github actions. */ + } + + 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) { + if (!issueNumber) return null; + const comments = await github.rest.issues.listComments({ + owner: context.repo.owner, + repo: context.repo.repo, + // eslint-disable-next-line camelcase -- The ID defined in the GitHub API. + 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, + // eslint-disable-next-line camelcase -- The ID defined in the GitHub API. + comment_id: existingComment.id, + body, + }); + } else { + await github.rest.issues.createComment({ + // eslint-disable-next-line camelcase -- The ID defined in the GitHub API. + issue_number: issueNumber, + owner: context.repo.owner, + repo: context.repo.repo, + body, + }); + } + } +} From adb30038e1dd41f221688b8dc88abdd529201865 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Sun, 26 Jan 2025 16:32:29 +0900 Subject: [PATCH 2/5] debug --- tools/pkg.pr.new-comment.mjs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tools/pkg.pr.new-comment.mjs b/tools/pkg.pr.new-comment.mjs index a58051c4..09ed932f 100644 --- a/tools/pkg.pr.new-comment.mjs +++ b/tools/pkg.pr.new-comment.mjs @@ -5,6 +5,11 @@ export default async function ({ github, context, output }) { // eslint-disable-next-line no-console -- For debugging on github actions. console.log("pkg-pr-new publish output:", JSON.stringify(output)); + if (context.event_name === "pull_request") { + // eslint-disable-next-line no-console -- For debugging on github actions. + console.log("Pull Request Event", JSON.stringify(context)); + } + const sha = context.event_name === "pull_request" ? context.payload.pull_request.head.sha From 5ea91e7139bf17286fb2ecbb78b9e15e6c421dd8 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Sun, 26 Jan 2025 16:33:53 +0900 Subject: [PATCH 3/5] debug --- tools/pkg.pr.new-comment.mjs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tools/pkg.pr.new-comment.mjs b/tools/pkg.pr.new-comment.mjs index 09ed932f..12573eae 100644 --- a/tools/pkg.pr.new-comment.mjs +++ b/tools/pkg.pr.new-comment.mjs @@ -5,10 +5,8 @@ export default async function ({ github, context, output }) { // eslint-disable-next-line no-console -- For debugging on github actions. console.log("pkg-pr-new publish output:", JSON.stringify(output)); - if (context.event_name === "pull_request") { - // eslint-disable-next-line no-console -- For debugging on github actions. - console.log("Pull Request Event", JSON.stringify(context)); - } + // eslint-disable-next-line no-console -- For debugging on github actions. + console.log("context: ", JSON.stringify(context)); const sha = context.event_name === "pull_request" From dae3f11df61d6e8ddd2faeec77b231090dad1f24 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Sun, 26 Jan 2025 16:37:18 +0900 Subject: [PATCH 4/5] update --- tools/pkg.pr.new-comment.mjs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tools/pkg.pr.new-comment.mjs b/tools/pkg.pr.new-comment.mjs index 12573eae..ba45bb77 100644 --- a/tools/pkg.pr.new-comment.mjs +++ b/tools/pkg.pr.new-comment.mjs @@ -5,11 +5,8 @@ export default async function ({ github, context, output }) { // eslint-disable-next-line no-console -- For debugging on github actions. console.log("pkg-pr-new publish output:", JSON.stringify(output)); - // eslint-disable-next-line no-console -- For debugging on github actions. - console.log("context: ", JSON.stringify(context)); - const sha = - context.event_name === "pull_request" + 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}`; From 9305248261cafdef567b66589024ed10d06d66fa Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Sun, 26 Jan 2025 16:43:39 +0900 Subject: [PATCH 5/5] update --- tools/pkg.pr.new-comment.mjs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tools/pkg.pr.new-comment.mjs b/tools/pkg.pr.new-comment.mjs index ba45bb77..689971e2 100644 --- a/tools/pkg.pr.new-comment.mjs +++ b/tools/pkg.pr.new-comment.mjs @@ -14,10 +14,13 @@ export default async function ({ github, context, output }) { const pullRequestNumber = await getPullRequestNumber(); const packages = output.packages.map((p) => { - const normalizedUrl = - pullRequestNumber && p.url.endsWith(sha) - ? `${p.url.slice(0, -sha.length)}${pullRequestNumber}` - : p.url; + let normalizedUrl = p.url; + if (pullRequestNumber && p.url.endsWith(sha)) { + normalizedUrl = `${p.url.slice(0, -sha.length)}${pullRequestNumber}`; + } + const repoPath = `/${context.repo.owner}/${context.repo.repo}/`; + normalizedUrl = normalizedUrl.replace(repoPath, "/"); + return { name: p.name, url: normalizedUrl,