From c534154045f56fd193b496fcba9e0e0a1387a0fd Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Wed, 22 May 2024 10:52:14 -0400 Subject: [PATCH 1/3] Fix issue with similarly named packgaes --- src/changelog.spec.ts | 27 +++++++++++++++++++++++++++ src/changelog.ts | 25 ++++++++++++++++++++----- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/changelog.spec.ts b/src/changelog.spec.ts index 6f51c73..d7f7839 100644 --- a/src/changelog.spec.ts +++ b/src/changelog.spec.ts @@ -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(); diff --git a/src/changelog.ts b/src/changelog.ts index 45ad416..3c78619 100644 --- a/src/changelog.ts +++ b/src/changelog.ts @@ -73,7 +73,9 @@ export default class Changelog { } private async getListOfUniquePackages(sha: string): Promise { - return (await Git.changedPaths(sha)) + let changedPaths = await Git.changedPaths(sha); + + return changedPaths .map(path => this.packageFromPath(path)) .filter(Boolean) .filter(onlyUnique); @@ -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("/"); From 09555acd22c272ba85cdc1691940ccc52d0371ae Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Wed, 22 May 2024 11:33:03 -0400 Subject: [PATCH 2/3] Git doesn't believe in directories, which allows us to simplify things a bit --- src/changelog.spec.ts | 4 +--- src/changelog.ts | 18 +++++++----------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/src/changelog.spec.ts b/src/changelog.spec.ts index d7f7839..f62dc4a 100644 --- a/src/changelog.spec.ts +++ b/src/changelog.spec.ts @@ -59,10 +59,8 @@ describe("Changelog", () => { const TESTS = [ ["", ""], - ["/ember-fastboot", "ember-fastboot"], - ["/ember-fastboot-2-fast-2-furious", "ember-fastboot-2-fast-2-furious"], + ["/ember-fastboot/package.json", "ember-fastboot"], ["/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"], ]; diff --git a/src/changelog.ts b/src/changelog.ts index 3c78619..02c36ac 100644 --- a/src/changelog.ts +++ b/src/changelog.ts @@ -91,21 +91,17 @@ export default class Changelog { // 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)); + const foundPackage = this.config.packages.find(p => { + let withSlash = p.path.endsWith("/") ? p.path : `${p.path}/`; - if (packageCandidates.length === 1) { - return packageCandidates[0].name; - } - - let longestMatch = ""; + return absolutePath.startsWith(withSlash); + }); - for (let pkg of packageCandidates) { - if (pkg.name.length > longestMatch.length) { - longestMatch = pkg.name; - } + if (foundPackage) { + return foundPackage.name; } - return longestMatch; // may be empty if 0 candidates exist + return ""; } else { // if we did not find any packages then default to const parts = path.split("/"); From 33b9046fff25a250ac913dcd131687d38ccb62f8 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Wed, 22 May 2024 11:41:27 -0400 Subject: [PATCH 3/3] Use path.sep --- src/changelog.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/changelog.ts b/src/changelog.ts index 02c36ac..a540d85 100644 --- a/src/changelog.ts +++ b/src/changelog.ts @@ -1,5 +1,5 @@ const pMap = require("p-map"); -const { resolve } = require("path"); +const { resolve, sep } = require("path"); import progressBar from "./progress-bar"; import { Configuration } from "./configuration"; @@ -89,10 +89,8 @@ export default class Changelog { // 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 foundPackage = this.config.packages.find(p => { - let withSlash = p.path.endsWith("/") ? p.path : `${p.path}/`; + let withSlash = p.path.endsWith(sep) ? p.path : `${p.path}${sep}`; return absolutePath.startsWith(withSlash); });