From 6b87b615320047118b73c57bb76ead84f5c150ca Mon Sep 17 00:00:00 2001 From: Lucas Shadler Date: Wed, 17 Feb 2021 12:18:18 -0800 Subject: [PATCH] fix: prefer prerelease tags when determining latest --- packages/cli/auto | 0 packages/core/src/__tests__/git.test.ts | 25 +++++++++++++++++++++++++ packages/core/src/git.ts | 22 +++++++++++++++++++--- 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 packages/cli/auto diff --git a/packages/cli/auto b/packages/cli/auto new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/core/src/__tests__/git.test.ts b/packages/core/src/__tests__/git.test.ts index 2a1d7e7c81..60f462060a 100644 --- a/packages/core/src/__tests__/git.test.ts +++ b/packages/core/src/__tests__/git.test.ts @@ -167,6 +167,31 @@ describe("github", () => { ); }); + test("will prefer latest tags from prerelease branch if available", async () => { + const gh = new Git(options); + + gh.getTags = (ref: string) => { + if (ref === "origin/master") { + return Promise.resolve(["1.0.0", "1.2.3", "1.4.0"]); + } + + return Promise.resolve([ + "1.0.0", + "1.2.3", + "1.4.0", + "1.4.1-beta.0", + "1.4.1-beta.1", + "1.4.1-beta.2", + "1.4.1-alpha.0", + "1.4.1-alpha.1", + ]); + }; + + expect(await gh.getTagNotInBaseBranch("alpha")).toBe("1.4.1-alpha.1"); + + expect(await gh.getTagNotInBaseBranch("beta")).toBe("1.4.1-beta.2"); + }); + test("handles tags with package names", async () => { const baseTags = ["@monorepo/models@2.0.0", "@monorepo/core@2.0.0"]; const branchTags = [ diff --git a/packages/core/src/git.ts b/packages/core/src/git.ts index c14bd4ca79..43e96146f5 100644 --- a/packages/core/src/git.ts +++ b/packages/core/src/git.ts @@ -49,7 +49,11 @@ export interface IGitOptions { /** An error originating from the GitHub */ class GitAPIError extends Error { /** Extend the base error */ - constructor(api: string, args: Record | unknown[], origError: Error) { + constructor( + api: string, + args: Record | unknown[], + origError: Error + ) { super( `Error calling github: ${api}\n\twith: ${JSON.stringify(args)}.\n\t${ origError.message @@ -219,7 +223,11 @@ export default class Git { /** Get the first commit for the repo */ async getFirstCommit(): Promise { - const list = await execPromise("git", ["rev-list", "--max-parents=0", "HEAD"]); + const list = await execPromise("git", [ + "rev-list", + "--max-parents=0", + "HEAD", + ]); return list.split("\n").pop() as string; } @@ -885,7 +893,15 @@ export default class Git { const baseTags = ( await this.getTags(`origin/${this.options.baseBranch}`) ).reverse(); - const branchTags = (await this.getTags(`heads/${branch}`)).reverse(); + let branchTags = (await this.getTags(`heads/${branch}`)).reverse(); + const branchTagsWithPrereleaseSuffix = branchTags.filter( + (tag) => tag.indexOf(`-${branch.toLowerCase()}`) >= 0 + ); + + if (branchTagsWithPrereleaseSuffix.length) { + branchTags = branchTagsWithPrereleaseSuffix; + } + const comparator = options.first ? lt : gt; let firstGreatestUnique: string | undefined;