From 1179c9b97f38b5c3009e21192aadea68c8ffff08 Mon Sep 17 00:00:00 2001 From: Andrew Lisowski Date: Fri, 12 Mar 2021 16:33:56 -0800 Subject: [PATCH] handle baseBranch not on remote --- packages/core/src/__tests__/next.test.ts | 106 +++++++++++++++++++++++ packages/core/src/auto.ts | 10 ++- packages/core/src/git.ts | 9 +- 3 files changed, 118 insertions(+), 7 deletions(-) create mode 100644 packages/core/src/__tests__/next.test.ts diff --git a/packages/core/src/__tests__/next.test.ts b/packages/core/src/__tests__/next.test.ts new file mode 100644 index 000000000..8b8152afa --- /dev/null +++ b/packages/core/src/__tests__/next.test.ts @@ -0,0 +1,106 @@ +import { Auto } from "../auto"; +import { dummyLog } from "../utils/logger"; +import makeCommitFromMsg from "./make-commit-from-msg"; +import execPromise from "../utils/exec-promise"; + +const exec = jest.fn(); +jest.mock("../utils/exec-promise"); +// @ts-ignore +execPromise.mockImplementation(exec); +exec.mockResolvedValue(""); + +jest.mock("../utils/git-reset.ts"); +jest.mock("../utils/load-plugins.ts"); +jest.mock("env-ci", () => () => ({ isCi: false, branch: "next" })); + +const defaults = { + baseBranch: "main", + owner: "foo", + repo: "bar", +}; + +jest.mock("@octokit/rest", () => { + const Octokit = class MockOctokit { + static plugin = () => Octokit; + + authenticate = () => undefined; + + search = { + issuesAndPullRequests: () => ({ data: { items: [] } }), + }; + + repos = { + get: jest.fn().mockReturnValue({}), + }; + + hook = { + error: () => undefined, + }; + + issues = { + listLabelsOnIssue: jest.fn().mockReturnValue({ data: [] }), + }; + + users = { + getAuthenticated: jest.fn().mockResolvedValue({}), + }; + }; + + return { Octokit }; +}); + +// @ts-ignore +jest.mock("gitlog", () => ({ + gitlogPromise: () => + Promise.resolve([ + { + rawBody: "foo", + hash: "123", + }, + { + rawBody: "foo", + hash: "456", + }, + ]), +})); + +test("falls back to local baseBranch if it doesn't exist on origin", async () => { + const auto = new Auto({ ...defaults, plugins: [] }); + + // @ts-ignore + auto.checkClean = () => Promise.resolve(true); + auto.logger = dummyLog(); + await auto.loadConfig(); + auto.remote = "origin"; + auto.git!.publish = () => Promise.resolve({ data: {} } as any); + auto.git!.getLastTagNotInBaseBranch = () => Promise.reject(new Error("Test")); + auto.git!.getLatestTagInBranch = () => Promise.reject(new Error("Test")); + auto.git!.getLatestRelease = () => Promise.resolve("abcd"); + auto.release!.generateReleaseNotes = () => Promise.resolve("notes"); + auto.release!.getCommitsInRelease = () => + Promise.resolve([ + makeCommitFromMsg("Test Commit", { labels: ["skip-release"] }), + ]); + + jest.spyOn(auto.release!, "getCommits").mockImplementation(); + const next = jest.fn(); + auto.hooks.next.tap("test", next); + + await auto.next({}); + expect(exec).toHaveBeenCalledWith("git", [ + "rev-list", + "--boundary", + "next...origin/main", + "--left-only", + ]); + + auto.git!.shaExists = () => Promise.resolve(false); + + await auto.next({}); + expect(exec).toHaveBeenCalledWith("git", [ + "rev-list", + "--boundary", + "next...main", + "--left-only", + ]); +}); diff --git a/packages/core/src/auto.ts b/packages/core/src/auto.ts index 66083f47a..94ae0919b 100644 --- a/packages/core/src/auto.ts +++ b/packages/core/src/auto.ts @@ -1362,11 +1362,15 @@ export default class Auto { ); const currentBranch = getCurrentBranch(); + const baseBranch = (await this.git.shaExists(`origin/${this.baseBranch}`)) + ? `origin/${this.baseBranch}` + : this.baseBranch; + const forkPoints = ( await execPromise("git", [ "rev-list", "--boundary", - `${currentBranch}...origin/${this.baseBranch}`, + `${currentBranch}...${baseBranch}`, "--left-only", ]) ) @@ -1384,9 +1388,7 @@ export default class Auto { const [, latestTagInBranch] = await on( this.git.getLatestTagInBranch(currentBranch) ); - const [, tagsInBaseBranch] = await on( - this.git.getTags(`origin/${this.baseBranch}`) - ); + const [, tagsInBaseBranch] = await on(this.git.getTags(baseBranch)); const [latestTagInBaseBranch] = (tagsInBaseBranch || []).reverse(); const lastTag = lastTagNotInBaseBranch || diff --git a/packages/core/src/git.ts b/packages/core/src/git.ts index 576befa88..cdc3a59bd 100644 --- a/packages/core/src/git.ts +++ b/packages/core/src/git.ts @@ -901,9 +901,12 @@ export default class Git { first?: boolean; } = {} ) { - const baseTags = ( - await this.getTags(`origin/${this.options.baseBranch}`) - ).reverse(); + const baseBranch = (await this.shaExists( + `origin/${this.options.baseBranch}` + )) + ? `origin/${this.options.baseBranch}` + : this.options.baseBranch; + const baseTags = (await this.getTags(baseBranch)).reverse(); let branchTags = (await this.getTags(`heads/${branch}`)).reverse(); const branchTagsWithPrereleaseSuffix = branchTags.filter( (tag) => tag.indexOf(`-${branch.toLowerCase()}`) >= 0