Skip to content

Commit 530e3a8

Browse files
committed
chore: add custom comment with link to playground for pkg.pr.new
1 parent e6dd871 commit 530e3a8

File tree

1 file changed

+91
-1
lines changed

1 file changed

+91
-1
lines changed

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

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,94 @@ 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: Post or update comment
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+
33+
const packages = output.packages
34+
.map((t) => `\`\`\`
35+
pnpm add ${t.url}
36+
\`\`\``)
37+
.join('\n');
38+
39+
const body = (number)=>`## \`pkg.pr.new\` for ${context.sha.substring(0, 7)}
40+
${packages}
41+
42+
[Open Playground](https://svelte.dev/playground?version=pr-${number})
43+
`;
44+
45+
const bot_comment_identifier = `## \`pkg.pr.new\` for ${context.sha.substring(0, 7)}`;
46+
47+
async function find_bot_comment(issue_number) {
48+
if (!issue_number) return null;
49+
const comments = await github.rest.issues.listComments({
50+
owner: context.repo.owner,
51+
repo: context.repo.repo,
52+
issue_number: issue_number,
53+
});
54+
return comments.data.find((comment) =>
55+
comment.body.includes(bot_comment_identifier)
56+
);
57+
}
58+
59+
async function create_or_update_comment(issue_number) {
60+
if (!issue_number) {
61+
console.log('No issue number provided. Cannot post or update comment.');
62+
return;
63+
}
64+
65+
const existing_comment = await find_bot_comment(issue_number);
66+
if (existing_comment) {
67+
await github.rest.issues.updateComment({
68+
owner: context.repo.owner,
69+
repo: context.repo.repo,
70+
comment_id: existing_comment.id,
71+
body: body(issue_number),
72+
});
73+
} else {
74+
await github.rest.issues.createComment({
75+
issue_number: issue_number,
76+
owner: context.repo.owner,
77+
repo: context.repo.repo,
78+
body: body(issue_number),
79+
});
80+
}
81+
}
82+
83+
async function log_publish_info() {
84+
console.log('\n' + '='.repeat(50));
85+
console.log('Publish Information');
86+
console.log('='.repeat(50));
87+
console.log('\nPublished Packages:');
88+
console.log(packages);
89+
console.log('\nPlayground URL:');
90+
console.log(`\nhttps://svelte.dev/playground?version=commit-${context.sha}`)
91+
console.log('\n' + '='.repeat(50));
92+
}
93+
94+
if (context.eventName === 'pull_request') {
95+
if (context.issue.number) {
96+
await create_or_update_comment(context.issue.number);
97+
}
98+
} else if (context.eventName === 'push') {
99+
const pull_requests = await github.rest.pulls.list({
100+
owner: context.repo.owner,
101+
repo: context.repo.repo,
102+
state: 'open',
103+
head: `${context.repo.owner}:${context.ref.replace('refs/heads/', '')}`,
104+
});
105+
106+
if (pull_requests.data.length > 0) {
107+
await create_or_update_comment(pull_requests.data[0].number);
108+
} else {
109+
console.log(
110+
'No open pull request found for this push. Logging publish information to console:'
111+
);
112+
await log_publish_info();
113+
}
114+
}

0 commit comments

Comments
 (0)