Skip to content

Commit dcf3e3d

Browse files
authored
Allow prerelease versions in release headers (#130)
Currently, this tool expects the version in a release header to be a version that looks like `<major>.<minor>.<patch>` and rejects other formats. However, it may be useful to publish a prerelease version of a package so that integration with other products/services can be tested in a more "live" manner, particularly if that package is a part of a monorepo. A prerelease version looks like `<major>.<minor>.<patch>-<prerelease identifier>.<number>`, where `<prerelease identifier>` can be an arbitrary word like `alpha`, `beta`, or `rc`. This commit allows such versions.
1 parent 9beab40 commit dcf3e3d

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

src/parse-changelog.test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,45 @@ describe('parseChangelog', () => {
199199
expect(changelog.getUnreleasedChanges()).toStrictEqual({});
200200
});
201201

202+
it('should parse changelog with prereleases', () => {
203+
const changelog = parseChangelog({
204+
changelogContent: outdent`
205+
# Changelog
206+
All notable changes to this project will be documented in this file.
207+
208+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
209+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
210+
211+
## [Unreleased]
212+
213+
## [1.0.0-rc.1] - 2020-01-01
214+
### Changed
215+
- Something else
216+
217+
## [0.0.2-beta.1] - 2020-01-01
218+
### Fixed
219+
- Something
220+
221+
## [0.0.1-alpha.1] - 2020-01-01
222+
### Changed
223+
- Something
224+
225+
[Unreleased]: https://github.com/ExampleUsernameOrOrganization/ExampleRepository/compare/v1.0.0-rc.1...HEAD
226+
[1.0.0-rc.1]: https://github.com/ExampleUsernameOrOrganization/ExampleRepository/compare/v0.0.2-beta.1...v1.0.0-rc.1
227+
[0.0.2-beta.1]: https://github.com/ExampleUsernameOrOrganization/ExampleRepository/compare/v0.0.1-alpha.1...v0.0.2-beta.1
228+
[0.0.1-alpha.1]: https://github.com/ExampleUsernameOrOrganization/ExampleRepository/releases/tag/v0.0.1-alpha.1
229+
`,
230+
repoUrl:
231+
'https://github.com/ExampleUsernameOrOrganization/ExampleRepository',
232+
});
233+
234+
expect(changelog.getReleases()).toStrictEqual([
235+
{ date: '2020-01-01', status: undefined, version: '1.0.0-rc.1' },
236+
{ date: '2020-01-01', status: undefined, version: '0.0.2-beta.1' },
237+
{ date: '2020-01-01', status: undefined, version: '0.0.1-alpha.1' },
238+
]);
239+
});
240+
202241
it('should parse changelog with release statuses', () => {
203242
const changelog = parseChangelog({
204243
changelogContent: outdent`
@@ -508,6 +547,32 @@ describe('parseChangelog', () => {
508547
).toThrow(`Malformed release header: '## [1.0.0 - 2020-01-01'`);
509548
});
510549

550+
it('should throw if version in release header is not SemVer-compatible', () => {
551+
const brokenChangelog = outdent`
552+
# Changelog
553+
All notable changes to this project will be documented in this file.
554+
555+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
556+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
557+
558+
## [Unreleased]
559+
560+
## [1.2.3.4]
561+
### Changed
562+
- Something else
563+
564+
[Unreleased]: https://github.com/ExampleUsernameOrOrganization/ExampleRepository/compare/v1.2.3.4...HEAD
565+
[1.2.3.4]: https://github.com/ExampleUsernameOrOrganization/ExampleRepository/releases/tag/v1.2.3.4
566+
`;
567+
expect(() =>
568+
parseChangelog({
569+
changelogContent: brokenChangelog,
570+
repoUrl:
571+
'https://github.com/ExampleUsernameOrOrganization/ExampleRepository',
572+
}),
573+
).toThrow(`Invalid SemVer version in release header: '## [1.2.3.4]`);
574+
});
575+
511576
it('should throw if release header uses the wrong header level', () => {
512577
const brokenChangelog = outdent`
513578
# Changelog

src/parse-changelog.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import semver from 'semver';
12
import Changelog from './changelog';
23
import { ChangeCategory, unreleased } from './constants';
34

@@ -107,12 +108,18 @@ export function parseChangelog({
107108
for (const line of contentfulChangelogLines) {
108109
if (line.startsWith('## [')) {
109110
const results = line.match(
110-
/^## \[(\d+\.\d+\.\d+)\](?: - (\d\d\d\d-\d\d-\d\d))?(?: \[(\w+)\])?/u,
111+
/^## \[([^[\]]+)\](?: - (\d\d\d\d-\d\d-\d\d))?(?: \[(\w+)\])?/u,
111112
);
112113
if (results === null) {
113114
throw new Error(`Malformed release header: '${truncated(line)}'`);
114115
}
115116

117+
if (semver.valid(results[1]) === null) {
118+
throw new Error(
119+
`Invalid SemVer version in release header: '${truncated(line)}'`,
120+
);
121+
}
122+
116123
// Trailing newline removed because the release section is expected to
117124
// be prefixed by a newline.
118125
finalizePreviousChange({

0 commit comments

Comments
 (0)