mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-18 14:14:37 +01:00
Release notes list every change with its pull request instead of "see CHANGELOG.md" (#4390)
GitHub release pages now list every change a release ships, one plain line each, linked to the pull request that made it, instead of one headline per version or "see CHANGELOG.md". Why: the release page is where people read what changed, and recent releases went out saying only "see CHANGELOG.md". Nothing to do: the next release uses it; a release with no CHANGELOG entry stops with `NOTES_MISSING` instead of publishing a stub. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
b1c32824fd
commit
a9da30363c
12 files changed
+551
-118
No files matched your search
+12
-78
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env -S bun --no-env-file --config=/dev/null
|
||||
/**
|
||||
* release-cut: tag the newest green commit on main as v<VERSION>, publish a
|
||||
* GitHub Release whose notes are that span's CHANGELOG headlines, and
|
||||
* GitHub Release whose notes list that span's CHANGELOG changes (release-notes.ts), and
|
||||
* fast-forward refs/heads/stable to it (docs/releases.md). Run by
|
||||
* .github/workflows/weekly-release.yml ("Stable release") every day at 14:00
|
||||
* UTC with --daily (the daily check, crew/scripts/release-cadence.ts: an important
|
||||
@@ -69,10 +69,12 @@ import * as os from 'node:os';
|
||||
import * as path from 'node:path';
|
||||
import { spawnSync } from 'node:child_process';
|
||||
import { FLOOR_HOURS, IMPORTANT_FLOOR_HOURS, SCHEDULED_GRACE_HOURS, decideDaily, importantChanges, type DailyDecision } from './release-cadence';
|
||||
import { KIND_LABEL, NOTES_CAP, changeSources, changelogEntries, compareVersions, releaseKind, releaseNotes } from './release-notes';
|
||||
import { bucketClass } from '../../lib/ci-gate';
|
||||
import { headCheckRows, type AsideRuns } from '../../lib/ci-head-checks';
|
||||
|
||||
export { FLOOR_HOURS, SCHEDULED_GRACE_HOURS };
|
||||
export { NOTES_CAP, changelogEntries, compareVersions, releaseKind, releaseNotes };
|
||||
|
||||
export type Gh = (args: string[]) => { status: number | null; stdout: string; stderr: string };
|
||||
export type Git = (args: string[]) => { ok: boolean; out: string };
|
||||
@@ -87,7 +89,6 @@ const NOTES_LOCAL = 'refs/notes/gstack-trains-release';
|
||||
const RED = new Set(['failure', 'timed_out', 'cancelled', 'action_required', 'startup_failure', 'stale']);
|
||||
export const WINDOW_DAYS = 7;
|
||||
export const WINDOW_COMMITS = 200;
|
||||
export const NOTES_CAP = 60_000;
|
||||
const HOUR = 3_600_000;
|
||||
|
||||
export const runGh: Gh = (args) => {
|
||||
@@ -104,16 +105,6 @@ function parseJson<T>(r: { status: number | null; stdout: string }): T | null {
|
||||
try { return JSON.parse(r.stdout) as T; } catch { return null; }
|
||||
}
|
||||
|
||||
/** Four-part versions compare numerically, part by part. */
|
||||
export function compareVersions(a: string, b: string): number {
|
||||
const pa = a.split('.').map(Number), pb = b.split('.').map(Number);
|
||||
for (let i = 0; i < Math.max(pa.length, pb.length); i++) {
|
||||
const d = (pa[i] ?? 0) - (pb[i] ?? 0);
|
||||
if (d !== 0) return d;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Green names what gated the commit: the backstop check's name, or `train <nonce>`. Pending means a check run on it is still going. */
|
||||
export type Verdict = { green: true; via: string } | { green: false; why: string; pending: boolean };
|
||||
interface CheckRun { id?: number; name: string; status: string; conclusion: string | null }
|
||||
@@ -231,68 +222,9 @@ export function readTrainNotes(git: Git, notesRef = NOTES_REF): TrainNotes {
|
||||
return notes;
|
||||
}
|
||||
|
||||
export interface ChangelogEntry { version: string; date: string; headline: string }
|
||||
|
||||
/** Every "## [X.Y.Z.W] - date" entry of a CHANGELOG with its first prose line, markdown stripped. */
|
||||
export function changelogEntries(text: string): ChangelogEntry[] {
|
||||
const out: ChangelogEntry[] = [];
|
||||
const lines = text.split('\n');
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const m = /^## \[(\d+(?:\.\d+)+)\](?: - (\S+))?/.exec(lines[i]!);
|
||||
if (!m) continue;
|
||||
let headline = '';
|
||||
for (let j = i + 1; j < lines.length && !lines[j]!.startsWith('## '); j++) {
|
||||
const l = lines[j]!.trim();
|
||||
if (!l || l.startsWith('#') || l.startsWith('|') || l.startsWith('-') || l.startsWith('```')) continue;
|
||||
const bold = /^\*\*(.+?)\*\*/.exec(l);
|
||||
headline = (bold ? bold[1]! : l.replace(/\*\*/g, '')).replace(/\s+/g, ' ');
|
||||
break;
|
||||
}
|
||||
out.push({ version: m[1]!, date: m[2] ?? '', headline });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export interface NotesInput { repo: string; version: string; date: string; prevVersion: string | null; prevTag: string | null; entries: ChangelogEntry[]; hotfix: boolean; first?: boolean; exception?: Exception | null }
|
||||
|
||||
/** A verified floor exception (a security or a regression cut): the sanitized reason and what tied it to the released commits. */
|
||||
export interface Exception { kind: 'security' | 'regression'; reason: string; evidence: string }
|
||||
|
||||
const KIND_LABEL = { security: 'Security', regression: 'Regression' } as const;
|
||||
|
||||
export function releaseKind(hotfix: boolean, exception: Exception | null | undefined): 'security' | 'regression' | 'hotfix' | 'regular' {
|
||||
return exception ? exception.kind : hotfix ? 'hotfix' : 'regular';
|
||||
}
|
||||
|
||||
/** Release notes: one headline per version in (prev, version], capped at NOTES_CAP. */
|
||||
export function releaseNotes(n: NotesInput): string {
|
||||
const span = n.entries.filter(e => compareVersions(e.version, n.version) <= 0 && (!n.prevVersion || compareVersions(e.version, n.prevVersion) > 0))
|
||||
.sort((a, b) => compareVersions(b.version, a.version));
|
||||
const plural = span.length === 1 ? '' : 's';
|
||||
const changelogUrl = 'https://github.com/' + n.repo + '/blob/v' + n.version + '/CHANGELOG.md';
|
||||
const compare = n.prevTag ? '[Compare](https://github.com/' + n.repo + '/compare/' + n.prevTag + '...v' + n.version + ') or read ' : 'Read ';
|
||||
const head = [
|
||||
'## gstack v' + n.version + ' (' + releaseKind(n.hotfix, n.exception) + ' release)',
|
||||
'',
|
||||
...(n.exception ? ['**' + KIND_LABEL[n.exception.kind] + ' release:** `' + n.exception.reason + '` (verified: ' + n.exception.evidence + '). Every version since the previous release ships with it.', ''] : []),
|
||||
n.first || !n.prevVersion
|
||||
? 'First tagged release: the ' + span.length + ' version' + plural + (n.prevVersion ? ' since v' + n.prevVersion + ' (the week before it)' : '') + ', one line each; the [full changelog](' + changelogUrl + ') has every earlier one.'
|
||||
: span.length + ' version' + plural + ' since v' + n.prevVersion + ', one line each. ' + compare + 'the [full changelog](' + changelogUrl + ').',
|
||||
'',
|
||||
'New installs and team-mode installs follow the stable branch, which now points here; gstack-config set update_channel main follows every merge instead ([docs/releases.md](https://github.com/' + n.repo + '/blob/v' + n.version + '/docs/releases.md)).',
|
||||
'',
|
||||
].join('\n');
|
||||
const bullets = span.map(e => '- **' + e.version + '**' + (e.date ? ' (' + e.date + ')' : '') + ': ' + (e.headline || 'see CHANGELOG.md'));
|
||||
let body = head + bullets.join('\n') + '\n';
|
||||
if (body.length > NOTES_CAP) {
|
||||
const kept: string[] = [];
|
||||
let size = head.length;
|
||||
for (const b of bullets) { if (size + b.length + 1 > NOTES_CAP - 200) break; kept.push(b); size += b.length + 1; }
|
||||
body = head + kept.join('\n') + '\n- ...and ' + (bullets.length - kept.length) + ' more versions; see CHANGELOG.md\n';
|
||||
}
|
||||
return body;
|
||||
}
|
||||
|
||||
const iso = (ms: number) => new Date(ms).toISOString().replace(/\.\d{3}Z$/, 'Z');
|
||||
|
||||
/**
|
||||
@@ -448,8 +380,8 @@ export interface Plan {
|
||||
tagExists: 'no' | 'same' | 'other'; stableAt: boolean; notes: string; skipped: string[];
|
||||
exception: Exception | null; floor: Floor; ridingAlong: number;
|
||||
}
|
||||
export type RefusalCode = 'NOTHING_NEW' | 'NO_GREEN_COMMIT' | 'NO_RELEASE_PR' | 'REFUSED' | 'REFUSED_FLOOR' | 'FLOOR_UNKNOWN' | 'REFUSED_SECURITY' | 'REFUSED_REGRESSION';
|
||||
/** `sha` names the candidate on NO_RELEASE_PR, which comes after the candidate was picked but before a plan exists. */
|
||||
export type RefusalCode = 'NOTHING_NEW' | 'NO_GREEN_COMMIT' | 'NO_RELEASE_PR' | 'NOTES_MISSING' | 'REFUSED' | 'REFUSED_FLOOR' | 'FLOOR_UNKNOWN' | 'REFUSED_SECURITY' | 'REFUSED_REGRESSION';
|
||||
/** `sha` names the candidate on NO_RELEASE_PR and NOTES_MISSING, which come after the candidate was picked but before a plan exists. */
|
||||
export type PlanResult = { ok: true; plan: Plan } | { ok: false; code: RefusalCode; message: string; skipped?: string[]; next?: string; plan?: Plan; sha?: string };
|
||||
|
||||
export interface PlanOptions {
|
||||
@@ -567,9 +499,11 @@ export function planRelease(o: PlanOptions): PlanResult {
|
||||
}
|
||||
const changelog = git(['show', sha + ':CHANGELOG.md']);
|
||||
const date = git(['show', '-s', '--format=%cs', sha]).out;
|
||||
const notes = releaseNotes({ repo, version, date, prevVersion, prevTag, entries: changelog.ok ? changelogEntries(changelog.out) : [], hotfix: Boolean(o.ref), first: !before, exception });
|
||||
const written = releaseNotes({ repo, version, date, prevVersion, prevTag, entries: changelog.ok ? changelogEntries(changelog.out) : [], hotfix: Boolean(o.ref), first: !before, exception, sources: changeSources(git, before, sha) });
|
||||
if (!written.ok) return { ok: false, code: 'NOTES_MISSING', sha, message: written.why };
|
||||
const notes = written.body;
|
||||
const floor = floorStatus({ git, gh, repo, prevStable, sha, exception: exception?.kind ?? null, scheduled: Boolean(o.scheduled), now: o.now?.() ?? Date.now(), floorHours: o.floorHours });
|
||||
const ridingAlong = Math.max(0, notes.split('\n').filter(l => l.startsWith('- **')).length - 1);
|
||||
const ridingAlong = written.versions - 1;
|
||||
const plan: Plan = { sha, version, tag, date, hotfix: Boolean(o.ref), via, prevStable, prevVersion, prevTag, tagExists, stableAt: prevStable === sha, notes, skipped, exception, floor, ridingAlong };
|
||||
if (floor.verdict === 'refused' || floor.verdict === 'unknown') return { ok: false, code: floor.verdict === 'refused' ? 'REFUSED_FLOOR' : 'FLOOR_UNKNOWN', message: floor.message!, next: floor.next, plan };
|
||||
return { ok: true, plan };
|
||||
@@ -621,7 +555,7 @@ export function describePlan(p: Plan, dryRun: boolean): string[] {
|
||||
'CANDIDATE: ' + p.sha + ' (' + p.date + ')' + (p.hotfix ? ' [hotfix: --ref]' : '') + ' gated by ' + p.via,
|
||||
'VERSION: ' + p.version + ' -> tag ' + p.tag + (p.tagExists === 'same' ? ' (exists at this commit)' : ''),
|
||||
'STABLE: ' + (p.stableAt ? 'already at ' + p.sha.slice(0, 12) + ' (finishing an earlier cut)' : (p.prevStable ? p.prevStable.slice(0, 12) + ' (v' + (p.prevVersion ?? '?') + ')' : 'none yet') + ' -> ' + p.sha.slice(0, 12)),
|
||||
'NOTES: ' + p.notes.split('\n').filter(l => l.startsWith('- ')).length + ' version lines, ' + p.notes.length + ' chars',
|
||||
'NOTES: ' + p.notes.split('\n').filter(l => l.startsWith('- ')).length + ' change lines, ' + p.notes.length + ' chars',
|
||||
p.floor.line,
|
||||
];
|
||||
if (p.exception) lines.push(p.exception.kind.toUpperCase() + ': `' + p.exception.reason + '` (verified: ' + p.exception.evidence + ')', 'RIDING_ALONG: ' + p.ridingAlong + ' other version' + (p.ridingAlong === 1 ? '' : 's') + ' ship with the fix');
|
||||
@@ -638,7 +572,7 @@ export function describePlan(p: Plan, dryRun: boolean): string[] {
|
||||
/**
|
||||
* The daily check (--daily, crew/scripts/release-cadence.ts): plan at the 24-hour important floor, then let
|
||||
* decideDaily pick the trigger, if any; NO_RELEASE_PR becomes a blocked decision only when a trigger
|
||||
* is due. decision is null when the plan's refusal stands on its own (a freeze, nothing green,
|
||||
* is due, and NOTES_MISSING stops only a cut decision (no version to cut today needs no notes). decision is null when the plan's refusal stands on its own (a freeze, nothing green,
|
||||
* FLOOR_UNKNOWN, a non-fast-forward, a version stamped on an unrelated commit); lines then say which
|
||||
* important changes are still waiting on the gate. A plan that cleared while the daily check itself
|
||||
* cannot date stable is held as FLOOR_UNKNOWN, so a null decision never reaches a cut.
|
||||
@@ -652,7 +586,7 @@ export function planDaily(o: PlanOptions & { fragmentsDir?: string }): { planned
|
||||
const noReleasePr = !planned.ok && planned.code === 'NO_RELEASE_PR';
|
||||
const sha = planned.ok ? planned.plan.sha
|
||||
: planned.code === 'REFUSED_FLOOR' ? planned.plan?.sha
|
||||
: noReleasePr ? planned.sha
|
||||
: noReleasePr || (!planned.ok && planned.code === 'NOTES_MISSING') ? planned.sha
|
||||
: planned.code === 'NOTHING_NEW' ? prev ?? undefined
|
||||
: undefined;
|
||||
const moved = prev ? stableMovedAt(git, gh, repo, prev) : null;
|
||||
|
||||
@@ -0,0 +1,224 @@
|
||||
/**
|
||||
* Release notes for a cut (crew/scripts/release-cut.ts, docs/releases.md#release-notes): every change in the
|
||||
* CHANGELOG versions a release ships, one line each, grouped under What's new, Fixes and Changes for
|
||||
* maintainers, with a link to the pull request that made it.
|
||||
*
|
||||
* A change is a top-level bullet of a version's section, or a paragraph when it is not a bullet. Its line is
|
||||
* the bullet's bold lead when that lead is a sentence, else its first sentence, with internal issue codes in
|
||||
* parentheses dropped. Its pull request is read from git (changeSources), never from its words: the commit that
|
||||
* added a version's heading folded the changelog.d fragments it deleted, in path order, so a folded version lists
|
||||
* each of those fragments' changes, as the fragment read just before the fold, with the trailing "(#N)" of the
|
||||
* squash commit that added that file there (wherever it sits, since a release can fold fragments that landed
|
||||
* before the previous one, and never a later commit that reused the file name). Changes the version holds past
|
||||
* its fragments' link the heading commit's pull request; a release commit (a "v<version> release" subject)
|
||||
* links nothing.
|
||||
* Its group comes from the heading it sits under ("### Fixed", "### Added", "### Tests" and the like, read
|
||||
* inside its fragment when it has one), else from the files its pull request touched (only tests, CI and
|
||||
* gstack's own release tooling: maintainers), else from its words (internal shipping machinery: maintainers;
|
||||
* "no longer", "again", "fix": a fix; anything else is new). A release whose own version has no entry, or a version
|
||||
* with no change, refuses the notes instead of publishing a stub or an older version's entry. Notes past NOTES_CAP
|
||||
* keep the changes that fit and send the rest to the full changelog.
|
||||
*/
|
||||
import { parseFragment } from '../../lib/changelog-fragments';
|
||||
import type { Exception, Git } from './release-cut';
|
||||
|
||||
export const NOTES_CAP = 60_000;
|
||||
|
||||
export interface Change { text: string; heading: string }
|
||||
export interface ChangelogEntry { version: string; date: string; changes: Change[] }
|
||||
/** A folded fragment: the pull request whose commit added its file (null when that commit names none), the files that commit touched, and its changes. */
|
||||
export interface FragmentSource { pr: number | null; files: string[]; changes: Change[] }
|
||||
/** The commit that added a version's heading: its pull request (null for a release commit), its files, and the fragments it folded in path order. */
|
||||
export interface VersionSource { pr: number | null; files: string[]; fragments: FragmentSource[] }
|
||||
export interface NotesSources { versions: Record<string, VersionSource> }
|
||||
export interface NotesInput { repo: string; version: string; date: string; prevVersion: string | null; prevTag: string | null; entries: ChangelogEntry[]; hotfix: boolean; first?: boolean; exception?: Exception | null; sources?: NotesSources }
|
||||
export type Notes = { ok: true; body: string; versions: number } | { ok: false; why: string };
|
||||
type Group = 'new' | 'fix' | 'maintainers';
|
||||
|
||||
const GROUPS: Array<[Group, string]> = [['new', "What's new"], ['fix', 'Fixes'], ['maintainers', 'Changes for maintainers']];
|
||||
export const KIND_LABEL = { security: 'Security', regression: 'Regression' } as const;
|
||||
const HEADING_GROUP: Array<[RegExp, Group]> = [
|
||||
[/^(tests?|testing|ci|for contributors|contributors|internal|maintainers?|chores?|infrastructure)\b/i, 'maintainers'],
|
||||
[/^(fixed|fixes|bug ?fixes|security)\b/i, 'fix'],
|
||||
[/^(added|new|features?)\b/i, 'new'],
|
||||
];
|
||||
const NEUTRAL_PATH = /^(changelog\.d\/|CHANGELOG\.md$|VERSION$|package\.json$|bun\.lock$)/;
|
||||
const MAINTAINER_PATH = /^(test\/|crew\/|\.github\/|evals?\/|CONTRIBUTING\.md$|TODOS\.md$|docs\/TESTING)/;
|
||||
const MAINTAINER_WORDS = /\b(test suite|flaky|CI|evals?|receipts?|pregate|ship-queue|merge trains?|carry|lander|release gate|backstop|fix-checks?|stamps?|review plan|review log)\b/i;
|
||||
const FIX_WORDS = /\b(no longer|again|fix(es|ed)?|broken|regression|crash(es|ed)?|bug)\b|instead of (failing|refusing|crashing|stopping)/i;
|
||||
const STAMP_SUBJECT = /^v\d+(\.\d+)+ release\b/;
|
||||
|
||||
/** Four-part versions compare numerically, part by part. */
|
||||
export function compareVersions(a: string, b: string): number {
|
||||
const pa = a.split('.').map(Number), pb = b.split('.').map(Number);
|
||||
for (let i = 0; i < Math.max(pa.length, pb.length); i++) {
|
||||
const d = (pa[i] ?? 0) - (pb[i] ?? 0);
|
||||
if (d !== 0) return d;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** The changes in a run of CHANGELOG lines: top-level bullets with their continuation lines, and plain paragraphs. */
|
||||
export function changesIn(lines: string[]): Change[] {
|
||||
const changes: Change[] = [];
|
||||
let heading = '', open: Change | null = null, blank = false, fence = false;
|
||||
for (const raw of lines) {
|
||||
const l = raw.trimEnd();
|
||||
if (/^\s*(```|~~~)/.test(l)) { fence = !fence; continue; }
|
||||
if (fence) continue;
|
||||
if (/^#{2,}\s/.test(l)) { heading = l.replace(/^#+\s*/, '').trim(); open = null; continue; }
|
||||
if (!l.trim()) { blank = true; continue; }
|
||||
const wasBlank = blank;
|
||||
blank = false;
|
||||
if (/^[-*] /.test(l)) { open = { text: l.slice(2).trim(), heading }; changes.push(open); continue; }
|
||||
if (/^\s*\|/.test(l) || /^\s*<\/?(details|summary)/i.test(l)) { open = null; continue; }
|
||||
if (open && (/^\s/.test(l) || !wasBlank)) { open.text += ' ' + l.trim(); continue; }
|
||||
open = { text: l.trim(), heading };
|
||||
changes.push(open);
|
||||
}
|
||||
return changes;
|
||||
}
|
||||
|
||||
/** Every "## [X.Y.Z.W] - date" section of a CHANGELOG with its changes. */
|
||||
export function changelogEntries(text: string): ChangelogEntry[] {
|
||||
const out: ChangelogEntry[] = [];
|
||||
const lines = text.split('\n');
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const m = /^## \[(\d+(?:\.\d+)+)\](?: - (\S+))?/.exec(lines[i]!);
|
||||
if (!m) continue;
|
||||
let j = i + 1;
|
||||
while (j < lines.length && !lines[j]!.startsWith('## [')) j++;
|
||||
out.push({ version: m[1]!, date: m[2] ?? '', changes: changesIn(lines.slice(i + 1, j)) });
|
||||
i = j - 1;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function firstSentence(text: string): string {
|
||||
let code = false;
|
||||
for (let i = 0; i < text.length; i++) {
|
||||
const c = text[i]!;
|
||||
if (c === '`') code = !code;
|
||||
if (code || !'.!?'.includes(c)) continue;
|
||||
const rest = text.slice(i + 1);
|
||||
if (/^["')*]*$/.test(rest) || /^["')*]*\s+["'(\[*`]*[A-Z0-9/@`]/.test(rest)) return text.slice(0, i + 1) + (/^["')*]+/.exec(rest)?.[0] ?? '');
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
/** One line for a change: its bold lead when that is a sentence, else its first sentence, markdown bold and internal codes removed. */
|
||||
export function changeLine(text: string): string {
|
||||
const bold = /^\*\*(.+?)\*\*/.exec(text);
|
||||
const lead = bold && /[.!?]["')`]*$/.test(bold[1]!.trim()) ? bold[1]! : firstSentence(text);
|
||||
return lead.replace(/\*\*/g, '')
|
||||
.replace(/\s*\((?!CVE-|GHSA-)[A-Z][A-Z0-9]*-\d+(?:(?:,\s*|\s+and\s+)(?!CVE-|GHSA-)[A-Z][A-Z0-9]*-\d+)*\)/g, '')
|
||||
.replace(/\b([Oo])utside voice/g, (_, o: string) => (o === 'O' ? 'S' : 's') + 'econd-model review')
|
||||
.replace(/\s+/g, ' ').replace(/\s+([.,;:!?])/g, '$1').trim();
|
||||
}
|
||||
|
||||
function prOf(subject: string): number | null {
|
||||
const squash = /\(#(\d+)\)\s*$/.exec(subject);
|
||||
const merge = /^Merge pull request #(\d+)/.exec(subject);
|
||||
return squash ? Number(squash[1]) : merge ? Number(merge[1]) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The pull requests behind the versions whose headings were added in from..to along main's first-parent history: for
|
||||
* each, the commit that added its "## [version]" heading and the changelog.d fragments that commit deleted, each read
|
||||
* as it stood just before that commit, with the pull request of the commit that added that file. No from (a first
|
||||
* release) reads nothing.
|
||||
*/
|
||||
export function changeSources(git: Git, from: string | null, to: string): NotesSources {
|
||||
const out: NotesSources = { versions: {} };
|
||||
if (!from) return out;
|
||||
const range = ['--first-parent', '--diff-merges=first-parent', '--no-renames', from + '..' + to];
|
||||
const log = git(['log', '--format=%x00%H%x09%s', '--name-status', ...range]);
|
||||
if (!log.ok) return out;
|
||||
const commits = new Map<string, { pr: number | null; stamp: boolean; files: string[]; folded: string[] }>();
|
||||
for (const block of log.out.split('\0').filter(b => b.trim())) {
|
||||
const [head, ...rest] = block.split('\n');
|
||||
const [sha, subject = ''] = head!.split('\t');
|
||||
const status = rest.filter(l => l.trim()).map(l => l.split('\t'));
|
||||
const folded = status.filter(([kind, file]) => kind === 'D' && /^changelog\.d\/[^/]+\.md$/.test(file!) && file !== 'changelog.d/README.md').map(([, file]) => file!).sort();
|
||||
commits.set(sha!, { pr: prOf(subject), stamp: STAMP_SUBJECT.test(subject), files: status.map(s => s.at(-1)!), folded });
|
||||
}
|
||||
const fragment = (fold: string, file: string): FragmentSource | null => {
|
||||
const added = git(['log', '-1', '--no-patch', '--first-parent', '--diff-merges=first-parent', '--diff-filter=A', '--format=%H%x09%s', fold + '^', '--', file]);
|
||||
const [sha, subject = ''] = added.ok ? added.out.split('\t') : [];
|
||||
const shown = git(['show', fold + '^:' + file]);
|
||||
const parsed = shown.ok ? parseFragment(file, shown.out) : null;
|
||||
if (!parsed || !('body' in parsed)) return null;
|
||||
const files = sha ? git(['show', '--diff-merges=first-parent', '--no-renames', '--format=', '--name-only', sha]) : null;
|
||||
return { pr: sha ? prOf(subject) : null, files: files?.ok ? files.out.split('\n').filter(Boolean) : [], changes: changesIn(parsed.body.split('\n')) };
|
||||
};
|
||||
const headings = git(['log', '--format=%x00%H', '-p', '--unified=0', ...range, '--', 'CHANGELOG.md']);
|
||||
if (headings.ok) for (const block of headings.out.split('\0').filter(b => b.trim())) {
|
||||
const sha = block.slice(0, 40);
|
||||
const c = commits.get(sha);
|
||||
if (!c) continue;
|
||||
for (const m of block.matchAll(/^\+## \[(\d+(?:\.\d+)+)\]/gm)) {
|
||||
out.versions[m[1]!] = { pr: c.stamp ? null : c.pr, files: c.files, fragments: c.folded.map(file => fragment(sha, file)).filter((f): f is FragmentSource => f !== null) };
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function groupOf(line: string, heading: string, files: string[]): Group {
|
||||
const byHeading = HEADING_GROUP.find(([re]) => re.test(heading));
|
||||
if (byHeading) return byHeading[1];
|
||||
const own = files.filter(f => !NEUTRAL_PATH.test(f));
|
||||
if (own.length && own.every(f => MAINTAINER_PATH.test(f))) return 'maintainers';
|
||||
if (MAINTAINER_WORDS.test(line)) return 'maintainers';
|
||||
return FIX_WORDS.test(line) ? 'fix' : 'new';
|
||||
}
|
||||
|
||||
export function releaseKind(hotfix: boolean, exception: Exception | null | undefined): 'security' | 'regression' | 'hotfix' | 'regular' {
|
||||
return exception ? exception.kind : hotfix ? 'hotfix' : 'regular';
|
||||
}
|
||||
|
||||
/** The release notes for versions in (prevVersion, version], or why there are none to publish. */
|
||||
export function releaseNotes(n: NotesInput): Notes {
|
||||
const span = n.entries.filter(e => compareVersions(e.version, n.version) <= 0 && (!n.prevVersion || compareVersions(e.version, n.prevVersion) > 0))
|
||||
.sort((a, b) => compareVersions(b.version, a.version));
|
||||
if (!span.length) return { ok: false, why: 'CHANGELOG.md at the release has no entry for v' + n.version + (n.prevVersion ? ' or any version since v' + n.prevVersion : '') };
|
||||
if (compareVersions(span[0]!.version, n.version) !== 0) return { ok: false, why: 'CHANGELOG.md at the release has no entry for v' + n.version + ', only for the older v' + span[0]!.version + '; write the entry before cutting the release' };
|
||||
const grouped: Record<Group, string[]> = { new: [], fix: [], maintainers: [] };
|
||||
const empty: string[] = [];
|
||||
for (const e of span) {
|
||||
const version = n.sources?.versions[e.version];
|
||||
const folded = version?.fragments ?? [];
|
||||
const own = e.changes.slice(folded.reduce((count, f) => count + f.changes.length, 0));
|
||||
const items: Array<{ change: Change; from?: { pr: number | null; files: string[] } }> = [...folded.flatMap(f => f.changes.map(change => ({ change, from: f }))), ...own.map(change => ({ change, from: version }))];
|
||||
let lines = 0;
|
||||
for (const { change, from } of items) {
|
||||
const line = changeLine(change.text);
|
||||
if (!line) continue;
|
||||
const pr = from?.pr ? ' ([#' + from.pr + '](https://github.com/' + n.repo + '/pull/' + from.pr + '))' : '';
|
||||
grouped[groupOf(line, change.heading, from?.files ?? [])].push('- ' + line + pr);
|
||||
lines++;
|
||||
}
|
||||
if (!lines) empty.push('v' + e.version);
|
||||
}
|
||||
if (empty.length) return { ok: false, why: 'no change to list in the CHANGELOG entry of ' + empty.join(', ') + '; write the entry before cutting the release' };
|
||||
const kind = releaseKind(n.hotfix, n.exception);
|
||||
const changelogUrl = 'https://github.com/' + n.repo + '/blob/v' + n.version + '/CHANGELOG.md';
|
||||
const plural = span.length === 1 ? '' : 's';
|
||||
const render = (omitted: number) => [
|
||||
'## gstack v' + n.version + (kind === 'regular' ? '' : ' (' + kind + ' release)'),
|
||||
'',
|
||||
...(n.exception ? ['**' + KIND_LABEL[n.exception.kind] + ' release:** `' + n.exception.reason + '` (verified: ' + n.exception.evidence + '). Every version since the previous release ships with it.', ''] : []),
|
||||
...(n.first || !n.prevVersion ? ['First tagged release: the ' + span.length + ' version' + plural + (n.prevVersion ? ' since v' + n.prevVersion : '') + '; the [full changelog](' + changelogUrl + ') has every earlier one.', ''] : []),
|
||||
...GROUPS.filter(([g]) => grouped[g].length).flatMap(([g, title]) => ['### ' + title, '', ...grouped[g], '']),
|
||||
...(omitted ? [omitted + (omitted === 1 ? ' more change is' : ' more changes are') + ' in the [full changelog](' + changelogUrl + ').', ''] : []),
|
||||
(n.prevTag ? '[Compare ' + n.prevTag + '...v' + n.version + '](https://github.com/' + n.repo + '/compare/' + n.prevTag + '...v' + n.version + ') · ' : '') + '[Full changelog](' + changelogUrl + ')',
|
||||
'',
|
||||
].join('\n');
|
||||
let omitted = 0, body = render(0);
|
||||
for (const g of [...GROUPS].reverse().map(([g]) => g)) {
|
||||
while (body.length > NOTES_CAP && grouped[g].length) {
|
||||
grouped[g].pop();
|
||||
body = render(++omitted);
|
||||
}
|
||||
}
|
||||
return { ok: true, body, versions: span.length };
|
||||
}
|
||||
Reference in new issue
Block a user