-
Notifications
You must be signed in to change notification settings - Fork 16
Add changelog updater #424
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| import 'dart:convert'; | ||
| import 'dart:io'; | ||
|
|
||
| import 'src/common.dart'; | ||
|
|
||
| String updateChangelogContent(String changelog, String message) { | ||
| final lines = LineSplitter.split(changelog).toList(); | ||
| var currentVersion = '0.0.1'; | ||
| var currentVersionLine = 0; | ||
|
|
||
| for (var i = 0; i < lines.length; i++) { | ||
| final line = lines[i].trim(); | ||
| if (line.startsWith('## ')) { | ||
| final content = line.substring(3).trim(); | ||
| final version = content.split(' ').first; | ||
| if (version.startsWith(RegExp(r'\d'))) { | ||
| currentVersion = version; | ||
| currentVersionLine = i; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| final isWip = currentVersion.endsWith('-wip'); | ||
|
|
||
| final output = <String>[]; | ||
| if (isWip) { | ||
| var sectionEnd = lines.indexWhere( | ||
| (line) => line.startsWith('## '), currentVersionLine + 1) - | ||
| 1; | ||
| if (sectionEnd < 0) { | ||
| sectionEnd = lines.length; | ||
| } | ||
|
|
||
| output.addAll(lines.take(sectionEnd)); | ||
| output.add('- $message'); | ||
| output.addAll(lines.skip(sectionEnd)); | ||
| } else { | ||
| output.addAll([ | ||
| '## $currentVersion-wip', | ||
| '', | ||
| '- $message', | ||
| '', | ||
| ...lines, | ||
| ]); | ||
| } | ||
|
|
||
| return '${output.join('\n')}\n'; | ||
| } | ||
|
|
||
| class ChangelogUpdaterCommand extends ReportCommand { | ||
| ChangelogUpdaterCommand() | ||
| : super('changelog', 'Update a changelog with a new entry.') { | ||
| argParser.addOption( | ||
| 'changelog', | ||
| abbr: 'c', | ||
| help: 'Path to the changelog file to update. Defaults to CHANGELOG.md.', | ||
| ); | ||
| } | ||
|
|
||
| @override | ||
| Future<int> run() async { | ||
| final args = argResults?.rest ?? const <String>[]; | ||
| if (args.isEmpty) { | ||
| stderr.writeln(''' | ||
| Usage: dart run report.dart changelog [--changelog <path>] "Your changelog message"'''); | ||
| stderr | ||
| .writeln('If no --changelog path is provided, CHANGELOG.md is used.'); | ||
| return 1; | ||
| } | ||
|
|
||
| final message = args.join(' '); | ||
| final changelogFile = | ||
| File(argResults?['changelog'] as String? ?? 'CHANGELOG.md'); | ||
|
|
||
| if (!changelogFile.existsSync()) { | ||
| stderr.writeln('Error: ${changelogFile.path} not found.'); | ||
| return 1; | ||
| } | ||
|
|
||
| final newChangelog = updateChangelogContent( | ||
| changelogFile.readAsStringSync(), | ||
| message, | ||
| ); | ||
| changelogFile.writeAsStringSync(newChangelog); | ||
| stdout.writeln('Changelog updated successfully.'); | ||
| return 0; | ||
| } | ||
| } | ||
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,66 @@ | ||
| import 'dart:io'; | ||
|
|
||
| import 'package:repo_manage/changelog_updater.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| void main() { | ||
| group('updateChangelogContent', () { | ||
| test('adds an entry to a wip changelog', () { | ||
| expectGolden( | ||
| inputPath: 'test/data/changelog_wip.md', | ||
| goldenPath: 'test/data/golden/changelog_wip.golden', | ||
| message: 'Add new feature', | ||
| ); | ||
| }); | ||
|
|
||
| test('creates a new wip section from a released changelog', () { | ||
| expectGolden( | ||
| inputPath: 'test/data/changelog_release.md', | ||
| goldenPath: 'test/data/golden/changelog_release.golden', | ||
| message: 'Fix regression', | ||
| ); | ||
| }); | ||
|
|
||
| test('creates a starter changelog when none is present', () { | ||
| expectGolden( | ||
| inputPath: 'test/data/changelog_empty.md', | ||
| goldenPath: 'test/data/golden/changelog_empty.golden', | ||
| message: 'Initial entry', | ||
| ); | ||
| }); | ||
|
|
||
| test('handles complex timezone formatting', () { | ||
| expectGolden( | ||
| inputPath: 'test/data/changelog_complex_timezone.md', | ||
| goldenPath: 'test/data/golden/changelog_complex_timezone.golden', | ||
| message: 'Add new feature', | ||
| ); | ||
| }); | ||
|
|
||
| test('handles complex timezone formatting with WIP suffix', () { | ||
| expectGolden( | ||
| inputPath: 'test/data/changelog_complex_timezone_wip.md', | ||
| goldenPath: 'test/data/golden/changelog_complex_timezone_wip.golden', | ||
| message: 'Add new feature', | ||
| ); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| void expectGolden({ | ||
| required String inputPath, | ||
| required String goldenPath, | ||
| required String message, | ||
| }) { | ||
| final input = File(inputPath).readAsStringSync(); | ||
| final output = updateChangelogContent(input, message); | ||
| final goldenFile = File(goldenPath); | ||
|
|
||
| if (!goldenFile.existsSync()) { | ||
| goldenFile.parent.createSync(recursive: true); | ||
| goldenFile.writeAsStringSync(output); | ||
| return; | ||
| } | ||
|
|
||
| expect(output, goldenFile.readAsStringSync()); | ||
| } |
Oops, something went wrong.
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.