Skip to content

Commit

Permalink
fix: prefer prerelease tags when determining latest
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Shadler committed Feb 17, 2021
1 parent 0713aef commit 61266ae
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
25 changes: 25 additions & 0 deletions packages/core/src/__tests__/git.test.ts
Expand Up @@ -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 = [
Expand Down
22 changes: 19 additions & 3 deletions packages/core/src/git.ts
Expand Up @@ -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<string, unknown> | unknown[], origError: Error) {
constructor(
api: string,
args: Record<string, unknown> | unknown[],
origError: Error
) {
super(
`Error calling github: ${api}\n\twith: ${JSON.stringify(args)}.\n\t${
origError.message
Expand Down Expand Up @@ -219,7 +223,11 @@ export default class Git {

/** Get the first commit for the repo */
async getFirstCommit(): Promise<string> {
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;
}

Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 61266ae

Please sign in to comment.