Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support patterns for speculative branch execution [WIP] #78

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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