-
Notifications
You must be signed in to change notification settings - Fork 0
Release-Workflow übernimmt den handgeschriebenen Unreleased-Abschnitt #234
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
Merged
CallMeTechie
merged 2 commits into
master
from
fix/changelog-release-consumes-unreleased
Jul 26, 2026
Merged
Changes from all commits
Commits
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
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,106 @@ | ||
| #!/usr/bin/env node | ||
| 'use strict'; | ||
|
|
||
| /** | ||
| * Prepare CHANGELOG.md for a release. | ||
| * | ||
| * Wird vom Release-Workflow aufgerufen. Die Logik lag früher als awk-Einzeiler | ||
| * im YAML und war damit nicht testbar — sie hatte einen Fehler, der sich über | ||
| * 24 Releases angesammelt hat: | ||
| * | ||
| * awk '/^# Changelog/ { print; print ""; print block; … }' | ||
| * | ||
| * Der erzeugte Versionsblock wurde blind hinter die Überschrift geschoben. Ein | ||
| * von Hand gepflegter `## [Unreleased]`-Abschnitt blieb dabei liegen, wo er | ||
| * war — also unterhalb der neuen Version. Folge: die ausführlichen | ||
| * Beschreibungen erschienen in keinem Release, und die Datei sammelte | ||
| * `[Unreleased]`-Überschriften an, die längst ausgeliefert waren. | ||
| * | ||
| * Neues Verhalten: | ||
| * - Steht direkt unter `# Changelog` ein `## [Unreleased]` MIT Inhalt, wird | ||
| * dessen Überschrift zur Version. Der aus der Commit-Message erzeugte | ||
| * Eintrag entfällt dann: hat jemand die Änderung beschrieben, ist die | ||
| * erste Zeile des letzten Commits die schlechtere Zusammenfassung. | ||
| * - Sonst wird wie bisher ein Block aus der Commit-Message erzeugt. | ||
| * | ||
| * Aufruf: node scripts/changelog-release.js <version> <date> <commit-subject> [datei] | ||
| */ | ||
|
|
||
| const fs = require('node:fs'); | ||
|
|
||
| function sectionFor(subject) { | ||
| if (/^feat/.test(subject)) return 'Features'; | ||
| if (/^fix/.test(subject)) return 'Fixes'; | ||
| if (/^docs/.test(subject)) return 'Dokumentation'; | ||
| return 'Änderungen'; | ||
| } | ||
|
|
||
| function entryFor(subject) { | ||
| // "feat(scope): text" → "text"; ohne Präfix bleibt die Zeile, wie sie ist. | ||
| const m = subject.match(/^[a-z]+(\([^)]*\))?!?:\s*(.*)$/); | ||
| return m ? m[2] : subject; | ||
| } | ||
|
|
||
| /** | ||
| * @returns {{ text: string, promoted: boolean }} promoted=true, wenn ein | ||
| * handgepflegter Unreleased-Block zur Version wurde. | ||
| */ | ||
| function prepare(content, version, date, subject) { | ||
| const lines = content.split('\n'); | ||
| const headingAt = lines.findIndex((l) => /^## \[/.test(l)); | ||
| const headerEnd = headingAt === -1 ? lines.length : headingAt; | ||
|
|
||
| const isUnreleased = headingAt !== -1 && /^## \[Unreleased\]/i.test(lines[headingAt]); | ||
| if (isUnreleased) { | ||
| // Inhalt bis zur nächsten Überschrift — Trennstriche und Leerzeilen zählen nicht. | ||
| const next = lines.findIndex((l, i) => i > headingAt && /^## \[/.test(l)); | ||
| const end = next === -1 ? lines.length : next; | ||
| const hasContent = lines.slice(headingAt + 1, end) | ||
| .some((l) => l.trim() && l.trim() !== '---'); | ||
| if (hasContent) { | ||
| const out = lines.slice(); | ||
| out[headingAt] = `## [${version}] — ${date}`; | ||
| return { text: out.join('\n'), promoted: true }; | ||
| } | ||
| } | ||
|
|
||
| const block = [ | ||
| `## [${version}] — ${date}`, | ||
| '', | ||
| `### ${sectionFor(subject)}`, | ||
| `- ${entryFor(subject)}`, | ||
| '', | ||
| '---', | ||
| '', | ||
| ]; | ||
| const out = lines.slice(0, headerEnd).concat(block, lines.slice(headerEnd)); | ||
| return { text: out.join('\n'), promoted: false }; | ||
| } | ||
|
|
||
| module.exports = { prepare, sectionFor, entryFor }; | ||
|
|
||
| if (require.main === module) { | ||
| const [version, date, subject, file = 'CHANGELOG.md'] = process.argv.slice(2); | ||
| if (!version || !date || subject === undefined) { | ||
| console.error('usage: changelog-release.js <version> <date> <commit-subject> [file]'); | ||
| process.exit(2); | ||
| } | ||
| // Lesen und den Fehlerfall abfangen, statt vorher auf Existenz zu prüfen: | ||
| // existsSync + readFileSync ist Check-then-Use (CodeQL js/file-system-race) | ||
| // — und der Versuch ist ohnehin die kürzere Fassung. | ||
| let current; | ||
| try { | ||
| current = fs.readFileSync(file, 'utf8'); | ||
| } catch (e) { | ||
| if (e.code === 'ENOENT') { | ||
| console.error(`${file} not found — nothing to do`); | ||
| process.exit(0); | ||
| } | ||
| throw e; | ||
| } | ||
| const { text, promoted } = prepare(current, version, date, subject.split('\n')[0]); | ||
| fs.writeFileSync(file, text); | ||
| console.log(promoted | ||
| ? `promoted the hand-written [Unreleased] block to [${version}]` | ||
| : `inserted a generated block for [${version}]`); | ||
| } | ||
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,164 @@ | ||
| 'use strict'; | ||
| const { test } = require('node:test'); | ||
| const assert = require('node:assert/strict'); | ||
| const { prepare } = require('../scripts/changelog-release'); | ||
|
|
||
| // Regression gegen den Fehler, der sich über 24 Releases angesammelt hat: der | ||
| // Release-Workflow schob seinen erzeugten Block blind hinter "# Changelog" und | ||
| // ließ einen handgepflegten [Unreleased]-Abschnitt darunter liegen. Dessen | ||
| // Beschreibungen erschienen dadurch in keinem Release, und die Datei sammelte | ||
| // [Unreleased]-Überschriften an, die längst ausgeliefert waren. | ||
|
|
||
| const WITH_UNRELEASED = `# Changelog | ||
|
|
||
| ## [Unreleased] | ||
|
|
||
| ### Security | ||
| - Etwas Wichtiges, von Hand beschrieben. | ||
|
|
||
| ### Fixed | ||
| - Noch etwas. | ||
|
|
||
| --- | ||
|
|
||
| ## [1.0.0] — 2026-01-01 | ||
|
|
||
| ### Fixes | ||
| - alt | ||
|
|
||
| --- | ||
| `; | ||
|
|
||
| const WITHOUT_UNRELEASED = `# Changelog | ||
|
|
||
| ## [1.0.0] — 2026-01-01 | ||
|
|
||
| ### Fixes | ||
| - alt | ||
|
|
||
| --- | ||
| `; | ||
|
|
||
| const EMPTY_UNRELEASED = `# Changelog | ||
|
|
||
| ## [Unreleased] | ||
|
|
||
| --- | ||
|
|
||
| ## [1.0.0] — 2026-01-01 | ||
|
|
||
| ### Fixes | ||
| - alt | ||
|
|
||
| --- | ||
| `; | ||
|
|
||
| function headings(text) { | ||
| return text.split('\n').filter((l) => l.startsWith('## [')); | ||
| } | ||
|
|
||
| test('a hand-written Unreleased block becomes the release', () => { | ||
| const { text, promoted } = prepare(WITH_UNRELEASED, '1.1.0', '2026-02-02', 'fix: irgendwas'); | ||
| assert.equal(promoted, true); | ||
| assert.deepEqual(headings(text), ['## [1.1.0] — 2026-02-02', '## [1.0.0] — 2026-01-01']); | ||
| // Der Inhalt reist mit — das ist der ganze Punkt. | ||
| assert.match(text, /## \[1\.1\.0\] — 2026-02-02\n\n### Security\n- Etwas Wichtiges/); | ||
| assert.match(text, /- Noch etwas\./); | ||
| // Kein zurückgelassener Unreleased-Block mehr. | ||
| assert.equal((text.match(/## \[Unreleased\]/g) || []).length, 0); | ||
| }); | ||
|
|
||
| test('the commit-derived entry is dropped when a human already described the release', () => { | ||
| const { text } = prepare(WITH_UNRELEASED, '1.1.0', '2026-02-02', 'fix: irgendwas'); | ||
| assert.ok(!text.includes('irgendwas'), 'die Commit-Zeile verdrängt die Beschreibung'); | ||
| }); | ||
|
|
||
| test('without an Unreleased block the generated entry is inserted as before', () => { | ||
| const { text, promoted } = prepare(WITHOUT_UNRELEASED, '1.1.0', '2026-02-02', 'feat(scope): neue Sache'); | ||
| assert.equal(promoted, false); | ||
| assert.deepEqual(headings(text), ['## [1.1.0] — 2026-02-02', '## [1.0.0] — 2026-01-01']); | ||
| assert.match(text, /### Features\n- neue Sache/); | ||
| }); | ||
|
|
||
| test('an empty Unreleased block falls back to the generated entry', () => { | ||
| // Sonst entstünde ein Versionsblock ohne jeden Inhalt. | ||
| const { text, promoted } = prepare(EMPTY_UNRELEASED, '1.1.0', '2026-02-02', 'fix: etwas'); | ||
| assert.equal(promoted, false); | ||
| assert.match(text, /### Fixes\n- etwas/); | ||
| }); | ||
|
|
||
| test('the commit type decides the section, and the prefix is stripped', () => { | ||
| for (const [subject, section, entry] of [ | ||
| ['feat: a', 'Features', 'a'], | ||
| ['feat(x): b', 'Features', 'b'], | ||
| ['fix!: c', 'Fixes', 'c'], | ||
| ['docs: d', 'Dokumentation', 'd'], | ||
| ['chore: e', 'Änderungen', 'e'], | ||
| ['ganz ohne Präfix', 'Änderungen', 'ganz ohne Präfix'], | ||
| ]) { | ||
| const { text } = prepare(WITHOUT_UNRELEASED, '1.1.0', '2026-02-02', subject); | ||
| assert.match(text, new RegExp(`### ${section}\\n- ${entry.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}`), subject); | ||
| } | ||
| }); | ||
|
|
||
| test('only the topmost block is promoted — stray Unreleased headings stay untouched', () => { | ||
| // Die 24 Altlasten in der echten Datei dürfen nicht versehentlich zur Version werden. | ||
| const stray = `# Changelog | ||
|
|
||
| ## [1.0.0] — 2026-01-01 | ||
|
|
||
| ### Fixes | ||
| - alt | ||
|
|
||
| --- | ||
|
|
||
| ## [Unreleased] | ||
|
|
||
| ### Fixed | ||
| - eine Altlast weiter unten | ||
|
|
||
| --- | ||
| `; | ||
| const { text, promoted } = prepare(stray, '1.1.0', '2026-02-02', 'fix: etwas'); | ||
| assert.equal(promoted, false); | ||
| assert.equal((text.match(/## \[Unreleased\]/g) || []).length, 1, 'die Altlast wurde angefasst'); | ||
| assert.deepEqual(headings(text)[0], '## [1.1.0] — 2026-02-02'); | ||
| }); | ||
|
|
||
| // Beide Fälle werden aus der echten Datei ABGELEITET, statt ihren jeweiligen | ||
| // Zustand vorauszusetzen — sonst kippt der Test, sobald jemand einen | ||
| // Unreleased-Abschnitt anlegt oder ein Release ihn befördert. | ||
| function realChangelog() { | ||
| const fs = require('node:fs'); | ||
| const path = require('node:path'); | ||
| return fs.readFileSync(path.join(__dirname, '..', 'CHANGELOG.md'), 'utf8'); | ||
| } | ||
| function withoutTopUnreleased(text) { | ||
| const lines = text.split('\n'); | ||
| const h = lines.findIndex((l) => /^## \[/.test(l)); | ||
| if (h === -1 || !/^## \[Unreleased\]/i.test(lines[h])) return text; | ||
| const next = lines.findIndex((l, i) => i > h && /^## \[/.test(l)); | ||
| return lines.slice(0, h).concat(lines.slice(next === -1 ? lines.length : next)).join('\n'); | ||
| } | ||
|
|
||
| test('the real CHANGELOG: without a top Unreleased block, exactly one heading is added', () => { | ||
| const real = withoutTopUnreleased(realChangelog()); | ||
| const { text, promoted } = prepare(real, '9.9.9', '2026-12-31', 'fix: probe'); | ||
| assert.equal(promoted, false); | ||
| assert.equal(headings(text).length, headings(real).length + 1); | ||
| assert.equal(headings(text)[0], '## [9.9.9] — 2026-12-31'); | ||
| assert.ok(text.startsWith('# Changelog')); | ||
| // Kein Bestandsinhalt verloren: die alte Datei steckt vollständig in der neuen. | ||
| assert.ok(text.includes(real.slice(real.indexOf('## [')))); | ||
| }); | ||
|
|
||
| test('the real CHANGELOG: with a hand-written block on top, it is promoted and nothing is added', () => { | ||
| const base = withoutTopUnreleased(realChangelog()); | ||
| const withBlock = base.replace('# Changelog\n', '# Changelog\n\n## [Unreleased]\n\n### Security\n- von Hand\n\n---\n'); | ||
| const { text, promoted } = prepare(withBlock, '9.9.9', '2026-12-31', 'fix: probe'); | ||
| assert.equal(promoted, true); | ||
| assert.equal(headings(text).length, headings(withBlock).length, 'es kam eine Überschrift dazu'); | ||
| assert.equal(headings(text)[0], '## [9.9.9] — 2026-12-31'); | ||
| assert.match(text, /## \[9\.9\.9\] — 2026-12-31\n\n### Security\n- von Hand/); | ||
| assert.ok(!text.includes('- probe'), 'die Commit-Zeile wurde zusätzlich eingefügt'); | ||
| }); |
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.