Skip to content

Commit

Permalink
Just convert the pattern into a RegExp...
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Engledew committed Dec 1, 2020
1 parent 4d86261 commit 698e2a5
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 110 deletions.
66 changes: 22 additions & 44 deletions lib/actions-util.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/actions-util.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions lib/actions-util.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/actions-util.test.js.map

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions lib/init-action.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/init-action.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/actions-util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,9 @@ test("patternIsSuperset()", (t) => {
t.false(actionsutil.patternIsSuperset("feature-*", "**"));
t.false(actionsutil.patternIsSuperset("a/**/c", "a/**/d"));
t.false(actionsutil.patternIsSuperset("a/**/c", "a/**"));
t.true(actionsutil.patternIsSuperset("a/**", "a/**/c"));
t.true(actionsutil.patternIsSuperset("a/**/c", "a/main-**/c"));
t.false(actionsutil.patternIsSuperset("a/**/b/**/c", "a/**/d/**/c"));
t.false(actionsutil.patternIsSuperset("a/main-**/c", "a/**/c"));
t.true(
actionsutil.patternIsSuperset(
Expand Down
87 changes: 27 additions & 60 deletions src/actions-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,69 +134,36 @@ function isObject(o: unknown): o is object {
return o !== null && typeof o === "object";
}

const WORKSPACE_BRANCES_PATTERN = new RegExp("(\\*\\*?|/)");

function tokenize(value: string): string[] {
return value.split(WORKSPACE_BRANCES_PATTERN).reduce(function (arr, cur) {
if (cur) {
arr.push(cur);
}
return arr;
}, [] as ReturnType<typeof tokenize>);
}

function considerToken(
a: string,
b: string
): { advance: boolean; consume: boolean } {
switch (a) {
case "*":
return { advance: b === "/", consume: b !== "/" };
case "**":
return { advance: false, consume: true };
default:
return { advance: a === b, consume: a === b };
}
}

function tokensAreSuperset(tokensA: string[], tokensB: string[]) {
let indexA = 0;
let indexB = 0;

let advance;
let consume = true;

while (advance || consume) {
const currentA = tokensA[indexA];
const currentB = tokensB[indexB];

if (currentB === undefined) {
return true;
}
if (currentA === undefined) {
return false;
}

const next = considerToken(currentA, currentB);

advance = next.advance;
consume = next.consume;

if (consume) {
indexB += 1;
}
if (advance) {
indexA += 1;
}
}
return false;
const GLOB_PATTERN = new RegExp("(\\*\\*?)");

function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
}

function patternToRegExp(value) {
return new RegExp(
`^${value
.split(GLOB_PATTERN)
.reduce(function (arr, cur) {
if (cur) {
if (cur === "**") {
arr.push(".*?");
} else if (cur === "*") {
arr.push("[^/]*?");
} else {
arr.push(escapeRegExp(cur));
}
}
return arr;
}, [])
.join("")}$`
);
}

// this function should return true if patternA is a superset of patternB
// e.g: * is a superset of main-* but main-* is not a superset of *.
export function patternIsSuperset(patternA: string, patternB: string): boolean {
const tokensA = tokenize(patternA);
const tokensB = tokenize(patternB);

return tokensAreSuperset(tokensA, tokensB) && tokensAreSuperset(tokensA.reverse(), tokensB.reverse());
return patternToRegExp(patternA).test(patternB);
}

function branchesToArray(branches?: string | null | string[]): string[] | "**" {
Expand Down
4 changes: 3 additions & 1 deletion src/init-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ async function run() {
// we do not want to worry users if linting is failing
// but we do want to send a status report containing this error code
// below
const userWorkflowErrors = workflowErrors.filter(o => o.code !== 'LintFailed');
const userWorkflowErrors = workflowErrors.filter(
(o) => o.code !== "LintFailed"
);

if (userWorkflowErrors.length > 0) {
core.warning(actionsUtil.formatWorkflowErrors(userWorkflowErrors));
Expand Down

0 comments on commit 698e2a5

Please sign in to comment.