From 0902dfc7f7fb9218df8f095cc141bf128cc63320 Mon Sep 17 00:00:00 2001 From: Michael Pearce Date: Tue, 17 Nov 2026 22:26:27 -0800 Subject: [PATCH] Version builds by their commit and simplify pull request checks - Show the short commit hash the app was built from as its version, and drop semver from package.json and the changelog - Name the commit each release shipped in; the build finds it for new entries - Start the release list at Warp, the first release with a clean ship commit - Replace the changelog and version checks with title, description and build checks --- .github/pull_request_template.md | 11 +- .github/workflows/changelog.yml | 17 - .github/workflows/pull-request.yml | 30 ++ CONTRIBUTING.md | 56 ++++ README.md | 10 +- docs/documentation.md | 34 +- package-lock.json | 1 - package.json | 1 - scripts/check-changelog.mjs | 54 --- scripts/check-pull-request.mjs | 22 ++ scripts/pull-request-rules.d.mts | 7 + scripts/pull-request-rules.mjs | 67 ++++ scripts/release-commits.d.mts | 2 + scripts/release-commits.mjs | 37 +++ src/components/WhatsNew.tsx | 24 +- src/core/build.ts | 2 + src/docs/changelog.ts | 460 ++------------------------ src/docs/main.tsx | 35 +- src/docs/styles.css | 1 + tests/e2e/documentation.spec.ts | 12 +- tests/e2e/whats-new.spec.ts | 21 +- tests/unit/changelog.test.ts | 27 +- tests/unit/pull-request-rules.test.ts | 72 ++++ tests/unit/release-commits.test.ts | 27 ++ vite.config.ts | 20 ++ 25 files changed, 464 insertions(+), 586 deletions(-) delete mode 100644 .github/workflows/changelog.yml create mode 100644 .github/workflows/pull-request.yml create mode 100644 CONTRIBUTING.md delete mode 100644 scripts/check-changelog.mjs create mode 100644 scripts/check-pull-request.mjs create mode 100644 scripts/pull-request-rules.d.mts create mode 100644 scripts/pull-request-rules.mjs create mode 100644 scripts/release-commits.d.mts create mode 100644 scripts/release-commits.mjs create mode 100644 src/core/build.ts create mode 100644 tests/unit/pull-request-rules.test.ts create mode 100644 tests/unit/release-commits.test.ts diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 5f89812..c47a4ac 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -1,3 +1,9 @@ + + ## Summary - @@ -5,8 +11,3 @@ ## Testing - - -## Release notes - -- [ ] Added a release to `src/docs/changelog.ts` describing what changed for users -- [ ] Bumped the version with `npm version minor --no-git-tag-version` (features) or `patch` (fixes), matching the new release diff --git a/.github/workflows/changelog.yml b/.github/workflows/changelog.yml deleted file mode 100644 index 3a69757..0000000 --- a/.github/workflows/changelog.yml +++ /dev/null @@ -1,17 +0,0 @@ -name: Changelog - -on: - pull_request: - branches: [main] - -jobs: - changelog: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - uses: actions/setup-node@v4 - with: - node-version: 22 - - run: node scripts/check-changelog.mjs origin/${{ github.base_ref }} diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml new file mode 100644 index 0000000..5b16238 --- /dev/null +++ b/.github/workflows/pull-request.yml @@ -0,0 +1,30 @@ +name: Pull request + +on: + pull_request: + branches: [main] + types: [opened, edited, reopened, synchronize] + +jobs: + format: + name: Title and description + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 22 + - run: node scripts/check-pull-request.mjs + + build: + name: Type check and build + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 22 + cache: npm + - run: npm ci + - run: npm run build diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..27202c3 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,56 @@ +# Contributing + +Pull requests to `main` must pass the **Pull request** checks: the title, the +description, and a type check and build. + +## Title + +Start the title with a type in parentheses, then a space and a sentence-case summary +with no period: + +```text +(feat) Add the Warp tool for closing gaps in scanned parts +(fix) Keep Drive connected after a reload +``` + +| Type | Use for | +| ---------- | ----------------------------------- | +| `feat` | A new feature | +| `fix` | A bug fix | +| `perf` | A performance improvement | +| `docs` | Documentation only | +| `refactor` | Code changes with the same behavior | +| `test` | Tests only | +| `build` | Build, dependencies, or tooling | +| `ci` | Continuous integration | +| `chore` | Other maintenance | + +Add `!` after the type, as in `(feat!)`, for a breaking change. + +## Description + +Use the [pull request template](.github/pull_request_template.md). The description +starts with these sections, in this order: + +1. `## Summary`: bullets describing what changed and why. +2. `## Testing`: what you ran and what passed, plus anything not tested. + +You can add other sections, such as a review log, after the summary. + +## Version and release notes + +The app's version is the short hash of the commit it was built from, as GitHub shows +it. Pull requests do not change a version number. + +To tell people about a change, add an entry at the top of `src/docs/changelog.ts`. It +appears on the documentation's What's new page and in the workbench's What's new +button with the commit it shipped in, which the build fills in. Entries are optional. +See +[maintaining the documentation](docs/documentation.md#whats-new). + +## Check locally + +```sh +npm run build +PR_TITLE="(feat) Add pins" PR_BODY_FILE=body.md node scripts/check-pull-request.mjs +``` diff --git a/README.md b/README.md index ea11ab3..ce83416 100644 --- a/README.md +++ b/README.md @@ -951,12 +951,10 @@ See [design specification](docs/design.md) and [verification notes](docs/verific ## Contributing -Every pull request must add a release to the documentation's **What's new** page. -Bump the version with `npm version minor --no-git-tag-version` for features or -`patch` for fixes, then add a release with the same version at the top of -`src/docs/changelog.ts` describing what changed for users. The Changelog check on -pull requests fails until both are done. See -[maintaining the documentation](docs/documentation.md#whats-new). +Pull request titles start with a type in parentheses, such as `(feat)` or `(fix)`, +and descriptions follow the template's Summary and Testing sections. Checks on pull +requests enforce both and make sure the app type-checks and builds. The app's version +is the short hash of the commit it was built from. See [CONTRIBUTING.md](CONTRIBUTING.md). ## License diff --git a/docs/documentation.md b/docs/documentation.md index 189d06b..b41e71e 100644 --- a/docs/documentation.md +++ b/docs/documentation.md @@ -31,26 +31,24 @@ client-side navigation enhancement. ## What's new -`src/docs/changelog.ts` lists every version, newest first, and renders at -`?guide=whats-new`. The sidebar and the overview link to the newest release. The -workbench header's What's new button shows the three newest releases and marks -one the visitor hasn't opened yet. - -Every pull request adds one release at the top of the list: - -1. Run `npm version minor --no-git-tag-version` for a new feature, or `patch` for - a fix or small improvement. This updates `package.json` and `package-lock.json`. -2. Add a release with the same `version`, today's `date`, a short `title`, and one - entry per user-visible change. Mark each `new`, `improved`, or `fixed`, and set - `guide` to an article ID to link to it. +`src/docs/changelog.ts` lists releases, newest first, and renders at +`?guide=whats-new`. Each release shows the short hash of the commit it shipped in, so a +bug report can name the build. The sidebar shows the commit the documentation was +built from, and the overview links to the newest release. The workbench header's +What's new button shows the three newest releases and marks one the visitor hasn't +opened yet. + +Entries are optional; add one when a change is worth telling people about. Put it at +the top of the list with today's `date`, a short, unique `title` (it becomes the +release's link), and one entry per user-visible change. Mark each `new`, `improved`, +or `fixed`, and set `guide` to an article ID to link to it. Leave out `commit`: the +build reads git history (`scripts/release-commits.mjs`) and shows the commit that +added the entry, which is the merge on `main`. Set `commit` only when the change +shipped in an earlier commit than its entry. Write for people using Kitbasher, not for reviewers: what they can now do, or what -works differently. The Changelog workflow runs `scripts/check-changelog.mjs`, which -fails a pull request that leaves the changelog untouched, does not raise the -version above `main`, or lets `package.json`, `package-lock.json`, and the newest -release disagree. `tests/unit/changelog.test.ts` checks ordering, dates, and guide -links. If two open pull requests take the same version, the one merged second must -move to the next version. +works differently. `tests/unit/changelog.test.ts` checks ordering, dates, unique +titles, commits, and guide links. ## Images diff --git a/package-lock.json b/package-lock.json index bb5a071..d09aa3f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,7 +6,6 @@ "packages": { "": { "name": "kitbasher", - "version": "1.18.1", "license": "MIT", "dependencies": { "@fontsource-variable/inter": "^5.3.0", diff --git a/package.json b/package.json index febb6c6..7807574 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,5 @@ { "name": "kitbasher", - "version": "1.18.1", "description": "A local-first 3D assembly workbench for multipart models", "scripts": { "dev": "vite", diff --git a/scripts/check-changelog.mjs b/scripts/check-changelog.mjs deleted file mode 100644 index 1cd68f3..0000000 --- a/scripts/check-changelog.mjs +++ /dev/null @@ -1,54 +0,0 @@ -import { execFileSync } from 'node:child_process'; -import { readFileSync } from 'node:fs'; - -const changelogPath = 'src/docs/changelog.ts'; -const base = process.argv[2] ?? process.env.CHANGELOG_BASE ?? 'origin/main'; -const git = (...args) => - execFileSync('git', args, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }); -const failures = []; - -const parse = (version) => version.split('.').map(Number); -const newer = (a, b) => { - const [x, y] = [parse(a), parse(b)]; - for (let i = 0; i < 3; i++) if (x[i] !== y[i]) return x[i] > y[i]; - return false; -}; -const latestIn = (source) => source.match(/version:\s*'(\d+\.\d+\.\d+)'/)?.[1]; -const show = (path) => { - try { - return git('show', `${base}:${path}`); - } catch { - return undefined; - } -}; - -const mergeBase = git('merge-base', base, 'HEAD').trim(); -const changed = git('diff', '--name-only', mergeBase, 'HEAD').split('\n').filter(Boolean); -if (!changed.includes(changelogPath)) - failures.push(`Add a release for this change to ${changelogPath}.`); - -const version = JSON.parse(readFileSync('package.json', 'utf8')).version; -const lockVersion = JSON.parse(readFileSync('package-lock.json', 'utf8')).version; -const latest = latestIn(readFileSync(changelogPath, 'utf8')); -const baseVersion = JSON.parse(show('package.json') ?? '{"version":"0.0.0"}').version; -const baseLatest = latestIn(show(changelogPath) ?? '') ?? '0.0.0'; - -if (latest !== version) - failures.push( - `The newest release in ${changelogPath} is ${latest}, but package.json is ${version}.`, - ); -if (lockVersion !== version) - failures.push(`package-lock.json is ${lockVersion}, but package.json is ${version}.`); -if (!newer(version, baseVersion)) - failures.push(`Bump package.json above ${baseVersion}, the version on ${base}.`); -if (!newer(latest ?? '0.0.0', baseLatest)) - failures.push(`Add a release newer than ${baseLatest} at the top of ${changelogPath}.`); - -if (failures.length) { - console.error(`Changelog check failed against ${base}:\n- ${failures.join('\n- ')}`); - console.error( - '\nRun `npm version minor --no-git-tag-version` (or patch for fixes) and add a matching release to src/docs/changelog.ts.', - ); - process.exit(1); -} -console.log(`Changelog has ${version}; ${base} has ${baseVersion}.`); diff --git a/scripts/check-pull-request.mjs b/scripts/check-pull-request.mjs new file mode 100644 index 0000000..b3f4be3 --- /dev/null +++ b/scripts/check-pull-request.mjs @@ -0,0 +1,22 @@ +import { readFileSync } from 'node:fs'; +import { checkBody, checkTitle, pullRequestTypes } from './pull-request-rules.mjs'; + +const event = process.env.GITHUB_EVENT_PATH + ? JSON.parse(readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8')).pull_request + : undefined; +const title = process.env.PR_TITLE ?? event?.title ?? ''; +const body = process.env.PR_BODY_FILE + ? readFileSync(process.env.PR_BODY_FILE, 'utf8') + : (process.env.PR_BODY ?? event?.body ?? ''); + +const failures = [...checkTitle(title), ...checkBody(body)]; +if (failures.length) { + console.error(`Pull request format check failed:\n- ${failures.join('\n- ')}`); + console.error( + `\nTitle types:\n${Object.entries(pullRequestTypes) + .map(([type, { label }]) => ` ${type}: ${label}`) + .join('\n')}\nSee CONTRIBUTING.md and .github/pull_request_template.md.`, + ); + process.exit(1); +} +console.log(`Pull request format is valid: ${title}`); diff --git a/scripts/pull-request-rules.d.mts b/scripts/pull-request-rules.d.mts new file mode 100644 index 0000000..eb91c3f --- /dev/null +++ b/scripts/pull-request-rules.d.mts @@ -0,0 +1,7 @@ +export const pullRequestTypes: Record; +export const requiredSections: string[]; +export function parseTitle( + title: string, +): { type: string; breaking: boolean; subject: string } | undefined; +export function checkTitle(title: string): string[]; +export function checkBody(body: string | null | undefined): string[]; diff --git a/scripts/pull-request-rules.mjs b/scripts/pull-request-rules.mjs new file mode 100644 index 0000000..0348865 --- /dev/null +++ b/scripts/pull-request-rules.mjs @@ -0,0 +1,67 @@ +export const pullRequestTypes = { + feat: { label: 'New feature' }, + fix: { label: 'Bug fix' }, + perf: { label: 'Performance' }, + docs: { label: 'Documentation' }, + refactor: { label: 'Refactor with no change in behavior' }, + test: { label: 'Tests only' }, + build: { label: 'Build, dependencies, or tooling' }, + ci: { label: 'Continuous integration' }, + chore: { label: 'Maintenance' }, +}; + +export const requiredSections = ['Summary', 'Testing']; + +const titlePattern = new RegExp(`^\\((${Object.keys(pullRequestTypes).join('|')})(!)?\\) (.+)$`); + +export function parseTitle(title) { + const match = title.trim().match(titlePattern); + return match ? { type: match[1], breaking: !!match[2], subject: match[3] } : undefined; +} + +export function checkTitle(title) { + const parsed = parseTitle(title); + if (!parsed) + return [ + `Start the title with a type in parentheses, such as "(feat) Add the Warp tool". Types: ${Object.keys(pullRequestTypes).join(', ')}.`, + ]; + const failures = []; + if (!/^[A-Z]/.test(parsed.subject)) + failures.push('Start the title after the type with a capital letter.'); + if (/[.\s]$/.test(parsed.subject)) failures.push('End the title without a period or space.'); + if (title.trim().length > 80) failures.push('Keep the title to 80 characters or fewer.'); + return failures; +} + +const meaningful = (text) => + text + .split('\n') + .map((line) => line.replace(/^\s*(?:[-*]|\d+\.)\s*/, '').trim()) + .some(Boolean); + +export function checkBody(body) { + const text = (body ?? '').replace(//g, '').replace(/\r\n/g, '\n'); + const sections = [...text.matchAll(/^## +(.+?)\s*$/gm)].map((match, i, all) => ({ + title: match[1], + content: text.slice(match.index + match[0].length, all[i + 1]?.index ?? text.length), + })); + const found = sections.map((section) => section.title); + const failures = []; + const positions = requiredSections.map((title) => found.indexOf(title)); + for (const [i, title] of requiredSections.entries()) + if (positions[i] < 0) failures.push(`Add a "## ${title}" section.`); + if ( + positions.every((position) => position >= 0) && + positions.some((p, i) => i && p < positions[i - 1]) + ) + failures.push( + `Order the sections ${requiredSections.map((title) => `"${title}"`).join(', ')}.`, + ); + if (found[0] !== undefined && found[0] !== 'Summary') + failures.push('Start the description with the "## Summary" section.'); + const content = (title) => sections.find((section) => section.title === title)?.content ?? ''; + for (const title of ['Summary', 'Testing']) + if (found.includes(title) && !meaningful(content(title))) + failures.push(`Fill in the "${title}" section.`); + return failures; +} diff --git a/scripts/release-commits.d.mts b/scripts/release-commits.d.mts new file mode 100644 index 0000000..d7d6d68 --- /dev/null +++ b/scripts/release-commits.d.mts @@ -0,0 +1,2 @@ +export function firstCommits(log: string): Record; +export function releaseCommits(path?: string): Record; diff --git a/scripts/release-commits.mjs b/scripts/release-commits.mjs new file mode 100644 index 0000000..494ba16 --- /dev/null +++ b/scripts/release-commits.mjs @@ -0,0 +1,37 @@ +import { execFileSync } from 'node:child_process'; + +const git = (...args) => + execFileSync('git', args, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim(); + +/** From `git log --reverse --format=%x00%h -p` output, the first commit that added each release title. */ +export function firstCommits(log) { + const found = {}; + let commit; + for (const line of log.split('\n')) { + // A NUL marks each commit; no diff line can start with one. + if (line.startsWith('\0')) commit = line.slice(1); + else { + const title = line.match(/^\+ {4}title: '(.*)',$/)?.[1]; + if (title && commit && !(title in found)) found[title] = commit; + } + } + return found; +} + +/** The short hash of the commit that added each release title to the changelog. */ +export function releaseCommits(path = 'src/docs/changelog.ts') { + try { + // Builds that clone only the latest commit fetch the rest; the repository is public. + if (git('rev-parse', '--is-shallow-repository') === 'true') + git('fetch', '--unshallow', '--quiet'); + } catch { + // Without history, releases that name no commit show none. + } + try { + return firstCommits( + git('log', '--reverse', '--abbrev=7', '--format=%x00%h', '-p', '-U0', '--', path), + ); + } catch { + return {}; + } +} diff --git a/src/components/WhatsNew.tsx b/src/components/WhatsNew.tsx index 4923479..b03d445 100644 --- a/src/components/WhatsNew.tsx +++ b/src/components/WhatsNew.tsx @@ -1,27 +1,29 @@ import { useCallback, useEffect, useRef, useState } from 'react'; import { ExternalLink, Sparkles, X } from 'lucide-react'; -import { latestRelease, releases } from '../docs/changelog'; +import { buildCommit } from '../core/build'; +import { latestRelease, releaseCommit, releaseId, releases } from '../docs/changelog'; import type { ChangeKind } from '../docs/changelog'; import { useDismiss } from './ui'; const seenKey = 'kitbasher.seen-release'; const shownReleases = 3; +const latestId = releaseId(latestRelease); const kindLabels: Record = { new: 'New', improved: 'Improved', fixed: 'Fixed' }; function readSeen() { try { const seen = localStorage.getItem(seenKey); // First visits start current; only releases shipped after that are announced. - if (seen === null) localStorage.setItem(seenKey, latestRelease.version); - return seen ?? latestRelease.version; + if (seen === null) localStorage.setItem(seenKey, latestId); + return seen ?? latestId; } catch { - return latestRelease.version; + return latestId; } } function markSeen() { try { - localStorage.setItem(seenKey, latestRelease.version); + localStorage.setItem(seenKey, latestId); } catch { // Storage can be unavailable; the dot then just clears for this session. } @@ -36,7 +38,7 @@ const formatDate = (date: string) => export function WhatsNew() { const [open, setOpen] = useState(false); - const [unseen, setUnseen] = useState(() => readSeen() !== latestRelease.version); + const [unseen, setUnseen] = useState(() => readSeen() !== latestId); const container = useRef(null); const trigger = useRef(null); const heading = useRef(null); @@ -56,7 +58,7 @@ export function WhatsNew() {