diff --git a/packages/client/src/speculativeBranchSelection.test.ts b/packages/client/src/speculativeBranchSelection.test.ts new file mode 100644 index 0000000..d41afd7 --- /dev/null +++ b/packages/client/src/speculativeBranchSelection.test.ts @@ -0,0 +1,23 @@ +import { findSpeculativeBaseBranch } from "./speculativeBranchSelection"; + +describe("speculativeBranchSelection", () => { + describe("findSpeculativeBaseBranch", () => { + it("should return first matching branch", () => { + const branches = ["master", "develop", "foobar"]; + + expect(findSpeculativeBaseBranch("develop", branches)).toBe("develop"); + }); + + it("should support matching branch with glob pattern", () => { + const branches = ["master", "v*-maintenance"]; + + expect(findSpeculativeBaseBranch("v1-maintenance", branches)).toBe("v1-maintenance"); + }); + + it("should return first configured branch by default", () => { + const branches = ["master", "develop"]; + + expect(findSpeculativeBaseBranch("someBranch", branches)).toBe("master"); + }); + }); +}); diff --git a/packages/client/src/speculativeBranchSelection.ts b/packages/client/src/speculativeBranchSelection.ts index 431df58..f32520d 100644 --- a/packages/client/src/speculativeBranchSelection.ts +++ b/packages/client/src/speculativeBranchSelection.ts @@ -2,6 +2,7 @@ import { PrInfo, FileStatuses } from "./api"; import { CodeChecksSettings } from "./types"; import { logger } from "./logger"; import execa = require("execa"); +import glob = require("glob"); const diffParser = require("./js/diff-parser/diff-parser.js").DiffParser; @@ -66,13 +67,17 @@ async function getBaseCommit(repoPath: string, speculativeBranchesInOrder: strin /** * Finds speculative branch based on the current branch name. If current branch is not on the list at all, select first. If it is, take the next one. If it's the last one just return undefined. */ -function findSpeculativeBaseBranch( +export function findSpeculativeBaseBranch( currentBranchName: string, speculativeBranchesInOrder: string[], ): string | undefined { - const currentBranchInOrder = speculativeBranchesInOrder.indexOf(currentBranchName); + const match = speculativeBranchesInOrder.find(pattern => { + const regex = new glob.Glob(pattern).minimatch.makeRe(); - return speculativeBranchesInOrder[currentBranchInOrder + 1]; + return regex.test(currentBranchName); + }); + + return match !== undefined ? currentBranchName : speculativeBranchesInOrder[0]; } async function getFileStatuses(repo: string, baseCommit: string, headCommit: string): Promise {