From 1e26187fcf66a1c2f86483d3494eedc567150610 Mon Sep 17 00:00:00 2001 From: Andrew Lisowski Date: Wed, 14 Oct 2020 16:41:21 -0700 Subject: [PATCH] group monorepo changelog lines --- .../__snapshots__/monorepo-log.test.ts.snap | 21 ++++- plugins/npm/__tests__/monorepo-log.test.ts | 76 ++++++++++++++++++- plugins/npm/src/index.ts | 39 ++++++++++ 3 files changed, 130 insertions(+), 6 deletions(-) diff --git a/plugins/npm/__tests__/__snapshots__/monorepo-log.test.ts.snap b/plugins/npm/__tests__/__snapshots__/monorepo-log.test.ts.snap index c4a6ddb94..54c0269fa 100644 --- a/plugins/npm/__tests__/__snapshots__/monorepo-log.test.ts.snap +++ b/plugins/npm/__tests__/__snapshots__/monorepo-log.test.ts.snap @@ -6,7 +6,6 @@ exports[`should add versions for independent packages 1`] = ` - woot [#12343](https://github.custom.com/pull/12343) (adam@dierkens.com) - \`@foobar/release@1.0.1\`, \`@foobar/party@1.0.3\` - [PLAYA-5052] - Some Feature [#12345](https://github.custom.com/pull/12345) (adam@dierkens.com) -- \`@foobar/release@1.0.1\`, \`@foobar/party@1.0.3\` - [PLAYA-5052] - Some Feature - Revert [#12345](https://github.custom.com/pull/12345) (adam@dierkens.com) #### 🏠 Internal @@ -39,9 +38,27 @@ exports[`should create sections for packages 1`] = ` "#### 💥 Breaking Change - woot [#12343](https://github.custom.com/pull/12343) (adam@dierkens.com) -- \`@foobar/release\`, \`@foobar/party\` +- \`@foobar/party\` - [PLAYA-5052] - Some Feature [#12345](https://github.custom.com/pull/12345) (adam@dierkens.com) +- \`@foobar/release\` + - [PLAYA-5052] - Some Feature - Revert [#12345](https://github.custom.com/pull/12345) (adam@dierkens.com) + +#### 🏠 Internal + +- \`@foobar/release\` + - Another Feature [#1234](https://github.custom.com/pull/1234) (adam@dierkens.com) + +#### Authors: 1 + +- Adam Dierkens (adam@dierkens.com)" +`; + +exports[`should group sections for packages 1`] = ` +"#### 💥 Breaking Change + +- woot [#12343](https://github.custom.com/pull/12343) (adam@dierkens.com) - \`@foobar/release\`, \`@foobar/party\` + - [PLAYA-5052] - Some Feature [#12345](https://github.custom.com/pull/12345) (adam@dierkens.com) - [PLAYA-5052] - Some Feature - Revert [#12345](https://github.custom.com/pull/12345) (adam@dierkens.com) #### 🏠 Internal diff --git a/plugins/npm/__tests__/monorepo-log.test.ts b/plugins/npm/__tests__/monorepo-log.test.ts index 2018c7c2d..72bef495e 100644 --- a/plugins/npm/__tests__/monorepo-log.test.ts +++ b/plugins/npm/__tests__/monorepo-log.test.ts @@ -19,9 +19,13 @@ jest.mock("child_process"); // @ts-ignore execSync.mockImplementation(exec); -jest.mock("../../../packages/core/dist/utils/get-lerna-packages", () => (...args: any[]) => getLernaPackages(...args)); -jest.mock("../../../packages/core/dist/utils/exec-promise", () => (...args: any[]) => - execPromise(...args) +jest.mock( + "../../../packages/core/dist/utils/get-lerna-packages", + () => (...args: any[]) => getLernaPackages(...args) +); +jest.mock( + "../../../packages/core/dist/utils/exec-promise", + () => (...args: any[]) => execPromise(...args) ); jest.mock("fs", () => ({ // @ts-ignore @@ -55,7 +59,7 @@ const commitsPromise = logParse.normalizeCommits([ }), ]); -test("should create sections for packages", async () => { +test("should group sections for packages", async () => { let changed = 0; getLernaPackages.mockImplementation(async () => @@ -115,6 +119,70 @@ test("should create sections for packages", async () => { expect(await changelog.generateReleaseNotes(commits)).toMatchSnapshot(); }); +test("should create sections for packages", async () => { + let changed = 0; + + getLernaPackages.mockImplementation(async () => + Promise.resolve([ + { + path: "packages/@foobar/release", + name: "@foobar/release", + version: "1.0.0", + }, + { + path: "packages/@foobar/party", + name: "@foobar/party", + version: "1.0.0", + }, + ]) + ); + exec.mockImplementation((cmd: string) => { + if (!cmd.startsWith("git --no-pager show")) { + return; + } + + changed++; + + if (changed === 3) { + return ""; + } + + if (changed === 4) { + return "packages/@foobar/release/README.md"; + } + + if (changed === 2) { + return "packages/@foobar/release/README.md"; + } + + return "packages/@foobar/party/package.json"; + }); + + readFileSync.mockReturnValue("{}"); + + const plugin = new NpmPlugin(); + const hooks = makeHooks(); + const changelog = new Changelog(dummyLog(), { + owner: "andrew", + repo: "test", + baseUrl: "https://github.custom.com/", + labels: defaultLabels, + baseBranch: "master", + prereleaseBranches: ["next"], + }); + + plugin.apply({ + config: { prereleaseBranches: ["next"] }, + hooks, + logger: dummyLog(), + } as Auto.Auto); + hooks.onCreateChangelog.call(changelog, Auto.SEMVER.patch); + changelog.loadDefaultHooks(); + + const commits = await commitsPromise; + expect(await changelog.generateReleaseNotes(commits)).toMatchSnapshot(); +}); + test("should be able to disable sections for packages", async () => { let changed = 0; diff --git a/plugins/npm/src/index.ts b/plugins/npm/src/index.ts index ce6f10902..05cd2bdf4 100644 --- a/plugins/npm/src/index.ts +++ b/plugins/npm/src/index.ts @@ -735,6 +735,45 @@ export default class NPMPlugin implements IPlugin { return [commit, [`- ${section}`, ` ${line}`].join("\n")]; } ); + + changelog.hooks.sortChangelogLines.tap( + "NPM - Monorepo Grouping", + (lines) => { + if (!isMonorepo() || !this.monorepoChangelog) { + return lines; + } + + const lineMap: Record = {}; + + lines.forEach((line) => { + const monoRepoLine = line.split("\n"); + + if (monoRepoLine.length === 1) { + if (!lineMap.root) { + lineMap.root = []; + } + + lineMap.root.push(line); + } else { + const [packageName, change] = monoRepoLine; + + if (!lineMap[packageName]) { + lineMap[packageName] = []; + } + + lineMap[packageName].push(change); + } + }); + + return Object.entries(lineMap).map(([packageName, changes]) => { + if (packageName === "root") { + return changes.join("\n"); + } + + return [packageName, ...changes].join("\n"); + }); + } + ); } );