Skip to content

Commit 4837e8f

Browse files
committed
Try to set up pkg.pr.new
1 parent 3b40210 commit 4837e8f

3 files changed

Lines changed: 737 additions & 3 deletions

File tree

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
name: Publish approved pull requests and latest commit to pkg.pr.new
2+
on:
3+
pull_request:
4+
types:
5+
- opened
6+
branches:
7+
- 'dev-2.0'
8+
push:
9+
branches:
10+
- 'dev-2.0'
11+
tags:
12+
- '!**'
13+
14+
permissions: {}
15+
16+
jobs:
17+
publish:
18+
runs-on: ubuntu-latest
19+
steps:
20+
- name: Checkout code
21+
uses: actions/checkout@v4
22+
- uses: actions/setup-node@v4
23+
with:
24+
node-version: 22
25+
26+
- name: Install dependencies
27+
run: npm ci
28+
29+
- name: Build library
30+
run: npm run build
31+
32+
- name: Publish library
33+
run: npx pkg-pr-new publish --no-template --json output.json --comment=off
34+
35+
- name: Post or update comment
36+
uses: actions/github-script@v8
37+
with:
38+
github-token: ${{ secrets.GITHUB_TOKEN }}
39+
script: |
40+
const fs = require('fs');
41+
const output = JSON.parse(fs.readFileSync('output.json', 'utf8'));
42+
43+
const packages = output.packages
44+
.map((p) => `- ${p.name}: ${p.url}`)
45+
.join('\n');
46+
47+
const cdnLinks = output.packages
48+
.map((p) => `- ${p.name}: ${p.url.replace('pkg.pr.new', 'raw.esm.sh/pr')}/lib/p5.min.js`)
49+
.join('\n');
50+
51+
const sha =
52+
context.event_name === 'pull_request'
53+
? context.payload.pull_request.head.sha
54+
: context.payload.after;
55+
56+
const commitUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/commit/${sha}`;
57+
58+
const body = `## Continuous Release
59+
60+
### CDN link
61+
62+
${cdnLinks}
63+
64+
### Published Packages:
65+
66+
${packages}
67+
68+
[View Commit](${commitUrl})
69+
70+
---
71+
72+
_This is an automated message._`;
73+
74+
const botCommentIdentifier = '## Continuous Release';
75+
76+
async function findBotComment(issueNumber) {
77+
if (!issueNumber) return null;
78+
const comments = await github.rest.issues.listComments({
79+
owner: context.repo.owner,
80+
repo: context.repo.repo,
81+
issue_number: issueNumber,
82+
});
83+
return comments.data.find((comment) =>
84+
comment.body.includes(botCommentIdentifier)
85+
);
86+
}
87+
88+
async function createOrUpdateComment(issueNumber) {
89+
if (!issueNumber) {
90+
console.log('No issue number provided. Cannot post or update comment.');
91+
return;
92+
}
93+
94+
const existingComment = await findBotComment(issueNumber);
95+
if (existingComment) {
96+
await github.rest.issues.updateComment({
97+
owner: context.repo.owner,
98+
repo: context.repo.repo,
99+
comment_id: existingComment.id,
100+
body: body,
101+
});
102+
} else {
103+
await github.rest.issues.createComment({
104+
issue_number: issueNumber,
105+
owner: context.repo.owner,
106+
repo: context.repo.repo,
107+
body: body,
108+
});
109+
}
110+
}
111+
112+
async function logPublishInfo() {
113+
console.log('\n' + '='.repeat(50));
114+
console.log('Publish Information');
115+
console.log('='.repeat(50));
116+
console.log('\nPublished Packages:');
117+
console.log(packages);
118+
console.log('\nTemplates:');
119+
console.log(templates);
120+
console.log(`\nCommit URL: ${commitUrl}`);
121+
console.log('\n' + '='.repeat(50));
122+
}
123+
124+
if (context.eventName === 'pull_request') {
125+
if (context.issue.number) {
126+
await createOrUpdateComment(context.issue.number);
127+
}
128+
} else if (context.eventName === 'push') {
129+
const pullRequests = await github.rest.pulls.list({
130+
owner: context.repo.owner,
131+
repo: context.repo.repo,
132+
state: 'open',
133+
head: `${context.repo.owner}:${context.ref.replace(
134+
'refs/heads/',
135+
''
136+
)}`,
137+
});
138+
139+
if (pullRequests.data.length > 0) {
140+
await createOrUpdateComment(pullRequests.data[0].number);
141+
} else {
142+
console.log(
143+
'No open pull request found for this push. Logging publish information to console:'
144+
);
145+
await logPublishInfo();
146+
}
147+
}

0 commit comments

Comments
 (0)