Skip to content

Commit

Permalink
Fix issue with similarly named packgaes
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed May 22, 2024
1 parent 7e13349 commit c534154
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
27 changes: 27 additions & 0 deletions src/changelog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,33 @@ describe("Changelog", () => {
}
});

describe("packageFromPath with similarly named packages", () => {
const MockedChangelog = require("./changelog").default;

const TESTS = [
["", ""],
["/ember-fastboot", "ember-fastboot"],
["/ember-fastboot-2-fast-2-furious", "ember-fastboot-2-fast-2-furious"],
["/ember-fastboot-2-fast-2-furious/package.json", "ember-fastboot-2-fast-2-furious"],
["/ember-fastboot-tokyo-drift", "ember-fastboot-tokyo-drift"],
["/ember-fastboot-tokyo-drift/package.json", "ember-fastboot-tokyo-drift"],
];

for (let [input, expected] of TESTS) {
it(`${input} -> ${expected}`, () => {
const changelog = new MockedChangelog({
rootPath: "/",
packages: [
{ name: "ember-fastboot", path: "/ember-fastboot" },
{ name: "ember-fastboot-2-fast-2-furious", path: "/ember-fastboot-2-fast-2-furious" },
{ name: "ember-fastboot-tokyo-drift", path: "/ember-fastboot-tokyo-drift" },
],
});
expect(changelog.packageFromPath(input)).toEqual(expected);
});
}
});

describe("getCommitInfos", () => {
beforeEach(() => {
require("./fetch").__resetMockResponses();
Expand Down
25 changes: 20 additions & 5 deletions src/changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ export default class Changelog {
}

private async getListOfUniquePackages(sha: string): Promise<string[]> {
return (await Git.changedPaths(sha))
let changedPaths = await Git.changedPaths(sha);

return changedPaths
.map(path => this.packageFromPath(path))
.filter(Boolean)
.filter(onlyUnique);
Expand All @@ -84,13 +86,26 @@ export default class Changelog {
// use the discovered packages
const absolutePath = resolve(this.config.rootPath, path);

const foundPackage = this.config.packages.find(p => absolutePath.startsWith(p.path));
// Sometimes multiple packages may exist with the same prefix:
// ember-fastboot
// ember-fastboot-2-fast-2-furious
//
// in this case, we can't short circuit with length === 1, but we can do a longer match
const packageCandidates = this.config.packages.filter(p => absolutePath.startsWith(p.path));

if (packageCandidates.length === 1) {
return packageCandidates[0].name;
}

if (foundPackage) {
return foundPackage.name;
let longestMatch = "";

for (let pkg of packageCandidates) {
if (pkg.name.length > longestMatch.length) {
longestMatch = pkg.name;
}
}

return "";
return longestMatch; // may be empty if 0 candidates exist
} else {
// if we did not find any packages then default to
const parts = path.split("/");
Expand Down

0 comments on commit c534154

Please sign in to comment.