Skip to content

Commit 092ed0b

Browse files
authored
chore: update pkg.pr.new workflow (take 2) (#1057)
1 parent c7155c3 commit 092ed0b

File tree

3 files changed

+88
-105
lines changed

3 files changed

+88
-105
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ jobs:
1212
name: 'Update comment'
1313
runs-on: ubuntu-latest
1414
steps:
15+
- uses: actions/checkout@v4
1516
- name: Download artifact
1617
uses: actions/download-artifact@v4
1718
with:
@@ -20,7 +21,7 @@ jobs:
2021
run-id: ${{ github.event.workflow_run.id }}
2122
- run: ls -R .
2223
- name: 'Post or update comment'
23-
uses: actions/github-script@v6
24+
uses: actions/github-script@v7
2425
with:
2526
github-token: ${{ secrets.GITHUB_TOKEN }}
2627
script: |

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

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ jobs:
3030
output.number = context.issue.number;
3131
output.event_name = context.eventName;
3232
output.ref = context.ref;
33+
output.sha = context.eventName === 'pull_request'
34+
? context.payload.pull_request.head.sha
35+
: context.payload.after;
3336
fs.writeFileSync('output.json', JSON.stringify(output), 'utf8');
3437
- name: Upload output
3538
uses: actions/upload-artifact@v4

tools/pkg.pr.new-comment.mjs

+83-104
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,39 @@
22
* Used in `/.github/workflows/pkg.pr.new.yml`
33
*/
44
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}
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-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}
4138
4239
## Try the Instant Preview in Online Playground
4340
@@ -46,79 +43,61 @@ export default async function ({ github, context, output }) {
4643
## Install the Instant Preview to Your Local
4744
4845
\`\`\`
49-
npm i ${packages.map((p) => p.url).join(" ")}
46+
npm i ${packages.map((p) => p.url).join(' ')}
5047
\`\`\`
5148
5249
## Published Instant Preview Packages:
5350
54-
${packages.map((p) => `- ${p.name}: ${p.url}`).join("\n")}
51+
${packages.map((p) => `- ${p.name}: ${p.url}`).join('\n')}
5552
5653
[View Commit](${commitUrl})`;
5754

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-
}
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+
return output.number;
70+
}
71+
72+
async function findBotComment(issueNumber) {
73+
if (!issueNumber) return null;
74+
const comments = await github.rest.issues.listComments({
75+
owner: context.repo.owner,
76+
repo: context.repo.repo,
77+
// eslint-disable-next-line camelcase -- The ID defined in the GitHub API.
78+
issue_number: issueNumber
79+
});
80+
return comments.data.find((comment) => comment.body.includes(botCommentIdentifier));
81+
}
82+
83+
async function createOrUpdateComment(issueNumber) {
84+
const existingComment = await findBotComment(issueNumber);
85+
if (existingComment) {
86+
await github.rest.issues.updateComment({
87+
owner: context.repo.owner,
88+
repo: context.repo.repo,
89+
// eslint-disable-next-line camelcase -- The ID defined in the GitHub API.
90+
comment_id: existingComment.id,
91+
body
92+
});
93+
} else {
94+
await github.rest.issues.createComment({
95+
// eslint-disable-next-line camelcase -- The ID defined in the GitHub API.
96+
issue_number: issueNumber,
97+
owner: context.repo.owner,
98+
repo: context.repo.repo,
99+
body
100+
});
101+
}
102+
}
124103
}

0 commit comments

Comments
 (0)