From 8af14a78059787ba2d9a0083b7700b32013709bd Mon Sep 17 00:00:00 2001 From: Lucas Shadler Date: Wed, 17 Feb 2021 12:18:18 -0800 Subject: [PATCH 1/2] fix: prefer prerelease tags when determining latest --- .gitignore | 5 ++++- packages/core/src/__tests__/git.test.ts | 25 +++++++++++++++++++++++++ packages/core/src/git.ts | 22 +++++++++++++++++++--- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index f98432194..2f18ac5a8 100644 --- a/.gitignore +++ b/.gitignore @@ -214,4 +214,7 @@ GitHub.sublime-settings # Ignore all local history of files .history -# End of https://www.gitignore.io/api/node,intellij,sublimetext,visualstudiocode \ No newline at end of file +# End of https://www.gitignore.io/api/node,intellij,sublimetext,visualstudiocode + +### Files generated from testing ### +packages/cli/auto \ No newline at end of file diff --git a/packages/core/src/__tests__/git.test.ts b/packages/core/src/__tests__/git.test.ts index 2a1d7e7c8..60f462060 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 c14bd4ca7..43e96146f 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; From 46ea98fc4858d40cde03c3f296935754fcc7ff09 Mon Sep 17 00:00:00 2001 From: Lucas Shadler Date: Wed, 17 Feb 2021 13:53:06 -0800 Subject: [PATCH 2/2] chore: adjust after rebase --- packages/core/src/__tests__/git.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/__tests__/git.test.ts b/packages/core/src/__tests__/git.test.ts index 1d89e522b..604538986 100644 --- a/packages/core/src/__tests__/git.test.ts +++ b/packages/core/src/__tests__/git.test.ts @@ -171,7 +171,7 @@ describe("github", () => { const gh = new Git(options); gh.getTags = (ref: string) => { - if (ref === "origin/master") { + if (ref === "origin/main") { return Promise.resolve(["1.0.0", "1.2.3", "1.4.0"]); }