Skip to content

Commit 022040e

Browse files
committed
chore: two steps workflow
1 parent fb68c5f commit 022040e

File tree

2 files changed

+117
-81
lines changed

2 files changed

+117
-81
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+
run:
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: 10 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -22,90 +22,19 @@ jobs:
2222
run: pnpm build
2323

2424
- run: pnpx pkg-pr-new publish --comment=off --json output.json --compact --no-template './packages/svelte'
25-
- name: Post or update comment
25+
- name: Add metadata to output
2626
uses: actions/github-script@v6
2727
with:
2828
github-token: ${{ secrets.GITHUB_TOKEN }}
2929
script: |
3030
const fs = require('fs');
3131
const output = JSON.parse(fs.readFileSync('output.json', 'utf8'));
32-
33-
const bot_comment_identifier = `<!-- pkg.pr.new comment -->`;
34-
35-
const body = (number) => `${bot_comment_identifier}
36-
37-
[Playground](https://svelte.dev/playground?version=pr-${number})
38-
39-
\`\`\`
40-
${output.packages.map((p) => `pnpm add https://pkg.pr.new/${p.name}@${number}`).join('\n')}
41-
\`\`\`
42-
`;
43-
44-
async function find_bot_comment(issue_number) {
45-
if (!issue_number) return null;
46-
const comments = await github.rest.issues.listComments({
47-
owner: context.repo.owner,
48-
repo: context.repo.repo,
49-
issue_number: issue_number,
50-
});
51-
return comments.data.find((comment) =>
52-
comment.body.includes(bot_comment_identifier)
53-
);
54-
}
55-
56-
async function create_or_update_comment(issue_number) {
57-
if (!issue_number) {
58-
console.log('No issue number provided. Cannot post or update comment.');
59-
return;
60-
}
61-
62-
const existing_comment = await find_bot_comment(issue_number);
63-
if (existing_comment) {
64-
await github.rest.issues.updateComment({
65-
owner: context.repo.owner,
66-
repo: context.repo.repo,
67-
comment_id: existing_comment.id,
68-
body: body(issue_number),
69-
});
70-
} else {
71-
await github.rest.issues.createComment({
72-
issue_number: issue_number,
73-
owner: context.repo.owner,
74-
repo: context.repo.repo,
75-
body: body(issue_number),
76-
});
77-
}
78-
}
79-
80-
async function log_publish_info() {
81-
console.log('\n' + '='.repeat(50));
82-
console.log('Publish Information');
83-
console.log('='.repeat(50));
84-
console.log('\nPublished Packages:');
85-
console.log(packages);
86-
console.log('\nPlayground URL:');
87-
console.log(`\nhttps://svelte.dev/playground?version=commit-${context.sha.substring(0, 7)}`)
88-
console.log('\n' + '='.repeat(50));
89-
}
90-
91-
if (context.eventName === 'pull_request') {
92-
if (context.issue.number) {
93-
await create_or_update_comment(context.issue.number);
94-
}
95-
} else if (context.eventName === 'push') {
96-
const pull_requests = await github.rest.pulls.list({
97-
owner: context.repo.owner,
98-
repo: context.repo.repo,
99-
state: 'open',
100-
head: `${context.repo.owner}:${context.ref.replace('refs/heads/', '')}`,
101-
});
102-
103-
if (pull_requests.data.length > 0) {
104-
await create_or_update_comment(pull_requests.data[0].number);
105-
} else {
106-
console.log(
107-
'No open pull request found for this push. Logging publish information to console:'
108-
);
109-
await log_publish_info();
110-
}
111-
}
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)