Skip to content
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

fix: nested packages detection #294

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"chalk": "^2.4.1",
"cli-highlight": "^2.1.4",
"execa": "^1.0.0",
"globby": "^11.0.0",
"hosted-git-info": "^3.0.4",
"make-fetch-happen": "^7.1.1",
"p-map": "^3.0.0",
Expand Down Expand Up @@ -99,4 +100,4 @@
"singleQuote": false,
"trailingComma": "es5"
}
}
}
2 changes: 1 addition & 1 deletion src/__mocks__/changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Configuration } from "../configuration";
const Changelog = require.requireActual("../changelog").default;

const defaultConfig = {
rootPath: "../",
rootPath: "/path/to/project",
repo: "lerna/lerna-changelog",
labels: {
"Type: New Feature": ":rocket: New Feature",
Expand Down
22 changes: 1 addition & 21 deletions src/changelog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,9 @@ jest.mock("../src/changelog");
jest.mock("../src/github-api");
jest.mock("./git");
jest.mock("./fetch");
jest.mock("./packages");

describe("Changelog", () => {
describe("packageFromPath", () => {
const MockedChangelog = require("./changelog").default;

const TESTS = [
["", ""],
["foo.js", ""],
["packages/foo.js", ""],
["packages/foo/bar.js", "foo"],
["packages/foo/bar/baz.js", "foo"],
["packages/@foo/bar.js", "@foo"],
["packages/@foo/bar/baz.js", "@foo/bar"],
];

for (let [input, expected] of TESTS) {
it(`${input} -> ${expected}`, () => {
const changelog = new MockedChangelog();
expect(changelog.packageFromPath(input)).toEqual(expected);
});
}
});

describe("getCommitInfos", () => {
beforeEach(() => {
require("./fetch").__resetMockResponses();
Expand Down
29 changes: 13 additions & 16 deletions src/changelog.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const path = require("path");
const pMap = require("p-map");

import progressBar from "./progress-bar";
Expand All @@ -6,6 +7,7 @@ import findPullRequestId from "./find-pull-request-id";
import * as Git from "./git";
import GithubAPI, { GitHubUserResponse } from "./github-api";
import { CommitInfo, Release } from "./interfaces";
import { getPackages, PackageInfo } from "./packages";
import MarkdownRenderer from "./markdown-renderer";

const UNRELEASED_TAG = "___unreleased___";
Expand Down Expand Up @@ -71,26 +73,20 @@ export default class Changelog {
return releases;
}

private async getListOfUniquePackages(sha: string): Promise<string[]> {
private async getListOfUniquePackages(sha: string, workspacePackages: PackageInfo[]): Promise<string[]> {
return (await Git.changedPaths(sha))
.map(path => this.packageFromPath(path))
.map(filePath => {
const fullFilePath = path.join(this.config.rootPath, filePath);
const matchingPackageFromPath = workspacePackages.find(packageInfo => {
return fullFilePath.startsWith(packageInfo.location);
});
if (matchingPackageFromPath) return matchingPackageFromPath.name;
return "";
Comment on lines +79 to +84
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the new logic to get the package name.

})
.filter(Boolean)
.filter(onlyUnique);
}

private packageFromPath(path: string): string {
const parts = path.split("/");
if (parts[0] !== "packages" || parts.length < 3) {
return "";
}

if (parts.length >= 4 && parts[1][0] === "@") {
return `${parts[1]}/${parts[2]}`;
}

return parts[1];
}

private getListOfCommits(from: string, to: string): Git.CommitListItem[] {
// Determine the tags range to get the commits for. Custom from/to can be
// provided via command-line options.
Expand Down Expand Up @@ -214,12 +210,13 @@ export default class Changelog {

private async fillInPackages(commits: CommitInfo[]) {
progressBar.init("Mapping commits to packages…", commits.length);
const workspacePackages = await getPackages(this.config);

try {
await pMap(
commits,
async (commit: CommitInfo) => {
commit.packages = await this.getListOfUniquePackages(commit.commitSHA);
commit.packages = await this.getListOfUniquePackages(commit.commitSHA, workspacePackages);

progressBar.tick();
},
Expand Down
5 changes: 2 additions & 3 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const execa = require("execa");
const hostedGitInfo = require("hosted-git-info");

import ConfigurationError from "./configuration-error";
import { getRootPath } from "./git";

export interface Configuration {
repo: string;
Expand All @@ -20,9 +21,7 @@ export interface ConfigLoaderOptions {
}

export function load(options: ConfigLoaderOptions = {}): Configuration {
let cwd = process.cwd();
let rootPath = execa.sync("git", ["rev-parse", "--show-toplevel"], { cwd }).stdout;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really related to this PR, but I figured the git command belongs to the git file.


let rootPath = getRootPath();
return fromPath(rootPath, options);
}

Expand Down
4 changes: 2 additions & 2 deletions src/functional/__snapshots__/markdown-empty.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`createMarkdown multiple tags outputs correct changelog 1`] = `""`;

exports[`createMarkdown single project outputs correct changelog 1`] = `""`;

exports[`createMarkdown single tags outputs correct changelog 1`] = `""`;

exports[`multiple tags outputs correct changelog 1`] = `""`;
24 changes: 12 additions & 12 deletions src/functional/__snapshots__/markdown-full.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exports[`createMarkdown multiple tags outputs correct changelog 1`] = `
## [email protected] (1977-05-25)

#### :rocket: New Feature
* \`a-new-hope\`
* \`@star-wars/a-new-hope\`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mock data contains scoped package names, hence these changes.

* [#1](https://github.com/lerna/lerna-changelog/pull/1) feat: May the force be with you ([@luke](https://github.com/luke))

#### Committers: 1
Expand All @@ -15,7 +15,7 @@ exports[`createMarkdown multiple tags outputs correct changelog 1`] = `
## [email protected] (1977-05-25)

#### :rocket: New Feature
* \`a-new-hope\`
* \`@star-wars/a-new-hope\`
* [#1](https://github.com/lerna/lerna-changelog/pull/1) feat: May the force be with you ([@luke](https://github.com/luke))

#### Committers: 1
Expand All @@ -25,7 +25,7 @@ exports[`createMarkdown multiple tags outputs correct changelog 1`] = `
## [email protected] (1977-05-25)

#### :rocket: New Feature
* \`a-new-hope\`
* \`@star-wars/a-new-hope\`
* [#1](https://github.com/lerna/lerna-changelog/pull/1) feat: May the force be with you ([@luke](https://github.com/luke))

#### Committers: 1
Expand Down Expand Up @@ -93,11 +93,11 @@ exports[`createMarkdown single tags outputs correct changelog 1`] = `
## Unreleased (2099-01-01)

#### :rocket: New Feature
* \`the-force-awakens\`, \`rogue-one\`
* \`@star-wars/the-force-awakens\`, \`@star-wars/rogue-one\`
* [#7](https://github.com/lerna/lerna-changelog/pull/7) feat: that is not how the Force works! ([@han-solo](https://github.com/han-solo))

#### :nail_care: Enhancement
* \`the-force-awakens\`, \`rogue-one\`
* \`@star-wars/the-force-awakens\`, \`@star-wars/rogue-one\`
* [#7](https://github.com/lerna/lerna-changelog/pull/7) feat: that is not how the Force works! ([@han-solo](https://github.com/han-solo))

#### Committers: 1
Expand All @@ -107,19 +107,19 @@ exports[`createMarkdown single tags outputs correct changelog 1`] = `
## v6.0.0 (1983-05-25)

#### :rocket: New Feature
* \`return-of-the-jedi\`
* \`@star-wars/return-of-the-jedi\`
* [#5](https://github.com/lerna/lerna-changelog/pull/5) feat: I am your father ([@vader](https://github.com/vader))

#### :bug: Bug Fix
* \`return-of-the-jedi\`
* \`@star-wars/return-of-the-jedi\`
* [#4](https://github.com/lerna/lerna-changelog/pull/4) fix: RRRAARRWHHGWWR ([@chewbacca](https://github.com/chewbacca))

#### :nail_care: Enhancement
* \`return-of-the-jedi\`
* \`@star-wars/return-of-the-jedi\`
* [#6](https://github.com/lerna/lerna-changelog/pull/6) refactor: he is my brother ([@princess-leia](https://github.com/princess-leia))

#### :house: Maintenance
* \`return-of-the-jedi\`
* \`@star-wars/return-of-the-jedi\`
* [#4](https://github.com/lerna/lerna-changelog/pull/4) fix: RRRAARRWHHGWWR ([@chewbacca](https://github.com/chewbacca))

#### Committers: 3
Expand All @@ -131,11 +131,11 @@ exports[`createMarkdown single tags outputs correct changelog 1`] = `
## v5.0.0 (1980-05-17)

#### :boom: Breaking Change
* \`empire-strikes-back\`
* \`@star-wars/empire-strikes-back\`
* [#2](https://github.com/lerna/lerna-changelog/pull/2) chore: Terminate her... immediately! ([@gtarkin](https://github.com/gtarkin))

#### :bug: Bug Fix
* \`empire-strikes-back\`
* \`@star-wars/empire-strikes-back\`
* [#3](https://github.com/lerna/lerna-changelog/pull/3) fix: Get me the rebels base! ([@vader](https://github.com/vader))

#### Committers: 2
Expand All @@ -146,7 +146,7 @@ exports[`createMarkdown single tags outputs correct changelog 1`] = `
## v4.0.0 (1977-05-25)

#### :rocket: New Feature
* \`a-new-hope\`
* \`@star-wars/a-new-hope\`
* [#1](https://github.com/lerna/lerna-changelog/pull/1) feat: May the force be with you ([@luke](https://github.com/luke))

#### Committers: 1
Expand Down
116 changes: 75 additions & 41 deletions src/functional/markdown-empty.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ jest.mock("../../src/changelog");
jest.mock("../../src/github-api");
jest.mock("../git");
jest.mock("../fetch");
jest.mock("../packages");

const listOfCommits: CommitListItem[] = [];

Expand All @@ -15,18 +16,21 @@ const listOfTags = ["v6.0.0", "v5.0.0", "v4.0.0", "v3.0.0", "v2.0.0", "v1.0.0",
const listOfPackagesForEachCommit: { [id: string]: string[] } = {
a0000001: ["packages/random/foo.js"],
a0000002: ["packages/random/package.json"],
a0000003: ["packages/a-new-hope/rebels.js"],
a0000004: ["packages/a-new-hope/package.json"],
a0000005: ["packages/empire-strikes-back/death-star.js"],
a0000006: ["packages/empire-strikes-back/death-star.js"],
a0000007: ["packages/empire-strikes-back/hoth.js"],
a0000008: ["packages/empire-strikes-back/hoth.js"],
a0000009: ["packages/empire-strikes-back/package.json"],
a0000010: ["packages/return-of-the-jedi/jabba-the-hutt.js"],
a0000011: ["packages/return-of-the-jedi/vader-luke.js"],
a0000012: ["packages/return-of-the-jedi/leia.js"],
a0000013: ["packages/return-of-the-jedi/package.json"],
a0000014: ["packages/the-force-awakens/mission.js", "packages/rogue-one/mission.js"],
a0000003: ["packages/star-wars/a-new-hope/rebels.js"],
a0000004: ["packages/star-wars/a-new-hope/package.json"],
a0000005: ["packages/star-wars/empire-strikes-back/death-star.js"],
a0000006: ["packages/star-wars/empire-strikes-back/death-star.js"],
a0000007: ["packages/star-wars/empire-strikes-back/hoth.js"],
a0000008: ["packages/star-wars/empire-strikes-back/hoth.js"],
a0000009: ["packages/star-wars/empire-strikes-back/package.json"],
a0000010: ["packages/star-wars/return-of-the-jedi/jabba-the-hutt.js"],
a0000011: ["packages/star-wars/return-of-the-jedi/vader-luke.js"],
a0000012: ["packages/star-wars/return-of-the-jedi/leia.js"],
a0000013: ["packages/star-wars/return-of-the-jedi/package.json"],
a0000014: [
"packages/star-wars/the-force-awakens/mission.js",
"packages/star-wars/origin-stories/rogue-one/mission.js",
],
Comment on lines +19 to +33
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to ensure that the "nested" package locations work.

a0000015: ["packages/untitled/script.md"],
};

Expand Down Expand Up @@ -106,45 +110,75 @@ const issuesCache = {
},
},
};

describe("multiple tags", () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved inside the describe("createMarkdown", for consistency

it("outputs correct changelog", async () => {
require("../git").changedPaths.mockImplementation((sha: string) => listOfPackagesForEachCommit[sha]);
require("../git").lastTag.mockImplementation(() => "v8.0.0");
require("../git").listCommits.mockImplementation(() => listOfCommits);
require("../git").listTagNames.mockImplementation(() => [
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
]);

require("../fetch").__setMockResponses({
...usersCache,
...issuesCache,
});

const MockedChangelog = require("../changelog").default;
const changelog = new MockedChangelog();

const markdown = await changelog.createMarkdown();

expect(markdown).toMatchSnapshot();
});
});
const listOfWorkspacePackages = [
{
name: "random",
location: "/path/to/project/packages/random",
},
{
name: "@star-wars/a-new-hope",
location: "/path/to/project/packages/star-wars/a-new-hope",
},
{
name: "@star-wars/empire-strikes-back",
location: "/path/to/project/packages/star-wars/empire-strikes-back",
},
{
name: "@star-wars/return-of-the-jedi",
location: "/path/to/project/packages/star-wars/return-of-the-jedi",
},
{
name: "@star-wars/the-force-awakens",
location: "/path/to/project/packages/star-wars/the-force-awakens",
},
{
name: "@star-wars/rogue-one",
location: "/path/to/project/packages/star-wars/origin-stories/rogue-one",
},
{
name: "@star-wars/solo",
location: "/path/to/project/packages/star-wars/origin-stories/solo",
},
];

describe("createMarkdown", () => {
beforeEach(() => {
require("../fetch").__resetMockResponses();
require("../packages").getPackages.mockImplementation(() => listOfWorkspacePackages);
});

afterEach(() => {
jest.resetAllMocks();
});

describe("multiple tags", () => {
it("outputs correct changelog", async () => {
require("../git").changedPaths.mockImplementation((sha: string) => listOfPackagesForEachCommit[sha]);
require("../git").lastTag.mockImplementation(() => "v8.0.0");
require("../git").listCommits.mockImplementation(() => listOfCommits);
require("../git").listTagNames.mockImplementation(() => [
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
]);

require("../fetch").__setMockResponses({
...usersCache,
...issuesCache,
});

const MockedChangelog = require("../changelog").default;
const changelog = new MockedChangelog();

const markdown = await changelog.createMarkdown();

expect(markdown).toMatchSnapshot();
});
});
describe("single tags", () => {
it("outputs correct changelog", async () => {
require("../git").changedPaths.mockImplementation((sha: string) => listOfPackagesForEachCommit[sha]);
Expand Down
Loading