diff --git a/lib/util/string-match.spec.ts b/lib/util/string-match.spec.ts index 0321d4ce5720d6..4a6c7fbd2bdca2 100644 --- a/lib/util/string-match.spec.ts +++ b/lib/util/string-match.spec.ts @@ -89,5 +89,23 @@ describe('util/string-match', () => { it('returns false if negative pattern is matched', () => { expect(matchRegexOrGlob('test', '!/te/')).toBeFalse(); }); + + it('returns true for wildcard is massaged', () => { + expect( + matchRegexOrGlob('test', '^*$', { massagePattern: true }), + ).toBeTrue(); + }); + + it('returns true for glob wildcard is massaged', () => { + expect( + matchRegexOrGlob('test', '*', { massagePattern: true }), + ).toBeTrue(); + }); + + it('returns true for massaged regex pattern is massaged', () => { + expect( + matchRegexOrGlob('test', '.*', { massagePattern: true }), + ).toBeTrue(); + }); }); }); diff --git a/lib/util/string-match.ts b/lib/util/string-match.ts index 241debfbd17d0d..1349e292d0004e 100644 --- a/lib/util/string-match.ts +++ b/lib/util/string-match.ts @@ -4,6 +4,10 @@ import { regEx } from './regex'; export type StringMatchPredicate = (s: string) => boolean; +export interface matchRegexOrGlobOptions { + massagePattern?: boolean; +} + export function isDockerDigest(input: string): boolean { return /^sha256:[a-f0-9]{64}$/i.test(input); } @@ -18,7 +22,15 @@ export function getRegexOrGlobPredicate(pattern: string): StringMatchPredicate { return (x: string): boolean => mm.match(x); } -export function matchRegexOrGlob(input: string, pattern: string): boolean { +export function matchRegexOrGlob( + input: string, + rawPattern: string, + options?: matchRegexOrGlobOptions, +): boolean { + const pattern = options?.massagePattern + ? massagePattern(rawPattern) + : rawPattern; + const predicate = getRegexOrGlobPredicate(pattern); return predicate(input); } @@ -26,6 +38,7 @@ export function matchRegexOrGlob(input: string, pattern: string): boolean { export function matchRegexOrGlobList( input: string, patterns: string[], + options?: matchRegexOrGlobOptions, ): boolean { if (!patterns.length) { return false; @@ -37,7 +50,9 @@ export function matchRegexOrGlobList( ); if ( positivePatterns.length && - !positivePatterns.some((pattern) => matchRegexOrGlob(input, pattern)) + !positivePatterns.some((pattern) => + matchRegexOrGlob(input, pattern, options), + ) ) { return false; } @@ -48,7 +63,9 @@ export function matchRegexOrGlobList( ); if ( negativePatterns.length && - !negativePatterns.every((pattern) => matchRegexOrGlob(input, pattern)) + !negativePatterns.every((pattern) => + matchRegexOrGlob(input, pattern, options), + ) ) { return false; } @@ -94,3 +111,7 @@ export function getRegexPredicate(input: string): StringMatchPredicate | null { } return null; } + +function massagePattern(pattern: string): string { + return pattern === '^*$' || pattern === '*' ? '**' : `/${pattern}/`; +}