From 6a67e169c6d216b6c811b07eca538d2a7f9bbdea Mon Sep 17 00:00:00 2001 From: Christopher Carman Date: Mon, 31 Aug 2020 16:58:39 -0700 Subject: [PATCH] feat: Support patterns for speculative branch execution --- .../src/speculativeBranchSelection.test.ts | 23 +++++++++++++++++++ .../client/src/speculativeBranchSelection.ts | 11 ++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 packages/client/src/speculativeBranchSelection.test.ts 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 {