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 Sep 1, 2020
1 parent ba52ad8 commit 6a67e16
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 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 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");
});
});
});
11 changes: 8 additions & 3 deletions packages/client/src/speculativeBranchSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

0 comments on commit 6a67e16

Please sign in to comment.