-
Notifications
You must be signed in to change notification settings - Fork 36
feat: consolidate per-package changelogs into root CHANGELOG #316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
burczu
wants to merge
6
commits into
callstack:main
Choose a base branch
from
burczu:feat/automate-release-creation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+199
−1
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8a0cfff
feat: consolidate per-package changelogs into root CHANGELOG on version
burczu 36ef4e6
fix(scripts): sort package CHANGELOG paths for deterministic output
burczu b90bb63
fix(scripts): preserve distinct entries that share a PR number
burczu 314265e
fix(scripts): skip writing empty version blocks
burczu a5394ca
fix(scripts): robust regex for idempotency check
burczu e70ec83
fix(scripts): detect version heading at file start
burczu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
|
|
||
| const ROOT_DIR = process.cwd(); | ||
| const PACKAGES_DIR = path.join(ROOT_DIR, 'packages'); | ||
| const ROOT_CHANGELOG = path.join(ROOT_DIR, 'CHANGELOG.md'); | ||
|
|
||
| const SECTION_ORDER = ['Major Changes', 'Minor Changes', 'Patch Changes']; | ||
|
|
||
| interface ParsedVersion { | ||
| version: string; | ||
| sections: Map<string, string[]>; | ||
| } | ||
|
|
||
| function extractEntryKey(entry: string): string { | ||
| const prMatch = entry.match(/\[#(\d+)\]/); | ||
| if (prMatch) return `pr-${prMatch[1]}`; | ||
|
|
||
| const hashMatch = entry.match(/\[`([a-f0-9]{7,40})`\]/); | ||
| if (hashMatch) return `commit-${hashMatch[1]}`; | ||
|
|
||
| return entry.trim(); | ||
| } | ||
|
|
||
| function parseEntries(block: string): string[] { | ||
| const entries: string[] = []; | ||
| let current: string[] = []; | ||
|
|
||
| for (const line of block.split('\n')) { | ||
| if (line.startsWith('- ')) { | ||
| if (current.length > 0) entries.push(current.join('\n').trim()); | ||
| current = [line]; | ||
| } else if (line.startsWith(' ') && current.length > 0) { | ||
| current.push(line); | ||
| } | ||
| // blank lines and non-indented non-bullet lines within a block are ignored | ||
| } | ||
|
|
||
| if (current.length > 0) entries.push(current.join('\n').trim()); | ||
|
|
||
| return entries.filter((e) => e.length > 0); | ||
| } | ||
|
|
||
| function parseLatestVersion(content: string): ParsedVersion | null { | ||
| const lines = content.split('\n'); | ||
|
|
||
| let vStart = -1; | ||
| let version = ''; | ||
| for (let i = 0; i < lines.length; i++) { | ||
| const m = lines[i].match(/^## (\d+\.\d+\.\d+)/); | ||
| if (m) { | ||
| vStart = i; | ||
| version = m[1]; | ||
| break; | ||
| } | ||
| } | ||
| if (vStart === -1) return null; | ||
|
|
||
| let vEnd = lines.length; | ||
| for (let i = vStart + 1; i < lines.length; i++) { | ||
| if (lines[i].match(/^## /)) { | ||
| vEnd = i; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| const sectionContent = lines.slice(vStart + 1, vEnd).join('\n'); | ||
| const subsectionHeaders = [...sectionContent.matchAll(/^### (.+)$/gm)]; | ||
| const subsectionBodies = sectionContent.split(/^### .+$/m); | ||
|
|
||
| const sections = new Map<string, string[]>(); | ||
|
|
||
| for (let i = 0; i < subsectionHeaders.length; i++) { | ||
| const name = subsectionHeaders[i][1].trim(); | ||
| const body = subsectionBodies[i + 1] ?? ''; | ||
| const entries = parseEntries(body).filter( | ||
| (e) => !e.startsWith('- Updated dependencies') | ||
| ); | ||
|
|
||
| if (entries.length > 0) { | ||
| sections.set(name, entries); | ||
| } | ||
| } | ||
|
|
||
| return { version, sections }; | ||
| } | ||
|
|
||
| function consolidate(): void { | ||
| const changelogPaths = fs | ||
| .readdirSync(PACKAGES_DIR, { withFileTypes: true }) | ||
| .filter((d) => d.isDirectory()) | ||
| .map((d) => path.join(PACKAGES_DIR, d.name, 'CHANGELOG.md')) | ||
| .filter((p) => fs.existsSync(p)) | ||
| .sort(); | ||
|
|
||
|
burczu marked this conversation as resolved.
|
||
| if (changelogPaths.length === 0) { | ||
| console.error('No package CHANGELOG files found.'); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| let targetVersion: string | null = null; | ||
| const consolidated = new Map<string, Map<string, string>>(); | ||
|
|
||
| for (const changelogPath of changelogPaths) { | ||
| const parsed = parseLatestVersion(fs.readFileSync(changelogPath, 'utf-8')); | ||
| if (!parsed) continue; | ||
|
|
||
| if (!targetVersion) { | ||
| targetVersion = parsed.version; | ||
| } else if (parsed.version !== targetVersion) { | ||
| console.warn( | ||
| `Version mismatch: expected ${targetVersion}, got ${parsed.version} in ${changelogPath}` | ||
| ); | ||
| continue; | ||
| } | ||
|
|
||
| for (const [section, entries] of parsed.sections) { | ||
| if (!consolidated.has(section)) consolidated.set(section, new Map()); | ||
| const target = consolidated.get(section)!; | ||
| for (const entry of entries) { | ||
| const key = extractEntryKey(entry); | ||
| if (!target.has(key)) target.set(key, entry); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (!targetVersion) { | ||
| console.error('Could not determine release version from package CHANGELOGs.'); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| if (consolidated.size === 0) { | ||
| console.log( | ||
| `No substantive entries for ${targetVersion} (all filtered as "Updated dependencies"), skipping root CHANGELOG update.` | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| // Idempotency: skip if this version is already in the root CHANGELOG | ||
| if (fs.existsSync(ROOT_CHANGELOG)) { | ||
| const existing = fs.readFileSync(ROOT_CHANGELOG, 'utf-8'); | ||
| const escapedVersion = targetVersion.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
| const versionHeadingRe = new RegExp(`^##\\s+${escapedVersion}(\\s|$)`, 'm'); | ||
| if (versionHeadingRe.test(existing)) { | ||
| console.log(`Root CHANGELOG already contains ${targetVersion}, skipping.`); | ||
| return; | ||
| } | ||
|
burczu marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // Build new version block | ||
| const block: string[] = [`## ${targetVersion}`, '']; | ||
|
|
||
| const orderedSections = [ | ||
| ...SECTION_ORDER.filter((s) => consolidated.has(s)), | ||
| ...[...consolidated.keys()].filter((s) => !SECTION_ORDER.includes(s)), | ||
| ]; | ||
|
|
||
| for (const section of orderedSections) { | ||
| const entries = [...consolidated.get(section)!.values()]; | ||
| if (entries.length === 0) continue; | ||
| block.push(`### ${section}`, ''); | ||
| for (const entry of entries) { | ||
| block.push(entry, ''); | ||
| } | ||
| } | ||
|
burczu marked this conversation as resolved.
|
||
|
|
||
| const newBlock = block.join('\n'); | ||
|
|
||
| let header: string; | ||
| let body: string; | ||
|
|
||
| if (fs.existsSync(ROOT_CHANGELOG)) { | ||
| const content = fs.readFileSync(ROOT_CHANGELOG, 'utf-8'); | ||
| const firstHeadingMatch = content.match(/^## /m); | ||
| if (firstHeadingMatch && firstHeadingMatch.index !== undefined) { | ||
| header = content.slice(0, firstHeadingMatch.index); | ||
| body = content.slice(firstHeadingMatch.index); | ||
| } else { | ||
| header = content.endsWith('\n') ? content : content + '\n'; | ||
| body = ''; | ||
| } | ||
|
burczu marked this conversation as resolved.
|
||
| } else { | ||
| header = `# Changelog\n\n_History prior to ${targetVersion} is available in the per-package CHANGELOG files._\n\n`; | ||
| body = ''; | ||
| } | ||
|
|
||
| fs.writeFileSync( | ||
| ROOT_CHANGELOG, | ||
| header + newBlock + (body ? '\n' + body : '\n'), | ||
| 'utf-8' | ||
| ); | ||
| console.log(`✓ Root CHANGELOG.md updated with ${targetVersion}`); | ||
| } | ||
|
|
||
| consolidate(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "type": "module" | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.