Skip to content

Commit 0ce4b55

Browse files
chore: add custom comment with link to playground for pkg.pr.new (#14151)
* chore: add custom comment with link to playground for `pkg.pr.new` * chore: small updates to the script output * chore: refine comment a bit, include multiple packages * tweak comment * chore: two steps workflow * chore: update workflow --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 3d67cd5 commit 0ce4b55

File tree

2 files changed

+124
-1
lines changed

2 files changed

+124
-1
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
name: Update pkg.pr.new comment
2+
3+
on:
4+
workflow_run:
5+
workflows: ['Publish Any Commit']
6+
types:
7+
- completed
8+
9+
jobs:
10+
build:
11+
name: 'Update comment'
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Download artifact
15+
uses: actions/download-artifact@v4
16+
with:
17+
name: output.json
18+
path: output.json
19+
github-token: ${{ secrets.GITHUB_TOKEN }}
20+
run-id: ${{ github.event.workflow_run.id }}
21+
- name: 'Post or update comment'
22+
uses: actions/github-script@v6
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+
29+
const bot_comment_identifier = `<!-- pkg.pr.new comment -->`;
30+
31+
const body = (number) => `${bot_comment_identifier}
32+
33+
[Playground](https://svelte.dev/playground?version=pr-${number})
34+
35+
\`\`\`
36+
${output.packages.map((p) => `pnpm add https://pkg.pr.new/${p.name}@${number}`).join('\n')}
37+
\`\`\`
38+
`;
39+
40+
async function find_bot_comment(issue_number) {
41+
if (!issue_number) return null;
42+
const comments = await github.rest.issues.listComments({
43+
owner: context.repo.owner,
44+
repo: context.repo.repo,
45+
issue_number: issue_number,
46+
});
47+
return comments.data.find((comment) =>
48+
comment.body.includes(bot_comment_identifier)
49+
);
50+
}
51+
52+
async function create_or_update_comment(issue_number) {
53+
if (!issue_number) {
54+
console.log('No issue number provided. Cannot post or update comment.');
55+
return;
56+
}
57+
58+
const existing_comment = await find_bot_comment(issue_number);
59+
if (existing_comment) {
60+
await github.rest.issues.updateComment({
61+
owner: context.repo.owner,
62+
repo: context.repo.repo,
63+
comment_id: existing_comment.id,
64+
body: body(issue_number),
65+
});
66+
} else {
67+
await github.rest.issues.createComment({
68+
issue_number: issue_number,
69+
owner: context.repo.owner,
70+
repo: context.repo.repo,
71+
body: body(issue_number),
72+
});
73+
}
74+
}
75+
76+
async function log_publish_info() {
77+
console.log('\n' + '='.repeat(50));
78+
console.log('Publish Information');
79+
console.log('='.repeat(50));
80+
console.log('\nPublished Packages:');
81+
console.log(packages);
82+
console.log('\nPlayground URL:');
83+
console.log(`\nhttps://svelte.dev/playground?version=commit-${output.sha.substring(0, 7)}`)
84+
console.log('\n' + '='.repeat(50));
85+
}
86+
87+
if (output.event_name === 'pull_request') {
88+
if (context.issue.number) {
89+
await create_or_update_comment(context.issue.number);
90+
}
91+
} else if (output.event_name === 'push') {
92+
const pull_requests = await github.rest.pulls.list({
93+
owner: context.repo.owner,
94+
repo: context.repo.repo,
95+
state: 'open',
96+
head: `${context.repo.owner}:${output.ref.replace('refs/heads/', '')}`,
97+
});
98+
99+
if (pull_requests.data.length > 0) {
100+
await create_or_update_comment(pull_requests.data[0].number);
101+
} else {
102+
console.log(
103+
'No open pull request found for this push. Logging publish information to console:'
104+
);
105+
await log_publish_info();
106+
}
107+
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,20 @@ jobs:
2121
- name: Build
2222
run: pnpm build
2323

24-
- run: pnpx pkg-pr-new publish --compact --no-template './packages/svelte'
24+
- run: pnpx pkg-pr-new publish --comment=off --json output.json --compact --no-template './packages/svelte'
25+
- name: Add metadata to output
26+
uses: actions/github-script@v6
27+
with:
28+
github-token: ${{ secrets.GITHUB_TOKEN }}
29+
script: |
30+
const fs = require('fs');
31+
const output = JSON.parse(fs.readFileSync('output.json', 'utf8'));
32+
output.number = context.issue.number;
33+
output.event_name = context.eventName;
34+
output.ref = context.ref;
35+
fs.writeFileSync('output.json', JSON.stringify(output), 'utf8');
36+
- name: Upload output
37+
uses: actions/upload-artifact@v4
38+
with:
39+
name: output.json
40+
path: output.json

0 commit comments

Comments
 (0)