Skip to content

Commit 90f90f6

Browse files
authored
Update changelogs correctly for monorepo packages (#50)
* Update changelogs correctly for monorepo packages Packages within a monorepo should now have their changelogs correctly updated. Previously the update step was incorrectly expecting the monorepo package to be using a tag of the format `v[version]`. This is not how we tag packages within an indepentently versioned monorepo because we need some way to distinguish the tags used for each project. This step has been updated to specify the expected tag prefix as being the package name followed by `@`. * Add two functional tests Two tests have been added to ensure changelog updates work correctly. One test handles the first package release, and the other test handles all other packages releases after the first.
1 parent 2771767 commit 90f90f6

File tree

5 files changed

+233
-12
lines changed

5 files changed

+233
-12
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
},
3737
"devDependencies": {
3838
"@lavamoat/allow-scripts": "^2.0.3",
39-
"@metamask/auto-changelog": "^2.3.0",
39+
"@metamask/auto-changelog": "^3.0.0",
4040
"@metamask/eslint-config": "^10.0.0",
4141
"@metamask/eslint-config-jest": "^10.0.0",
4242
"@metamask/eslint-config-nodejs": "^10.0.0",

src/functional.test.ts

Lines changed: 225 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,8 @@ describe('create-release-branch (functional)', () => {
304304
- Update "a"
305305
- Initial commit
306306
307-
[Unreleased]: https://github.com/example-org/example-repo/compare/v2.0.0...HEAD
308-
[2.0.0]: https://github.com/example-org/example-repo/releases/tag/v2.0.0
307+
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/a@2.0.0...HEAD
308+
[2.0.0]: https://github.com/example-org/example-repo/releases/tag/@scope/a@2.0.0
309309
`),
310310
);
311311
expect(
@@ -318,8 +318,227 @@ describe('create-release-branch (functional)', () => {
318318
### Uncategorized
319319
- Initial commit
320320
321-
[Unreleased]: https://github.com/example-org/example-repo/compare/v2.0.0...HEAD
322-
[2.0.0]: https://github.com/example-org/example-repo/releases/tag/v2.0.0
321+
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
322+
[2.0.0]: https://github.com/example-org/example-repo/releases/tag/@scope/[email protected]
323+
`),
324+
);
325+
},
326+
);
327+
});
328+
329+
it('updates package changelogs with package changes since the last package release', async () => {
330+
await withMonorepoProjectEnvironment(
331+
{
332+
packages: {
333+
$root$: {
334+
name: '@scope/monorepo',
335+
version: '1.0.0',
336+
directoryPath: '.',
337+
},
338+
a: {
339+
name: '@scope/a',
340+
version: '1.0.0',
341+
directoryPath: 'packages/a',
342+
},
343+
b: {
344+
name: '@scope/b',
345+
version: '1.0.0',
346+
directoryPath: 'packages/b',
347+
},
348+
},
349+
workspaces: {
350+
'.': ['packages/*'],
351+
},
352+
createInitialCommit: false,
353+
},
354+
async (environment) => {
355+
// Create an initial commit
356+
await environment.writeFileWithinPackage(
357+
'a',
358+
'CHANGELOG.md',
359+
buildChangelog(`
360+
## [Unreleased]
361+
362+
## [1.0.0]
363+
### Added
364+
- Initial release
365+
366+
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
367+
[1.0.0]: https://github.com/example-org/example-repo/releases/tag/@scope/[email protected]
368+
`),
369+
);
370+
await environment.writeFileWithinPackage(
371+
'b',
372+
'CHANGELOG.md',
373+
buildChangelog(`
374+
## [Unreleased]
375+
376+
## [1.0.0]
377+
### Added
378+
- Initial release
379+
380+
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
381+
[1.0.0]: https://github.com/example-org/example-repo/releases/tag/@scope/[email protected]
382+
`),
383+
);
384+
await environment.createCommit('Initial commit');
385+
await environment.runCommand('git', ['tag', '@scope/[email protected]']);
386+
await environment.runCommand('git', ['tag', '@scope/[email protected]']);
387+
await environment.runCommand('git', ['tag', 'v1.0.0']);
388+
389+
// Create another commit that only changes "a"
390+
await environment.writeFileWithinPackage(
391+
'a',
392+
'dummy.txt',
393+
'Some content',
394+
);
395+
await environment.createCommit('Update "a"');
396+
397+
// Run the tool
398+
await environment.runTool({
399+
releaseSpecification: {
400+
packages: {
401+
a: 'major',
402+
},
403+
},
404+
});
405+
406+
// Only "a" should be updated
407+
expect(
408+
await environment.readFileWithinPackage('a', 'CHANGELOG.md'),
409+
).toStrictEqual(
410+
buildChangelog(`
411+
## [Unreleased]
412+
413+
## [2.0.0]
414+
### Uncategorized
415+
- Update "a"
416+
417+
## [1.0.0]
418+
### Added
419+
- Initial release
420+
421+
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
422+
[2.0.0]: https://github.com/example-org/example-repo/compare/@scope/[email protected]...@scope/[email protected]
423+
[1.0.0]: https://github.com/example-org/example-repo/releases/tag/@scope/[email protected]
424+
`),
425+
);
426+
expect(
427+
await environment.readFileWithinPackage('b', 'CHANGELOG.md'),
428+
).toStrictEqual(
429+
buildChangelog(`
430+
## [Unreleased]
431+
432+
## [1.0.0]
433+
### Added
434+
- Initial release
435+
436+
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
437+
[1.0.0]: https://github.com/example-org/example-repo/releases/tag/@scope/[email protected]
438+
`),
439+
);
440+
},
441+
);
442+
});
443+
444+
it('updates package changelogs with package changes since the last root release if this is the first package release', async () => {
445+
await withMonorepoProjectEnvironment(
446+
{
447+
packages: {
448+
$root$: {
449+
name: '@scope/monorepo',
450+
version: '1.0.0',
451+
directoryPath: '.',
452+
},
453+
a: {
454+
name: '@scope/a',
455+
version: '0.0.0',
456+
directoryPath: 'packages/a',
457+
},
458+
b: {
459+
name: '@scope/b',
460+
version: '1.0.0',
461+
directoryPath: 'packages/b',
462+
},
463+
},
464+
workspaces: {
465+
'.': ['packages/*'],
466+
},
467+
createInitialCommit: false,
468+
},
469+
async (environment) => {
470+
// Create an initial commit
471+
await environment.writeFileWithinPackage(
472+
'a',
473+
'CHANGELOG.md',
474+
buildChangelog(`
475+
## [Unreleased]
476+
477+
[Unreleased]: https://github.com/example-org/example-repo
478+
`),
479+
);
480+
await environment.writeFileWithinPackage(
481+
'b',
482+
'CHANGELOG.md',
483+
buildChangelog(`
484+
## [Unreleased]
485+
486+
## [1.0.0]
487+
### Added
488+
- Initial release
489+
490+
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
491+
[1.0.0]: https://github.com/example-org/example-repo/releases/tag/@scope/[email protected]
492+
`),
493+
);
494+
await environment.createCommit('Initial commit');
495+
await environment.runCommand('git', ['tag', '@scope/[email protected]']);
496+
await environment.runCommand('git', ['tag', 'v1.0.0']);
497+
498+
// Create another commit that only changes "a"
499+
await environment.writeFileWithinPackage(
500+
'a',
501+
'dummy.txt',
502+
'Some content',
503+
);
504+
await environment.createCommit('Update "a"');
505+
506+
// Run the tool
507+
await environment.runTool({
508+
releaseSpecification: {
509+
packages: {
510+
a: 'major',
511+
},
512+
},
513+
});
514+
515+
// Only "a" should be updated
516+
expect(
517+
await environment.readFileWithinPackage('a', 'CHANGELOG.md'),
518+
).toStrictEqual(
519+
buildChangelog(`
520+
## [Unreleased]
521+
522+
## [1.0.0]
523+
### Uncategorized
524+
- Update "a"
525+
526+
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
527+
[1.0.0]: https://github.com/example-org/example-repo/releases/tag/@scope/[email protected]
528+
`),
529+
);
530+
expect(
531+
await environment.readFileWithinPackage('b', 'CHANGELOG.md'),
532+
).toStrictEqual(
533+
buildChangelog(`
534+
## [Unreleased]
535+
536+
## [1.0.0]
537+
### Added
538+
- Initial release
539+
540+
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/[email protected]
541+
[1.0.0]: https://github.com/example-org/example-repo/releases/tag/@scope/[email protected]
323542
`),
324543
);
325544
},
@@ -652,8 +871,8 @@ The release spec file has been retained for you to edit again and make the neces
652871
- Update "a"
653872
- Initial commit
654873
655-
[Unreleased]: https://github.com/example-org/example-repo/compare/v2.0.0...HEAD
656-
[2.0.0]: https://github.com/example-org/example-repo/releases/tag/v2.0.0
874+
[Unreleased]: https://github.com/example-org/example-repo/compare/@scope/a@2.0.0...HEAD
875+
[2.0.0]: https://github.com/example-org/example-repo/releases/tag/@scope/a@2.0.0
657876
`),
658877
);
659878
expect(

src/package.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ describe('package', () => {
474474
isReleaseCandidate: true,
475475
projectRootDirectory: sandbox.directoryPath,
476476
repoUrl: 'https://repo.url',
477+
tagPrefixes: ['package@', 'v'],
477478
})
478479
.mockResolvedValue('new changelog');
479480
await fs.promises.writeFile(changelogPath, 'existing changelog');

src/package.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ async function updatePackageChangelog({
271271
isReleaseCandidate: true,
272272
projectRootDirectory: pkg.directoryPath,
273273
repoUrl: repositoryUrl,
274+
tagPrefixes: [`${pkg.validatedManifest.name}@`, 'v'],
274275
});
275276

276277
if (newChangelogContent) {

yarn.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -873,17 +873,17 @@ __metadata:
873873
languageName: node
874874
linkType: hard
875875

876-
"@metamask/auto-changelog@npm:^2.3.0":
877-
version: 2.6.1
878-
resolution: "@metamask/auto-changelog@npm:2.6.1"
876+
"@metamask/auto-changelog@npm:^3.0.0":
877+
version: 3.0.0
878+
resolution: "@metamask/auto-changelog@npm:3.0.0"
879879
dependencies:
880880
diff: ^5.0.0
881881
execa: ^5.1.1
882882
semver: ^7.3.5
883883
yargs: ^17.0.1
884884
bin:
885885
auto-changelog: dist/cli.js
886-
checksum: 6ae84de492e4aec710ff2dd793f258b7cc3aba3fdeb416c0d3ab55a156da61b4ccd3272e9a6608f32b71695dc42b527a380ce62566e387240665556c8da26de3
886+
checksum: ee6f41b466e8f0deb8bc454936513602c4767b7be94f704da3579e3100154c92779dcfde542076158138d5fcfb64ce491ab7fc30248ae097c7d903be0cad9fb4
887887
languageName: node
888888
linkType: hard
889889

@@ -893,7 +893,7 @@ __metadata:
893893
dependencies:
894894
"@lavamoat/allow-scripts": ^2.0.3
895895
"@metamask/action-utils": ^0.0.2
896-
"@metamask/auto-changelog": ^2.3.0
896+
"@metamask/auto-changelog": ^3.0.0
897897
"@metamask/eslint-config": ^10.0.0
898898
"@metamask/eslint-config-jest": ^10.0.0
899899
"@metamask/eslint-config-nodejs": ^10.0.0

0 commit comments

Comments
 (0)