Skip to content

Commit e36ae82

Browse files
authored
chore: add publish workflow and script for pkg.pr.new integration (#4819)
* chore: add publish workflow and script for pkg.pr.new integration * chore: update baseBranch in config.json to use origin/main * fixup!
1 parent b0a2024 commit e36ae82

5 files changed

Lines changed: 204 additions & 1 deletion

File tree

.changeset/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"fixed": [],
55
"linked": [],
66
"access": "public",
7-
"baseBranch": "main",
7+
"baseBranch": "origin/main",
88
"updateInternalDependencies": "patch",
99
"ignore": []
1010
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/* eslint-disable camelcase */
2+
3+
import fs from "node:fs";
4+
5+
/**
6+
* @param {{ github: EXPECTED_ANY, context: EXPECTED_ANY }} params params
7+
*/
8+
export async function run({ github, context }) {
9+
const output = JSON.parse(fs.readFileSync("output.json", "utf8"));
10+
11+
const sha =
12+
context.eventName === "pull_request" ? context.payload.pull_request.head.sha : context.sha;
13+
14+
const commitUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/commit/${sha}`;
15+
16+
const packages = output.packages.map((pkg) => `${pkg.name}@${pkg.url}`).join(" ");
17+
18+
const botCommentIdentifier = "<!-- posted by pkg.pr.new -->";
19+
const body = `${botCommentIdentifier}
20+
This PR is packaged and the instant preview is available (${commitUrl}).
21+
22+
Install it locally:
23+
24+
- npm
25+
26+
\`\`\`shell
27+
npm i -D ${packages}
28+
\`\`\`
29+
30+
- yarn
31+
32+
\`\`\`shell
33+
yarn add -D ${packages}
34+
\`\`\`
35+
36+
- pnpm
37+
38+
\`\`\`shell
39+
pnpm add -D ${packages}
40+
\`\`\`
41+
`;
42+
43+
/**
44+
* @param {number=} issueNumber PR number
45+
* @returns {Promise<EXPECTED_ANY>} the existing bot comment, if any
46+
*/
47+
async function findBotComment(issueNumber) {
48+
if (!issueNumber) return null;
49+
const comments = await github.rest.issues.listComments({
50+
owner: context.repo.owner,
51+
repo: context.repo.repo,
52+
issue_number: issueNumber,
53+
});
54+
return comments.data.find(
55+
(comment) =>
56+
// Prevent unintentional overwriting
57+
comment.user &&
58+
comment.user.login === "github-actions[bot]" &&
59+
comment.body.includes(botCommentIdentifier),
60+
);
61+
}
62+
63+
/**
64+
* @param {number=} issueNumber issue number
65+
* @returns {Promise<void>}
66+
*/
67+
async function createOrUpdateComment(issueNumber) {
68+
if (!issueNumber) {
69+
console.log("No issue number provided. Cannot post or update comment.");
70+
return;
71+
}
72+
73+
const existingComment = await findBotComment(issueNumber);
74+
75+
if (existingComment) {
76+
await github.rest.issues.updateComment({
77+
owner: context.repo.owner,
78+
repo: context.repo.repo,
79+
comment_id: existingComment.id,
80+
body,
81+
});
82+
} else {
83+
await github.rest.issues.createComment({
84+
issue_number: issueNumber,
85+
owner: context.repo.owner,
86+
repo: context.repo.repo,
87+
body,
88+
});
89+
}
90+
}
91+
92+
/**
93+
* @returns {void}
94+
*/
95+
function logPublishInfo() {
96+
console.log(`\n${"=".repeat(50)}`);
97+
console.log("Publish Information");
98+
console.log("=".repeat(50));
99+
console.log("\nPublished Packages:");
100+
console.log(output.packages);
101+
console.log("\nTemplates:");
102+
console.log(output.templates);
103+
console.log(`\nCommit URL: ${commitUrl}`);
104+
console.log(`\n${"=".repeat(50)}`);
105+
}
106+
107+
if (context.eventName === "pull_request") {
108+
if (context.issue.number) {
109+
await createOrUpdateComment(context.issue.number);
110+
}
111+
} else if (context.eventName === "push") {
112+
const { data: prs } = await github.rest.repos.listPullRequestsAssociatedWithCommit({
113+
owner: context.repo.owner,
114+
repo: context.repo.repo,
115+
commit_sha: sha,
116+
});
117+
118+
if (prs.length > 0) {
119+
await createOrUpdateComment(prs[0].number);
120+
} else {
121+
console.log(
122+
"No open pull request found for this push. Logging publish information to console:",
123+
);
124+
logPublishInfo();
125+
}
126+
}
127+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
name: Publish to pkg.pr.new
2+
3+
on:
4+
pull_request:
5+
branches: [main]
6+
push:
7+
branches: [main]
8+
tags: ["!**"]
9+
10+
permissions:
11+
issues: write
12+
pull-requests: write
13+
14+
jobs:
15+
publish:
16+
if: |
17+
github.repository == 'webpack/webpack-cli' &&
18+
(github.event_name != 'pull_request' ||
19+
github.event.pull_request.head.repo.full_name == github.repository)
20+
name: Publish to pkg.pr.new
21+
runs-on: ubuntu-latest
22+
outputs:
23+
sha: ${{ steps.publish.outputs.sha }}
24+
urls: ${{ steps.publish.outputs.urls }}
25+
steps:
26+
- name: Checkout code
27+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
28+
with:
29+
# Changeset needs access to the main branch to find pending changesets
30+
fetch-depth: 0
31+
32+
- name: Setup Node.js
33+
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
34+
with:
35+
node-version: lts/*
36+
cache: "npm"
37+
38+
- name: Install dependencies
39+
run: npm ci
40+
41+
- name: Build
42+
run: npm run build
43+
44+
- name: Compute affected packages
45+
id: affected
46+
run: |
47+
npx changeset status --output changes.json || true
48+
npm query .workspace > workspaces.json
49+
packages=$(jq -r --slurpfile ws workspaces.json '[.releases[] | select(.type != "none") | .name] as $names | $ws[0][] | select(.name | IN($names[])) | "./" + .location' changes.json 2>/dev/null | tr '\n' ' ' || true)
50+
echo "packages=$packages" >> "$GITHUB_OUTPUT"
51+
echo "Affected packages: ${packages:-none}"
52+
53+
- name: Publish
54+
if: steps.affected.outputs.packages != ''
55+
run: npx pkg-pr-new publish --compact --json output.json --comment=off ${{ steps.affected.outputs.packages }}
56+
57+
- name: Add metadata to output
58+
if: steps.affected.outputs.packages != ''
59+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
60+
with:
61+
github-token: ${{ secrets.GITHUB_TOKEN }}
62+
script: |
63+
const { run } = await import('${{ github.workspace }}/.github/scripts/publish-to-pkg-pr-new.mjs');
64+
await run({ github, context });

package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"json5": "^2.2.3",
7777
"lint-staged": "^17.0.8",
7878
"mini-css-extract-plugin": "^2.10.2",
79+
"pkg-pr-new": "^0.0.79",
7980
"prettier": "^3.8.4",
8081
"readable-stream": "^4.7.0",
8182
"sass": "^1.101.0",

0 commit comments

Comments
 (0)