From f2b2513f41be201d9158a7eede74d3f4294362a5 Mon Sep 17 00:00:00 2001 From: Josh Dales Date: Sat, 28 Jan 2023 15:34:31 -0500 Subject: [PATCH] Update the matching to use a regexp rather than minimatch --- __tests__/fixtures/branches.yml | 8 +++---- src/labeler.ts | 38 ++++++++++++++++----------------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/__tests__/fixtures/branches.yml b/__tests__/fixtures/branches.yml index 57369f42..266ef961 100644 --- a/__tests__/fixtures/branches.yml +++ b/__tests__/fixtures/branches.yml @@ -1,11 +1,11 @@ test-branch: - - branch: "test/**" + - branch: "^test/" feature-branch: - - branch: "*/feature/*" + - branch: "/feature/" bug-branch: - - branch: "{bug,fix}/*" + - branch: "^bug/|fix/" array-branch: - - branch: ["array/*"] + - branch: ["^array/"] diff --git a/src/labeler.ts b/src/labeler.ts index 6f882ca5..9b3c31b3 100644 --- a/src/labeler.ts +++ b/src/labeler.ts @@ -6,7 +6,7 @@ import {Minimatch, IMinimatch} from 'minimatch'; interface MatchConfig { all?: string[]; any?: string[]; - branch?: string | string[]; + branch?: string[]; } type StringOrMatchConfig = string | MatchConfig; @@ -156,6 +156,11 @@ function toMatchConfig(config: StringOrMatchConfig): MatchConfig { return { any: [config] }; + } else if (typeof config.branch === 'string') { + return { + ...config, + branch: [config.branch] + }; } return config; @@ -223,18 +228,18 @@ function checkAll(changedFiles: string[], globs: string[]): boolean { return true; } -function matchBranchPattern(matcher: IMinimatch, branchName: string): boolean { - core.debug(` - ${printPattern(matcher)}`); - if (matcher.match(branchName)) { +function matchBranchPattern(matcher: RegExp, branchName: string): boolean { + core.debug(` - ${matcher}`); + if (matcher.test(branchName)) { core.debug(` "branch" pattern matched`); return true; } - core.debug(` ${printPattern(matcher)} did not match`); + core.debug(` ${matcher} did not match`); return false; } -function checkBranch(glob: string | string[]): boolean { +function checkBranch(glob: string[]): boolean { const branchName = getHeadBranchName(); if (!branchName) { core.debug(` no branch name`); @@ -242,21 +247,16 @@ function checkBranch(glob: string | string[]): boolean { } core.debug(` checking "branch" pattern against ${branchName}`); - if (Array.isArray(glob)) { - const matchers = glob.map(g => new Minimatch(g)); - for (const matcher of matchers) { - if (matchBranchPattern(matcher, branchName)) { - core.debug(` "branch" patterns matched against ${branchName}`); - return true; - } + const matchers = glob.map(g => new RegExp(g)); + for (const matcher of matchers) { + if (matchBranchPattern(matcher, branchName)) { + core.debug(` "branch" patterns matched against ${branchName}`); + return true; } - - core.debug(` "branch" patterns did not match against ${branchName}`); - return false; - } else { - const matcher = new Minimatch(glob); - return matchBranchPattern(matcher, branchName); } + + core.debug(` "branch" patterns did not match against ${branchName}`); + return false; } function checkMatch(changedFiles: string[], matchConfig: MatchConfig): boolean {