Skip to content

Commit

Permalink
refactor: string match function naming (#27392)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Feb 18, 2024
1 parent 971f9c1 commit 9edde47
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 75 deletions.
18 changes: 9 additions & 9 deletions lib/config/validation.ts
Expand Up @@ -10,9 +10,9 @@ import type { CustomManager } from '../modules/manager/custom/types';
import type { HostRule } from '../types/host-rules';
import { regEx } from '../util/regex';
import {
anyMatchRegexOrMinimatch,
configRegexPredicate,
isConfigRegex,
getRegexPredicate,
isRegexMatch,
matchRegexOrGlobList,
} from '../util/string-match';
import * as template from '../util/template';
import {
Expand Down Expand Up @@ -255,9 +255,9 @@ export async function validateConfig(
}
} else if (
['allowedVersions', 'matchCurrentVersion'].includes(key) &&
isConfigRegex(val)
isRegexMatch(val)
) {
if (!configRegexPredicate(val)) {
if (!getRegexPredicate(val)) {
errors.push({
topic: 'Configuration Error',
message: `Invalid regExp for ${currentPath}: \`${val}\``,
Expand All @@ -266,7 +266,7 @@ export async function validateConfig(
} else if (
key === 'matchCurrentValue' &&
is.string(val) &&
!configRegexPredicate(val)
!getRegexPredicate(val)
) {
errors.push({
topic: 'Configuration Error',
Expand Down Expand Up @@ -552,8 +552,8 @@ export async function validateConfig(
if (key === 'baseBranches') {
for (const baseBranch of val as string[]) {
if (
isConfigRegex(baseBranch) &&
!configRegexPredicate(baseBranch)
isRegexMatch(baseBranch) &&
!getRegexPredicate(baseBranch)
) {
errors.push({
topic: 'Configuration Error',
Expand Down Expand Up @@ -724,7 +724,7 @@ export async function validateConfig(
message: `Invalid hostRules headers value configuration: header must be a string.`,
});
}
if (!anyMatchRegexOrMinimatch(header, allowedHeaders)) {
if (!matchRegexOrGlobList(header, allowedHeaders)) {
errors.push({
topic: 'Configuration Error',
message: `hostRules header \`${header}\` is not allowed by this bot's \`allowedHeaders\`.`,
Expand Down
4 changes: 2 additions & 2 deletions lib/logger/remap.ts
@@ -1,7 +1,7 @@
import type { LogLevelString } from 'bunyan';
import {
StringMatchPredicate,
makeRegexOrMinimatchPredicate,
getRegexOrGlobPredicate,
} from '../util/string-match';
import type { LogLevelRemap } from './types';

Expand All @@ -14,7 +14,7 @@ function match(remap: LogLevelRemap, input: string): boolean {
const { matchMessage: pattern } = remap;
let matchFn = matcherCache.get(remap);
if (!matchFn) {
matchFn = makeRegexOrMinimatchPredicate(pattern);
matchFn = getRegexOrGlobPredicate(pattern);
matcherCache.set(remap, matchFn);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/util/http/host-rules.ts
Expand Up @@ -10,7 +10,7 @@ import { logger } from '../../logger';
import { hasProxy } from '../../proxy';
import type { HostRule } from '../../types';
import * as hostRules from '../host-rules';
import { anyMatchRegexOrMinimatch } from '../string-match';
import { matchRegexOrGlobList } from '../string-match';
import { parseUrl } from '../url';
import { dnsLookup } from './dns';
import { keepAliveAgents } from './keep-alive';
Expand Down Expand Up @@ -169,7 +169,7 @@ export function applyHostRule<GotOptions extends HostRulesGotOptions>(
const filteredHeaders: Record<string, string> = {};

for (const [header, value] of Object.entries(hostRule.headers)) {
if (anyMatchRegexOrMinimatch(header, allowedHeaders)) {
if (matchRegexOrGlobList(header, allowedHeaders)) {
filteredHeaders[header] = value;
} else {
logger.once.error(
Expand Down
4 changes: 2 additions & 2 deletions lib/util/package-rules/base-branches.ts
@@ -1,6 +1,6 @@
import is from '@sindresorhus/is';
import type { PackageRule, PackageRuleInputConfig } from '../../config/types';
import { configRegexPredicate } from '../string-match';
import { getRegexPredicate } from '../string-match';
import { Matcher } from './base';

export class BaseBranchesMatcher extends Matcher {
Expand All @@ -17,7 +17,7 @@ export class BaseBranchesMatcher extends Matcher {
}

return matchBaseBranches.some((matchBaseBranch): boolean => {
const isAllowedPred = configRegexPredicate(matchBaseBranch);
const isAllowedPred = getRegexPredicate(matchBaseBranch);
if (isAllowedPred) {
return isAllowedPred(baseBranch);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/util/package-rules/current-value.ts
@@ -1,7 +1,7 @@
import is from '@sindresorhus/is';
import type { PackageRule, PackageRuleInputConfig } from '../../config/types';
import { logger } from '../../logger';
import { configRegexPredicate } from '../string-match';
import { getRegexPredicate } from '../string-match';
import { Matcher } from './base';

export class CurrentValueMatcher extends Matcher {
Expand All @@ -12,7 +12,7 @@ export class CurrentValueMatcher extends Matcher {
if (is.undefined(matchCurrentValue)) {
return null;
}
const matchCurrentValuePred = configRegexPredicate(matchCurrentValue);
const matchCurrentValuePred = getRegexPredicate(matchCurrentValue);

if (!matchCurrentValuePred) {
logger.debug(
Expand Down
6 changes: 2 additions & 4 deletions lib/util/package-rules/current-version.ts
Expand Up @@ -2,7 +2,7 @@ import is from '@sindresorhus/is';
import type { PackageRule, PackageRuleInputConfig } from '../../config/types';
import { logger } from '../../logger';
import * as allVersioning from '../../modules/versioning';
import { configRegexPredicate } from '../string-match';
import { getRegexPredicate } from '../string-match';
import { Matcher } from './base';

export class CurrentVersionMatcher extends Matcher {
Expand All @@ -22,9 +22,7 @@ export class CurrentVersionMatcher extends Matcher {
!!lockedVersion && is.nullOrUndefined(currentValue);
const version = allVersioning.get(versioning);
const matchCurrentVersionStr = matchCurrentVersion.toString();
const matchCurrentVersionPred = configRegexPredicate(
matchCurrentVersionStr,
);
const matchCurrentVersionPred = getRegexPredicate(matchCurrentVersionStr);

if (matchCurrentVersionPred) {
const compareVersion = lockedVersion ?? currentVersion ?? currentValue;
Expand Down
6 changes: 3 additions & 3 deletions lib/util/package-rules/repositories.ts
@@ -1,6 +1,6 @@
import is from '@sindresorhus/is';
import type { PackageRule, PackageRuleInputConfig } from '../../config/types';
import { anyMatchRegexOrMinimatch } from '../string-match';
import { matchRegexOrGlobList } from '../string-match';
import { Matcher } from './base';

export class RepositoriesMatcher extends Matcher {
Expand All @@ -16,7 +16,7 @@ export class RepositoriesMatcher extends Matcher {
return false;
}

return anyMatchRegexOrMinimatch(repository, matchRepositories);
return matchRegexOrGlobList(repository, matchRepositories);
}

override excludes(
Expand All @@ -31,6 +31,6 @@ export class RepositoriesMatcher extends Matcher {
return false;
}

return anyMatchRegexOrMinimatch(repository, excludeRepositories);
return matchRegexOrGlobList(repository, excludeRepositories);
}
}
52 changes: 24 additions & 28 deletions lib/util/string-match.spec.ts
@@ -1,93 +1,89 @@
import {
anyMatchRegexOrMinimatch,
configRegexPredicate,
matchRegexOrMinimatch,
getRegexPredicate,
matchRegexOrGlob,
matchRegexOrGlobList,
} from './string-match';

describe('util/string-match', () => {
describe('anyMatchRegexOrMinimatch()', () => {
describe('matchRegexOrGlobList()', () => {
it('returns false if empty patterns', () => {
expect(anyMatchRegexOrMinimatch('test', [])).toBeFalse();
expect(matchRegexOrGlobList('test', [])).toBeFalse();
});

it('returns false if no match', () => {
expect(anyMatchRegexOrMinimatch('test', ['/test2/'])).toBeFalse();
expect(matchRegexOrGlobList('test', ['/test2/'])).toBeFalse();
});

it('returns true if any match', () => {
expect(anyMatchRegexOrMinimatch('test', ['test', '/test2/'])).toBeTrue();
expect(matchRegexOrGlobList('test', ['test', '/test2/'])).toBeTrue();
});

it('returns true if one match with negative patterns', () => {
expect(anyMatchRegexOrMinimatch('test', ['!/test2/'])).toBeTrue();
expect(matchRegexOrGlobList('test', ['!/test2/'])).toBeTrue();
});

it('returns true if every match with negative patterns', () => {
expect(
anyMatchRegexOrMinimatch('test', ['!/test2/', '!/test3/']),
).toBeTrue();
expect(matchRegexOrGlobList('test', ['!/test2/', '!/test3/'])).toBeTrue();
});

it('returns true if matching positive and negative patterns', () => {
expect(anyMatchRegexOrMinimatch('test', ['test', '!/test3/'])).toBeTrue();
expect(matchRegexOrGlobList('test', ['test', '!/test3/'])).toBeTrue();
});

it('returns true if matching every negative pattern (regex)', () => {
expect(
anyMatchRegexOrMinimatch('test', ['test', '!/test3/', '!/test4/']),
matchRegexOrGlobList('test', ['test', '!/test3/', '!/test4/']),
).toBeTrue();
});

it('returns false if not matching every negative pattern (regex)', () => {
expect(
anyMatchRegexOrMinimatch('test', ['!/test3/', '!/test/']),
).toBeFalse();
expect(matchRegexOrGlobList('test', ['!/test3/', '!/test/'])).toBeFalse();
});

it('returns true if matching every negative pattern (glob)', () => {
expect(
anyMatchRegexOrMinimatch('test', ['test', '!test3', '!test4']),
matchRegexOrGlobList('test', ['test', '!test3', '!test4']),
).toBeTrue();
});

it('returns false if not matching every negative pattern (glob)', () => {
expect(anyMatchRegexOrMinimatch('test', ['!test3', '!te*'])).toBeFalse();
expect(matchRegexOrGlobList('test', ['!test3', '!te*'])).toBeFalse();
});
});

describe('configRegexPredicate', () => {
describe('getRegexPredicate()', () => {
it('allows valid regex pattern', () => {
expect(configRegexPredicate('/hello/')).not.toBeNull();
expect(getRegexPredicate('/hello/')).not.toBeNull();
});

it('invalidates invalid regex pattern', () => {
expect(configRegexPredicate('/^test\\d+$/m')).toBeNull();
expect(getRegexPredicate('/^test\\d+$/m')).toBeNull();
});

it('allows the i flag in regex pattern', () => {
expect(configRegexPredicate('/^test\\d+$/i')).not.toBeNull();
expect(getRegexPredicate('/^test\\d+$/i')).not.toBeNull();
});

it('allows negative regex pattern', () => {
expect(configRegexPredicate('!/^test\\d+$/i')).not.toBeNull();
expect(getRegexPredicate('!/^test\\d+$/i')).not.toBeNull();
});

it('does not allow non-regex input', () => {
expect(configRegexPredicate('hello')).toBeNull();
expect(getRegexPredicate('hello')).toBeNull();
});
});

describe('matchRegexOrMinimatch()', () => {
describe('matchRegexOrGlob()', () => {
it('returns true if positive regex pattern matched', () => {
expect(matchRegexOrMinimatch('test', '/test/')).toBeTrue();
expect(matchRegexOrGlob('test', '/test/')).toBeTrue();
});

it('returns true if negative regex is not matched', () => {
expect(matchRegexOrMinimatch('test', '!/test3/')).toBeTrue();
expect(matchRegexOrGlob('test', '!/test3/')).toBeTrue();
});

it('returns false if negative pattern is matched', () => {
expect(matchRegexOrMinimatch('test', '!/te/')).toBeFalse();
expect(matchRegexOrGlob('test', '!/te/')).toBeFalse();
});
});
});
28 changes: 12 additions & 16 deletions lib/util/string-match.ts
Expand Up @@ -8,10 +8,8 @@ export function isDockerDigest(input: string): boolean {
return /^sha256:[a-f0-9]{64}$/i.test(input);
}

export function makeRegexOrMinimatchPredicate(
pattern: string,
): StringMatchPredicate {
const regExPredicate = configRegexPredicate(pattern);
export function getRegexOrGlobPredicate(pattern: string): StringMatchPredicate {
const regExPredicate = getRegexPredicate(pattern);
if (regExPredicate) {
return regExPredicate;
}
Expand All @@ -20,12 +18,12 @@ export function makeRegexOrMinimatchPredicate(
return (x: string): boolean => mm.match(x);
}

export function matchRegexOrMinimatch(input: string, pattern: string): boolean {
const predicate = makeRegexOrMinimatchPredicate(pattern);
export function matchRegexOrGlob(input: string, pattern: string): boolean {
const predicate = getRegexOrGlobPredicate(pattern);
return predicate(input);
}

export function anyMatchRegexOrMinimatch(
export function matchRegexOrGlobList(
input: string,
patterns: string[],
): boolean {
Expand All @@ -39,7 +37,7 @@ export function anyMatchRegexOrMinimatch(
);
if (
positivePatterns.length &&
!positivePatterns.some((pattern) => matchRegexOrMinimatch(input, pattern))
!positivePatterns.some((pattern) => matchRegexOrGlob(input, pattern))
) {
return false;
}
Expand All @@ -50,7 +48,7 @@ export function anyMatchRegexOrMinimatch(
);
if (
negativePatterns.length &&
!negativePatterns.every((pattern) => matchRegexOrMinimatch(input, pattern))
!negativePatterns.every((pattern) => matchRegexOrGlob(input, pattern))
) {
return false;
}
Expand All @@ -65,13 +63,13 @@ export const UUIDRegex = regEx(
const configValStart = regEx(/^!?\//);
const configValEnd = regEx(/\/i?$/);

export function isConfigRegex(input: unknown): input is string {
export function isRegexMatch(input: unknown): input is string {
return (
is.string(input) && configValStart.test(input) && configValEnd.test(input)
);
}

function parseConfigRegex(input: string): RegExp | null {
function parseRegexMatch(input: string): RegExp | null {
try {
const regexString = input
.replace(configValStart, '')
Expand All @@ -83,11 +81,9 @@ function parseConfigRegex(input: string): RegExp | null {
return null;
}

export function configRegexPredicate(
input: string,
): StringMatchPredicate | null {
if (isConfigRegex(input)) {
const configRegex = parseConfigRegex(input);
export function getRegexPredicate(input: string): StringMatchPredicate | null {
if (isRegexMatch(input)) {
const configRegex = parseRegexMatch(input);
if (configRegex) {
const isPositive = !input.startsWith('!');
return (x: string): boolean => {
Expand Down
6 changes: 3 additions & 3 deletions lib/workers/global/autodiscover.ts
Expand Up @@ -3,7 +3,7 @@ import type { AllConfig } from '../../config/types';
import { logger } from '../../logger';
import { platform } from '../../modules/platform';
import { minimatchFilter } from '../../util/minimatch';
import { configRegexPredicate, isConfigRegex } from '../../util/string-match';
import { getRegexPredicate, isRegexMatch } from '../../util/string-match';

// istanbul ignore next
function repoName(value: string | { repository: string }): string {
Expand Down Expand Up @@ -100,8 +100,8 @@ export function applyFilters(repos: string[], filters: string[]): string[] {

for (const filter of filters) {
let res: string[];
if (isConfigRegex(filter)) {
const autodiscoveryPred = configRegexPredicate(filter);
if (isRegexMatch(filter)) {
const autodiscoveryPred = getRegexPredicate(filter);
if (!autodiscoveryPred) {
throw new Error(`Failed to parse regex pattern "${filter}"`);
}
Expand Down

0 comments on commit 9edde47

Please sign in to comment.