Skip to content

Commit

Permalink
group monorepo changelog lines
Browse files Browse the repository at this point in the history
  • Loading branch information
hipstersmoothie committed Oct 14, 2020
1 parent 17b1056 commit 1e26187
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 6 deletions.
21 changes: 19 additions & 2 deletions plugins/npm/__tests__/__snapshots__/monorepo-log.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ exports[`should add versions for independent packages 1`] = `
- woot [#12343](https://github.custom.com/pull/12343) ([email protected])
- \`@foobar/[email protected]\`, \`@foobar/[email protected]\`
- [PLAYA-5052] - Some Feature [#12345](https://github.custom.com/pull/12345) ([email protected])
- \`@foobar/[email protected]\`, \`@foobar/[email protected]\`
- [PLAYA-5052] - Some Feature - Revert [#12345](https://github.custom.com/pull/12345) ([email protected])
#### 🏠 Internal
Expand Down Expand Up @@ -39,9 +38,27 @@ exports[`should create sections for packages 1`] = `
"#### 💥 Breaking Change
- woot [#12343](https://github.custom.com/pull/12343) ([email protected])
- \`@foobar/release\`, \`@foobar/party\`
- \`@foobar/party\`
- [PLAYA-5052] - Some Feature [#12345](https://github.custom.com/pull/12345) ([email protected])
- \`@foobar/release\`
- [PLAYA-5052] - Some Feature - Revert [#12345](https://github.custom.com/pull/12345) ([email protected])
#### 🏠 Internal
- \`@foobar/release\`
- Another Feature [#1234](https://github.custom.com/pull/1234) ([email protected])
#### Authors: 1
- Adam Dierkens ([email protected])"
`;

exports[`should group sections for packages 1`] = `
"#### 💥 Breaking Change
- woot [#12343](https://github.custom.com/pull/12343) ([email protected])
- \`@foobar/release\`, \`@foobar/party\`
- [PLAYA-5052] - Some Feature [#12345](https://github.custom.com/pull/12345) ([email protected])
- [PLAYA-5052] - Some Feature - Revert [#12345](https://github.custom.com/pull/12345) ([email protected])
#### 🏠 Internal
Expand Down
76 changes: 72 additions & 4 deletions plugins/npm/__tests__/monorepo-log.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 () =>
Expand Down Expand Up @@ -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;

Expand Down
39 changes: 39 additions & 0 deletions plugins/npm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string[]> = {};

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");
});
}
);
}
);

Expand Down

0 comments on commit 1e26187

Please sign in to comment.