Skip to content

Commit 8100635

Browse files
authored
add pkg.pr.new workflows (#13676)
1 parent 50d9072 commit 8100635

File tree

2 files changed

+155
-0
lines changed

2 files changed

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

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

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Publish Any Commit
2+
on: [push, pull_request]
3+
4+
jobs:
5+
build:
6+
permissions: {}
7+
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- name: Checkout code
12+
uses: actions/checkout@v4
13+
14+
- name: install corepack
15+
run: npm i -g [email protected]
16+
17+
- run: corepack enable
18+
- uses: actions/setup-node@v4
19+
with:
20+
node-version: 18.x
21+
cache: pnpm
22+
23+
- name: Install dependencies
24+
run: pnpm install --frozen-lockfile
25+
26+
- name: Build
27+
run: pnpm build
28+
29+
- run: pnpx pkg-pr-new publish --comment=off --json output.json --compact --no-template './packages/*'
30+
- name: Add metadata to output
31+
uses: actions/github-script@v6
32+
with:
33+
github-token: ${{ secrets.GITHUB_TOKEN }}
34+
script: |
35+
const fs = require('fs');
36+
const output = JSON.parse(fs.readFileSync('output.json', 'utf8'));
37+
output.number = context.issue.number;
38+
output.event_name = context.eventName;
39+
output.ref = context.ref;
40+
fs.writeFileSync('output.json', JSON.stringify(output), 'utf8');
41+
- name: Upload output
42+
uses: actions/upload-artifact@v4
43+
with:
44+
name: output
45+
path: ./output.json
46+
47+
- run: ls -R .

0 commit comments

Comments
 (0)