Skip to content

Commit 9e065ec

Browse files
committed
chore: update pkg.pr.new job
1 parent 84872c6 commit 9e065ec

File tree

2 files changed

+137
-2
lines changed

2 files changed

+137
-2
lines changed

.github/workflows/pkg.pr.new.yml

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
name: Publish to pkg.pr.new
2-
on: [push, pull_request]
2+
on:
3+
pull_request:
4+
branches: [main]
5+
push:
6+
branches: [main]
7+
tags: ["!**"]
38

49
jobs:
510
build:
@@ -13,4 +18,13 @@ jobs:
1318
run: pnpm install
1419
- name: Build
1520
run: pnpm run build
16-
- run: pnpx pkg-pr-new publish --compact
21+
- run: pnpx pkg-pr-new publish --compact --json output.json --comment=off
22+
- uses: actions/github-script@v7
23+
with:
24+
github-token: ${{ secrets.GITHUB_TOKEN }}
25+
script: |
26+
const fs = require('fs');
27+
const output = JSON.parse(fs.readFileSync('output.json', 'utf8'));
28+
const { default: process } = await import('${{ github.workspace }}/tools/pkg.pr.new-comment.mjs')
29+
30+
await process({github, context, core, output})

tools/pkg.pr.new-comment.mjs

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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.event_name === "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+
const normalizedUrl =
18+
pullRequestNumber && p.url.endsWith(sha)
19+
? `${p.url.slice(0, -sha.length)}${pullRequestNumber}`
20+
: p.url;
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-svelte%20with%20typescript",
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 in Online Playground
40+
41+
[ESLint Online Playground](${onlineUrl})
42+
43+
## Install the Instant Preview to Your Local
44+
45+
\`\`\`
46+
npm i ${packages.map((p) => p.url).join(" ")}
47+
\`\`\`
48+
49+
## Published Instant Preview Packages:
50+
51+
${packages.map((p) => `- ${p.name}: ${p.url}`).join("\n")}
52+
53+
[View Commit](${commitUrl})`;
54+
55+
if (pullRequestNumber) {
56+
await createOrUpdateComment(pullRequestNumber);
57+
} else {
58+
/* eslint-disable no-console -- For debugging on github actions. */
59+
console.log(
60+
"No open pull request found for this push. Logging publish information to console:",
61+
);
62+
console.log(`\n${"=".repeat(50)}`);
63+
console.log(body);
64+
console.log(`\n${"=".repeat(50)}`);
65+
/* eslint-enable no-console -- For debugging on github actions. */
66+
}
67+
68+
async function getPullRequestNumber() {
69+
if (context.eventName === "pull_request") {
70+
if (context.issue.number) {
71+
return context.issue.number;
72+
}
73+
} else if (context.eventName === "push") {
74+
const pullRequests = await github.rest.pulls.list({
75+
owner: context.repo.owner,
76+
repo: context.repo.repo,
77+
state: "open",
78+
head: `${context.repo.owner}:${context.ref.replace("refs/heads/", "")}`,
79+
});
80+
81+
if (pullRequests.data.length > 0) {
82+
return pullRequests.data[0].number;
83+
}
84+
}
85+
return null;
86+
}
87+
88+
async function findBotComment(issueNumber) {
89+
if (!issueNumber) return null;
90+
const comments = await github.rest.issues.listComments({
91+
owner: context.repo.owner,
92+
repo: context.repo.repo,
93+
// eslint-disable-next-line camelcase -- The ID defined in the GitHub API.
94+
issue_number: issueNumber,
95+
});
96+
return comments.data.find((comment) =>
97+
comment.body.includes(botCommentIdentifier),
98+
);
99+
}
100+
101+
async function createOrUpdateComment(issueNumber) {
102+
const existingComment = await findBotComment(issueNumber);
103+
if (existingComment) {
104+
await github.rest.issues.updateComment({
105+
owner: context.repo.owner,
106+
repo: context.repo.repo,
107+
// eslint-disable-next-line camelcase -- The ID defined in the GitHub API.
108+
comment_id: existingComment.id,
109+
body,
110+
});
111+
} else {
112+
await github.rest.issues.createComment({
113+
// eslint-disable-next-line camelcase -- The ID defined in the GitHub API.
114+
issue_number: issueNumber,
115+
owner: context.repo.owner,
116+
repo: context.repo.repo,
117+
body,
118+
});
119+
}
120+
}
121+
}

0 commit comments

Comments
 (0)