diff --git a/.github/workflows/create-blog-post.yml b/.github/workflows/create-blog-post.yml new file mode 100644 index 00000000..29d201af --- /dev/null +++ b/.github/workflows/create-blog-post.yml @@ -0,0 +1,60 @@ +name: Create Blog Post + +on: + workflow_call: + inputs: + tag: + description: 'Release tag (e.g. v5.99.0)' + required: true + type: string + workflow_dispatch: + inputs: + tag: + description: 'Release tag (e.g. v5.99.0)' + required: true + type: string +permissions: + contents: write + pull-requests: write + +jobs: + create-post: + runs-on: ubuntu-latest + env: + TAG: ${{ inputs.tag }} + AUTHOR: ${{ github.actor }} + + steps: + - name: Checkout + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + with: + persist-credentials: false + + - name: Setup Node.js + uses: actions/setup-node@6044e13b5dc448c55e2357c09f80417699197238 # v6.2.0 + with: + node-version: lts/* + cache: 'npm' + + - name: Install dependencies + run: npm ci + + - name: Create post + shell: bash + run: | + set -euo pipefail + node scripts/release/create-post.mjs "$TAG" --author="$AUTHOR" + node --run format + + - name: Open pull request + uses: gr2m/create-or-update-pull-request-action@b65137ca591da0b9f43bad7b24df13050ea45d1b # v1.10.1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + commit-message: 'chore: blog post for webpack@${{ env.TAG }}' + title: 'chore: blog post for webpack@${{ env.TAG }}' + body: | + Automated blog post by the `${{ env.TAG }}` webpack release. + branch: 'chore/blog-${{ env.TAG }}' + author: 'github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>' + draft: true diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index da0e7d86..32c34f8e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -36,15 +36,25 @@ jobs: shell: bash run: | set -euo pipefail - node scripts/update-versions.mjs "$TAG" + node scripts/release/update-versions.mjs "$TAG" - name: Open pull request - uses: peter-evans/create-pull-request@5f6978faf089d4d20b00c7766989d076bb2fc7f1 # v8.1.1 + uses: gr2m/create-or-update-pull-request-action@b65137ca591da0b9f43bad7b24df13050ea45d1b # v1.10.1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: - token: ${{ secrets.GITHUB_TOKEN }} commit-message: 'chore: sync to webpack@${{ env.TAG }}' title: 'chore: sync to webpack@${{ env.TAG }}' body: | Automated version sync triggered by the `${{ env.TAG }}` webpack release. branch: 'update-versions' + author: 'github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>' draft: true + + blog-post: + uses: ./.github/workflows/create-blog-post.yml + permissions: + contents: write + pull-requests: write + with: + tag: ${{ inputs.tag }} diff --git a/.prettierignore b/.prettierignore index c0258d5b..a8191ea4 100644 --- a/.prettierignore +++ b/.prettierignore @@ -8,4 +8,5 @@ out /pages/docs/plugins/* !/pages/docs/plugins/index.md versions.json -/generated \ No newline at end of file +/generated +*.hbs \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 7ffab150..4431590f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26,6 +26,7 @@ "eslint": "^10.4.0", "feed": "^6.0.0", "globals": "^17.6.0", + "handlebars": "^4.7.9", "husky": "^9.1.7", "lint-staged": "^17.0.5", "prettier": "^3.8.3", @@ -5443,6 +5444,38 @@ "node": ">=6.0" } }, + "node_modules/handlebars": { + "version": "4.7.9", + "resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.7.9.tgz", + "integrity": "sha512-4E71E0rpOaQuJR2A3xDZ+GM1HyWYv1clR58tC8emQNeQe3RH7MAzSbat+V0wG78LQBo6m6bzSG/L4pBuCsgnUQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "minimist": "^1.2.5", + "neo-async": "^2.6.2", + "source-map": "^0.6.1", + "wordwrap": "^1.0.0" + }, + "bin": { + "handlebars": "bin/handlebars" + }, + "engines": { + "node": ">=0.4.7" + }, + "optionalDependencies": { + "uglify-js": "^3.1.4" + } + }, + "node_modules/handlebars/node_modules/source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "dev": true, + "license": "BSD-3-Clause", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/has-bigints": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/has-bigints/-/has-bigints-1.1.0.tgz", @@ -8426,6 +8459,16 @@ "url": "https://github.com/sponsors/isaacs" } }, + "node_modules/minimist": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz", + "integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==", + "dev": true, + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/minimizer-webpack-plugin": { "version": "5.6.1", "resolved": "https://registry.npmjs.org/minimizer-webpack-plugin/-/minimizer-webpack-plugin-5.6.1.tgz", @@ -11131,6 +11174,20 @@ "integrity": "sha512-ARDJmphmdvUk6Glw7y9DQ2bFkKBHwQHLi2lsaH6PPmz/Ka9sFOBsBluozhDltWmnv9u/cF6Rt87znRTPV+yp/A==", "license": "MIT" }, + "node_modules/uglify-js": { + "version": "3.19.3", + "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.19.3.tgz", + "integrity": "sha512-v3Xu+yuwBXisp6QYTcH4UbH+xYJXqnq2m/LtQVWKWzYc1iehYnLixoQDN9FH6/j9/oybfd6W9Ghwkl8+UMKTKQ==", + "dev": true, + "license": "BSD-2-Clause", + "optional": true, + "bin": { + "uglifyjs": "bin/uglifyjs" + }, + "engines": { + "node": ">=0.8.0" + } + }, "node_modules/unbox-primitive": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/unbox-primitive/-/unbox-primitive-1.1.0.tgz", @@ -11714,6 +11771,13 @@ "node": ">=0.10.0" } }, + "node_modules/wordwrap": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-1.0.0.tgz", + "integrity": "sha512-gvVzJFlPycKc5dZN4yPkP8w7Dc37BtP1yczEneOb4uq34pXZcvrtRTmWV8W+Ume+XCxKgbjM+nevkyFPMybd4Q==", + "dev": true, + "license": "MIT" + }, "node_modules/wrap-ansi": { "version": "10.0.0", "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-10.0.0.tgz", diff --git a/package.json b/package.json index 58753421..a458bcdc 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "eslint": "^10.4.0", "feed": "^6.0.0", "globals": "^17.6.0", + "handlebars": "^4.7.9", "husky": "^9.1.7", "lint-staged": "^17.0.5", "prettier": "^3.8.3", diff --git a/scripts/markdown/governance.mjs b/scripts/markdown/governance.mjs index 9fe6a6be..e4fc4723 100644 --- a/scripts/markdown/governance.mjs +++ b/scripts/markdown/governance.mjs @@ -1,19 +1,8 @@ import { mkdir, writeFile } from 'node:fs/promises'; import { join } from 'node:path'; -import { fetchWithRetry } from '../utils/fetch.mjs'; +import { fetchWithAuth } from '../utils/fetch.mjs'; import { rewriteRelativeLinks } from './sanitize.mjs'; -const REPO = 'webpack/governance'; -const RAW_URL = `https://raw.githubusercontent.com/${REPO}/HEAD`; -// GitHub's editor needs a real branch name, HEAD only works for reading. -const EDIT_URL = `https://github.com/${REPO}/edit/main`; - -const { GH_TOKEN } = process.env; - -const BASE_HEADERS = { - ...(GH_TOKEN && { Authorization: `Bearer ${GH_TOKEN}` }), -}; - // Maps source filenames in webpack/governance repo to their output slug and sidebar label. // Insertion order determines sidebar order, this could be changed as per need. const FILE_MAP = { @@ -54,9 +43,8 @@ await mkdir(outputDir, { recursive: true }); const results = await Promise.all( Object.entries(FILE_MAP).map(async ([source, { output, label }]) => { - const res = await fetchWithRetry(`${RAW_URL}/${source}`, { - headers: BASE_HEADERS, - }); + const url = `https://raw.githubusercontent.com/webpack/governance/HEAD/${source}`; + const res = await fetchWithAuth(url); if (!res.ok) { console.error(`Failed: ${source} -> ${res.status} ${res.statusText}`); @@ -72,7 +60,7 @@ const results = await Promise.all( // site derives the page title from — fall back to the sidebar label. if (!/^# /m.test(body)) body = `# ${label}\n\n${body}`; - const content = `---\nsource: ${EDIT_URL}/${source}\n---\n\n${body}`; + const content = `---\nsource: https://github.com/webpack/governance/edit/main/${source}\n---\n\n${body}`; await writeFile(join(outputDir, `${output}.md`), content, 'utf8'); console.log(`Fetched: ${source} -> ${output}.md`); return { output, label }; diff --git a/scripts/markdown/readmes.mjs b/scripts/markdown/readmes.mjs index ec1221d3..fa6c6e38 100644 --- a/scripts/markdown/readmes.mjs +++ b/scripts/markdown/readmes.mjs @@ -1,15 +1,8 @@ import { mkdir, writeFile } from 'node:fs/promises'; import { join } from 'node:path'; -import { fetchWithRetry } from '../utils/fetch.mjs'; +import { fetchWithAuth, fetchWithRetry } from '../utils/fetch.mjs'; import cleanupMarkdown from './sanitize.mjs'; -const { GH_TOKEN } = process.env; - -const BASE_HEADERS = { - ...(GH_TOKEN && { Authorization: `Bearer ${GH_TOKEN}` }), - 'X-GitHub-Api-Version': '2022-11-28', -}; - const parseNextLink = linkHeader => linkHeader?.match(/<([^>]+)>;\s*rel="next"/)?.[1] ?? null; @@ -20,7 +13,7 @@ const discoverRepos = async () => { 'https://api.github.com/orgs/webpack/repos?per_page=100&type=public'; while (url) { - const res = await fetchWithRetry(url, { headers: BASE_HEADERS }); + const res = await fetchWithAuth(url); for (const repo of await res.json()) { if (repo.archived) continue; diff --git a/scripts/release/create-post.mjs b/scripts/release/create-post.mjs new file mode 100644 index 00000000..d7991633 --- /dev/null +++ b/scripts/release/create-post.mjs @@ -0,0 +1,93 @@ +import { readFile, writeFile } from 'node:fs/promises'; +import { parseArgs } from 'node:util'; + +import Handlebars from 'handlebars'; +import matter from 'gray-matter'; + +import { fetchWithAuth } from '../../utils/fetch.mjs'; + +const { values } = parseArgs({ + options: { + // Author + author: { type: 'string', default: 'avivkeller' }, + }, +}); + +const API_BASE = 'https://api.github.com/repos/webpack/webpack'; +const TEMPLATE_PATH = new URL('./template.md.hbs', import.meta.url); + +const fetchJSON = url => fetchWithAuth(url).then(r => r.json()); + +const getLatestVersion = async () => { + const { tag_name } = await fetchJSON(`${API_BASE}/releases/latest`); + return tag_name; +}; + +const getChangesets = async version => { + try { + const { files = [] } = await fetchJSON(`${API_BASE}/commits/${version}`); + + return files + .filter( + file => + file.status === 'removed' && + file.filename.startsWith('.changeset/') && + file.patch + ) + .map(({ patch }) => stripPatchMarkers(patch)); + } catch { + const changesets = await fetchJSON(`${API_BASE}/contents/.changeset`); + return Promise.all( + changesets.map(({ download_url }) => + fetch(download_url).then(d => d.text()) + ) + ); + } +}; + +const stripPatchMarkers = patch => patch.replace(/^@@.*\n|^[- ]/gm, ''); +const capitalize = value => value.charAt(0).toUpperCase() + value.slice(1); + +const parseChangeset = file => { + const { data, content } = matter(file); + const [title, ...paragraphs] = content.trim().split(/\n{2,}/); + + return { + notable: data.notable === true, + semver: capitalize(data.webpack), + title, + description: paragraphs.join('\n\n'), + }; +}; + +async function renderReleaseNotes(version, changes, date) { + Handlebars.registerHelper('filter', (items, property, expected) => + items.filter(item => item[property] === expected) + ); + + Handlebars.registerHelper('groupBy', (items, property) => + Object.groupBy(items, item => item[property]) + ); + + const source = await readFile(TEMPLATE_PATH, 'utf8'); + const render = Handlebars.compile(source, { noEscape: true }); + + return render({ + changes, + version, + date, + ...values, + }); +} + +const date = new Date().toISOString().slice(0, 10); +const version = await getLatestVersion(); +const changesets = await getChangesets(version); +const changes = changesets.map(parseChangeset); +const post = await renderReleaseNotes(version, changes, date); +writeFile( + new URL( + import.meta.resolve(`../../../pages/blog/${date}-webpack-${version}.md`) + ), + post +); diff --git a/scripts/release/template.md.hbs b/scripts/release/template.md.hbs new file mode 100644 index 00000000..cf77fea8 --- /dev/null +++ b/scripts/release/template.md.hbs @@ -0,0 +1,38 @@ +--- +date: '{{date}}' +category: release +title: 'webpack {{version}}' +layout: blog-post +author: '{{author}}' +--- + +# webpack {{version}} + + + +{{#with (filter changes "notable" true) as |notableChanges|}} +{{#if notableChanges.length}} +## Notable Changes + +{{#each notableChanges}} +### _({{semver}})_ {{title}} + +{{description}} + +{{/each}} +{{/if}} +{{/with}} +{{#with (filter changes "notable" false) as |otherChanges|}} +{{#if otherChanges.length}} +## Other Changes + +{{#each (groupBy otherChanges "semver")}} +### {{@key}} + +{{#each this}} +- {{title}} +{{/each}} + +{{/each}} +{{/if}} +{{/with}} \ No newline at end of file diff --git a/scripts/update-versions.mjs b/scripts/release/update-versions.mjs similarity index 100% rename from scripts/update-versions.mjs rename to scripts/release/update-versions.mjs diff --git a/scripts/utils/fetch.mjs b/scripts/utils/fetch.mjs index f18bd13c..cac9e373 100644 --- a/scripts/utils/fetch.mjs +++ b/scripts/utils/fetch.mjs @@ -1,10 +1,8 @@ -// fetch() wrapper that retries flakey responses like the GitHub and Open Collective -// occasionally 503 midbuild,so one blip doesn't fail the whole deploy. +import { setTimeout as sleep } from 'node:timers/promises'; +const { GH_TOKEN } = process.env; const RETRYABLE = new Set([429, 502, 503, 504]); -const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); - // Use the server's Retry After when it sends one, otherwise back off. const delayFor = (attempt, baseDelay, response) => { const retryAfter = Number(response?.headers.get('retry-after')); @@ -40,3 +38,21 @@ export const fetchWithRetry = async ( await sleep(delayFor(attempt, baseDelay, response)); } }; + +const githubHeaders = { + ...(GH_TOKEN && { Authorization: `Bearer ${GH_TOKEN}` }), + 'X-GitHub-Api-Version': '2022-11-28', +}; + +export const fetchWithAuth = (url, fetchOptions = {}, retryOptions) => + fetchWithRetry( + url, + { + ...fetchOptions, + headers: { + ...fetchOptions?.headers, + githubHeaders, + }, + }, + retryOptions + );