From 403592b6ca3403e0eac83338276dc67c986edd07 Mon Sep 17 00:00:00 2001 From: 7w1 Date: Mon, 18 May 2026 17:52:00 -0500 Subject: [PATCH 1/3] update workflow to credit all committers --- .github/scripts/enrich-changelog.mjs | 226 ++++++++++++++++++++++++++ .github/workflows/prepare-release.yml | 75 +-------- 2 files changed, 229 insertions(+), 72 deletions(-) create mode 100644 .github/scripts/enrich-changelog.mjs diff --git a/.github/scripts/enrich-changelog.mjs b/.github/scripts/enrich-changelog.mjs new file mode 100644 index 000000000..29c83fb53 --- /dev/null +++ b/.github/scripts/enrich-changelog.mjs @@ -0,0 +1,226 @@ +#!/usr/bin/env node +/** + * Replaces Knope commit markers with PR links and credits every human commit author. + * + * Usage: + * GITHUB_TOKEN=... GITHUB_REPOSITORY=owner/repo node .github/scripts/enrich-changelog.mjs [options] + * + * Options: + * --file Changelog to enrich (default: CHANGELOG.md) + * --dry-run Print enriched output without writing files or updating PRs + * --update-release-pr Also enrich the open release PR body and apply the internal label + */ + +import fs from 'node:fs'; + +const BOT_USER_TYPE = 'Bot'; +const COMMIT_MARKER = //g; + +function parseArgs(argv) { + const opts = { + file: 'CHANGELOG.md', + dryRun: false, + updateReleasePr: false, + }; + for (let i = 0; i < argv.length; i++) { + const arg = argv[i]; + if (arg === '--dry-run') opts.dryRun = true; + else if (arg === '--update-release-pr') opts.updateReleasePr = true; + else if (arg === '--file') opts.file = argv[++i]; + else if (arg === '--help' || arg === '-h') { + console.log(`See the header comment in ${import.meta.url}`); + process.exit(0); + } else { + console.error(`Unknown argument: ${arg}`); + process.exit(1); + } + } + return opts; +} + +function isBot(user) { + if (!user?.login) return true; + if (user.type === BOT_USER_TYPE) return true; + return user.login.endsWith('[bot]'); +} + +function parseRepository() { + const repoEnv = process.env.GITHUB_REPOSITORY; + if (!repoEnv?.includes('/')) { + throw new Error('Set GITHUB_REPOSITORY to owner/repo (e.g. SableClient/Sable).'); + } + const [owner, repo] = repoEnv.split('/'); + return { owner, repo }; +} + +async function githubRequest(token, path, { params, method = 'GET', body, accept } = {}) { + const url = new URL(`https://api.github.com${path}`); + for (const [key, value] of Object.entries(params ?? {})) { + if (value !== undefined) url.searchParams.set(key, String(value)); + } + const response = await fetch(url, { + method, + headers: { + Authorization: `Bearer ${token}`, + Accept: accept ?? 'application/vnd.github+json', + 'X-GitHub-Api-Version': '2022-11-28', + 'User-Agent': 'sable-changelog-enrich', + ...(body ? { 'Content-Type': 'application/json' } : {}), + }, + body: body ? JSON.stringify(body) : undefined, + }); + if (!response.ok) { + const text = await response.text(); + throw new Error(`GitHub API ${response.status} ${path}: ${text}`); + } + if (response.status === 204) return null; + return response.json(); +} + +async function listPullRequestCommits(token, owner, repo, pullNumber) { + const commits = []; + let page = 1; + while (true) { + const batch = await githubRequest(token, `/repos/${owner}/${repo}/pulls/${pullNumber}/commits`, { + params: { per_page: 100, page }, + }); + commits.push(...batch); + if (batch.length < 100) break; + page += 1; + } + return commits; +} + +async function getPullRequestContributors(token, owner, repo, pullNumber, cache) { + if (cache.has(pullNumber)) return cache.get(pullNumber); + + const commits = await listPullRequestCommits(token, owner, repo, pullNumber); + const logins = new Set(); + for (const commit of commits) { + const author = commit.author; + if (author?.login && !isBot(author)) { + logins.add(author.login); + } + } + + const contributors = [...logins].sort((a, b) => a.localeCompare(b, 'en')); + cache.set(pullNumber, contributors); + return contributors; +} + +function formatPullRequestCredit(pr, contributors) { + const link = `[#${pr.number}](<${pr.html_url}>)`; + if (contributors.length === 0) return `(${link})`; + const byline = contributors.map((login) => `@${login}`).join(', '); + return `(${link} by ${byline})`; +} + +async function resolveCommitMarker(token, owner, repo, hash, contributorCache) { + try { + const associated = await githubRequest( + token, + `/repos/${owner}/${repo}/commits/${hash}/pulls`, + { accept: 'application/vnd.github.groot-preview+json' }, + ); + const pr = associated[0]; + if (!pr) return `(\`${hash}\`)`; + + const contributors = await getPullRequestContributors( + token, + owner, + repo, + pr.number, + contributorCache, + ); + return formatPullRequestCredit(pr, contributors); + } catch { + return `(\`${hash}\`)`; + } +} + +async function enrichText(token, owner, repo, text, contributorCache) { + const hashes = [...new Set([...text.matchAll(COMMIT_MARKER)].map((match) => match[1]))]; + let enriched = text; + for (const hash of hashes) { + const credit = await resolveCommitMarker(token, owner, repo, hash, contributorCache); + enriched = enriched.replaceAll(``, credit); + } + return enriched; +} + +async function ensureLabel(token, owner, repo, name, color) { + try { + await githubRequest(token, `/repos/${owner}/${repo}/labels/${encodeURIComponent(name)}`); + } catch (error) { + if (!String(error.message).includes('404')) throw error; + await githubRequest(token, `/repos/${owner}/${repo}/labels`, { + method: 'POST', + body: { name, color }, + }); + } +} + +async function updateReleasePullRequest(token, owner, repo, contributorCache, dryRun) { + const openPulls = await githubRequest(token, `/repos/${owner}/${repo}/pulls`, { + params: { head: `${owner}:release`, state: 'open' }, + }); + if (openPulls.length === 0) { + console.warn('No open PR found for the release branch.'); + return; + } + + const releasePr = openPulls[0]; + const enrichedBody = await enrichText(token, owner, repo, releasePr.body ?? '', contributorCache); + + if (dryRun) { + console.log('--- release PR body (dry run) ---\n'); + console.log(enrichedBody); + return; + } + + const label = 'internal'; + await ensureLabel(token, owner, repo, label, 'e4e669'); + await Promise.all([ + githubRequest(token, `/repos/${owner}/${repo}/pulls/${releasePr.number}`, { + method: 'PATCH', + body: { body: enrichedBody }, + }), + githubRequest(token, `/repos/${owner}/${repo}/issues/${releasePr.number}/labels`, { + method: 'POST', + body: { labels: [label] }, + }), + ]); +} + +async function main() { + const opts = parseArgs(process.argv.slice(2)); + const token = process.env.GITHUB_TOKEN; + if (!token) { + throw new Error('Set GITHUB_TOKEN to a token with repo read access (and write for --update-release-pr).'); + } + + const { owner, repo } = parseRepository(); + const contributorCache = new Map(); + + if (!fs.existsSync(opts.file)) { + console.warn(`Changelog file not found: ${opts.file}`); + } else { + const original = fs.readFileSync(opts.file, 'utf8'); + const enriched = await enrichText(token, owner, repo, original, contributorCache); + + if (opts.dryRun) { + console.log(enriched); + } else { + fs.writeFileSync(opts.file, enriched); + } + } + + if (opts.updateReleasePr) { + await updateReleasePullRequest(token, owner, repo, contributorCache, opts.dryRun); + } +} + +main().catch((error) => { + console.error(error); + process.exit(1); +}); diff --git a/.github/workflows/prepare-release.yml b/.github/workflows/prepare-release.yml index ad10769fd..ce2658c38 100644 --- a/.github/workflows/prepare-release.yml +++ b/.github/workflows/prepare-release.yml @@ -39,78 +39,9 @@ jobs: GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }} - name: Enrich changelog and update release PR - uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 - with: - github-token: ${{ steps.generate-token.outputs.token }} - script: | - const fs = require("fs"); - const { owner, repo } = context.repo; - - async function resolveHash(hash) { - try { - const prs = await github.rest.repos.listPullRequestsAssociatedWithCommit({ - owner, repo, commit_sha: hash, - }); - const pr = prs.data[0]; - return pr - ? `([#${pr.number}](<${pr.html_url}>) by @${pr.user.login})` - : `(\`${hash}\`)`; - } catch { - return `(\`${hash}\`)`; - } - } - - async function enrichText(text) { - const marker = //g; - const hashes = [...new Set([...text.matchAll(marker)].map(m => m[1]))]; - for (const hash of hashes) { - text = text.replaceAll(``, await resolveHash(hash)); - } - return text; - } - - const changelog = "CHANGELOG.md"; - if (fs.existsSync(changelog)) { - const enriched = await enrichText(fs.readFileSync(changelog, "utf8")); - fs.writeFileSync(changelog, enriched); - } - - const prs = await github.rest.pulls.list({ - owner, repo, - head: `${owner}:release`, - state: "open", - }); - - if (prs.data.length === 0) { - core.warning("No open PR found for the release branch."); - return; - } - - const pr = prs.data[0]; - - const label = "internal"; - try { - await github.rest.issues.getLabel({ owner, repo, name: label }); - } catch (e) { - if (e.status === 404) { - await github.rest.issues.createLabel({ owner, repo, name: label, color: "e4e669" }); - } else { - throw e; - } - } - - await Promise.all([ - github.rest.pulls.update({ - owner, repo, - pull_number: pr.number, - body: await enrichText(pr.body ?? ""), - }), - github.rest.issues.addLabels({ - owner, repo, - issue_number: pr.number, - labels: [label], - }), - ]); + env: + GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }} + run: node .github/scripts/enrich-changelog.mjs --update-release-pr - name: Amend release commit with enriched changelog shell: bash From c220332e3b1bdb1bcea5d3e9c61803cb67567b21 Mon Sep 17 00:00:00 2001 From: 7w1 Date: Mon, 18 May 2026 17:57:03 -0500 Subject: [PATCH 2/3] formatting and lint --- .github/scripts/enrich-changelog.mjs | 85 +++++++++++++++++----------- 1 file changed, 51 insertions(+), 34 deletions(-) diff --git a/.github/scripts/enrich-changelog.mjs b/.github/scripts/enrich-changelog.mjs index 29c83fb53..bb2e4241d 100644 --- a/.github/scripts/enrich-changelog.mjs +++ b/.github/scripts/enrich-changelog.mjs @@ -1,4 +1,5 @@ #!/usr/bin/env node +/* oxlint-disable no-console */ /** * Replaces Knope commit markers with PR links and credits every human commit author. * @@ -53,22 +54,33 @@ function parseRepository() { return { owner, repo }; } +function setSearchParams(url, params) { + for (const [key, value] of Object.entries(params ?? {})) { + if (value === undefined) continue; + if (typeof value === 'string' || typeof value === 'number') { + url.searchParams.set(key, String(value)); + } + } +} + async function githubRequest(token, path, { params, method = 'GET', body, accept } = {}) { const url = new URL(`https://api.github.com${path}`); - for (const [key, value] of Object.entries(params ?? {})) { - if (value !== undefined) url.searchParams.set(key, String(value)); + setSearchParams(url, params); + + const headers = { + Authorization: `Bearer ${token}`, + Accept: accept ?? 'application/vnd.github+json', + 'X-GitHub-Api-Version': '2022-11-28', + 'User-Agent': 'sable-changelog-enrich', + }; + + const init = { method, headers }; + if (body != null && method !== 'GET') { + headers['Content-Type'] = 'application/json'; + init.body = JSON.stringify(body); } - const response = await fetch(url, { - method, - headers: { - Authorization: `Bearer ${token}`, - Accept: accept ?? 'application/vnd.github+json', - 'X-GitHub-Api-Version': '2022-11-28', - 'User-Agent': 'sable-changelog-enrich', - ...(body ? { 'Content-Type': 'application/json' } : {}), - }, - body: body ? JSON.stringify(body) : undefined, - }); + + const response = await fetch(url, init); if (!response.ok) { const text = await response.text(); throw new Error(`GitHub API ${response.status} ${path}: ${text}`); @@ -77,18 +89,17 @@ async function githubRequest(token, path, { params, method = 'GET', body, accept return response.json(); } +async function fetchPullRequestCommitPage(token, owner, repo, pullNumber, page, commits) { + const batch = await githubRequest(token, `/repos/${owner}/${repo}/pulls/${pullNumber}/commits`, { + params: { per_page: 100, page }, + }); + commits.push(...batch); + if (batch.length < 100) return commits; + return fetchPullRequestCommitPage(token, owner, repo, pullNumber, page + 1, commits); +} + async function listPullRequestCommits(token, owner, repo, pullNumber) { - const commits = []; - let page = 1; - while (true) { - const batch = await githubRequest(token, `/repos/${owner}/${repo}/pulls/${pullNumber}/commits`, { - params: { per_page: 100, page }, - }); - commits.push(...batch); - if (batch.length < 100) break; - page += 1; - } - return commits; + return fetchPullRequestCommitPage(token, owner, repo, pullNumber, 1, []); } async function getPullRequestContributors(token, owner, repo, pullNumber, cache) { @@ -103,7 +114,7 @@ async function getPullRequestContributors(token, owner, repo, pullNumber, cache) } } - const contributors = [...logins].sort((a, b) => a.localeCompare(b, 'en')); + const contributors = [...logins].toSorted((a, b) => a.localeCompare(b, 'en')); cache.set(pullNumber, contributors); return contributors; } @@ -117,11 +128,9 @@ function formatPullRequestCredit(pr, contributors) { async function resolveCommitMarker(token, owner, repo, hash, contributorCache) { try { - const associated = await githubRequest( - token, - `/repos/${owner}/${repo}/commits/${hash}/pulls`, - { accept: 'application/vnd.github.groot-preview+json' }, - ); + const associated = await githubRequest(token, `/repos/${owner}/${repo}/commits/${hash}/pulls`, { + accept: 'application/vnd.github.groot-preview+json', + }); const pr = associated[0]; if (!pr) return `(\`${hash}\`)`; @@ -130,7 +139,7 @@ async function resolveCommitMarker(token, owner, repo, hash, contributorCache) { owner, repo, pr.number, - contributorCache, + contributorCache ); return formatPullRequestCredit(pr, contributors); } catch { @@ -140,9 +149,15 @@ async function resolveCommitMarker(token, owner, repo, hash, contributorCache) { async function enrichText(token, owner, repo, text, contributorCache) { const hashes = [...new Set([...text.matchAll(COMMIT_MARKER)].map((match) => match[1]))]; + const credits = await Promise.all( + hashes.map(async (hash) => ({ + hash, + credit: await resolveCommitMarker(token, owner, repo, hash, contributorCache), + })) + ); + let enriched = text; - for (const hash of hashes) { - const credit = await resolveCommitMarker(token, owner, repo, hash, contributorCache); + for (const { hash, credit } of credits) { enriched = enriched.replaceAll(``, credit); } return enriched; @@ -196,7 +211,9 @@ async function main() { const opts = parseArgs(process.argv.slice(2)); const token = process.env.GITHUB_TOKEN; if (!token) { - throw new Error('Set GITHUB_TOKEN to a token with repo read access (and write for --update-release-pr).'); + throw new Error( + 'Set GITHUB_TOKEN to a token with repo read access (and write for --update-release-pr).' + ); } const { owner, repo } = parseRepository(); From 4d91fe6053b08e64dcc4de9dd760c11e4d8af2e6 Mon Sep 17 00:00:00 2001 From: 7w1 Date: Mon, 18 May 2026 18:00:26 -0500 Subject: [PATCH 3/3] update changeset for image viewer --- .changeset/feat_image_viewer_changes.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/.changeset/feat_image_viewer_changes.md b/.changeset/feat_image_viewer_changes.md index 70a0eb579..a026706b3 100644 --- a/.changeset/feat_image_viewer_changes.md +++ b/.changeset/feat_image_viewer_changes.md @@ -4,8 +4,6 @@ default: minor Change Image Viewer to feel more natural to use. -# Changes to Image viewer - - Fixed zoom gestures generally not working on mobile. - Changed the % number in the top right to reflect the zoom of the original image as opposed to the change from it fitting the container. - Made the zoom pill allow entering custom values.