Skip to content

Commit

Permalink
feat: Support patterns for speculative branch execution
Browse files Browse the repository at this point in the history
  • Loading branch information
carmanchris31 committed Aug 31, 2020
1 parent ba52ad8 commit b9d741b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
23 changes: 23 additions & 0 deletions packages/client/src/speculativeBranchSelection.test.ts
Original file line number Diff line number Diff line change
@@ -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 match branch pattern", () => {
const branches = ["master", "/v\d+-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")
});
})
})
10 changes: 8 additions & 2 deletions packages/client/src/speculativeBranchSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,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 currentBranchInOrder = speculativeBranchesInOrder.findIndex((branch) => {
if (branch.startsWith("/")) {
return new RegExp(branch).test(currentBranchName);
}

return branch === currentBranchName;
});

return speculativeBranchesInOrder[currentBranchInOrder + 1];
}
Expand Down

0 comments on commit b9d741b

Please sign in to comment.