Skip to content

Commit

Permalink
refactor: Move irrelevant functions from the regex helper (#27100)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Feb 6, 2024
1 parent c777450 commit de05ef0
Show file tree
Hide file tree
Showing 17 changed files with 118 additions and 119 deletions.
8 changes: 6 additions & 2 deletions lib/config/validation.ts
Expand Up @@ -8,8 +8,12 @@ import type {
} from '../modules/manager/custom/regex/types';
import type { CustomManager } from '../modules/manager/custom/types';
import type { HostRule } from '../types/host-rules';
import { configRegexPredicate, isConfigRegex, regEx } from '../util/regex';
import { anyMatchRegexOrMinimatch } from '../util/string';
import { regEx } from '../util/regex';
import {
anyMatchRegexOrMinimatch,
configRegexPredicate,
isConfigRegex,
} from '../util/string-match';
import * as template from '../util/template';
import {
hasValidSchedule,
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/datasource/docker/index.ts
Expand Up @@ -8,7 +8,7 @@ import type { HttpResponse } from '../../../util/http/types';
import { hasKey } from '../../../util/object';
import { regEx } from '../../../util/regex';
import { type AsyncResult, Result } from '../../../util/result';
import { isDockerDigest } from '../../../util/string';
import { isDockerDigest } from '../../../util/string-match';
import {
ensurePathPrefix,
joinUrlParts,
Expand Down
8 changes: 6 additions & 2 deletions lib/modules/platform/bitbucket/index.ts
Expand Up @@ -10,7 +10,7 @@ import { BitbucketHttp, setBaseUrl } from '../../../util/http/bitbucket';
import type { HttpOptions } from '../../../util/http/types';
import { regEx } from '../../../util/regex';
import { sanitize } from '../../../util/sanitize';
import { isUUID } from '../../../util/string';
import { UUIDRegex } from '../../../util/string-match';
import type {
BranchStatusConfig,
CreatePRConfig,
Expand Down Expand Up @@ -701,7 +701,11 @@ export async function addReviewers(
const body = {
title,
reviewers: reviewers.map((username: string) => {
const key = isUUID(username) ? 'uuid' : 'username';
const isUUID =
username.startsWith('{') &&
username.endsWith('}') &&
UUIDRegex.test(username.slice(1, -1));
const key = isUUID ? 'uuid' : 'username';
return {
[key]: username,
};
Expand Down
2 changes: 1 addition & 1 deletion 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';
import { anyMatchRegexOrMinimatch } from '../string-match';
import { parseUrl } from '../url';
import { dnsLookup } from './dns';
import { keepAliveAgents } from './keep-alive';
Expand Down
2 changes: 1 addition & 1 deletion 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 '../regex';
import { configRegexPredicate } from '../string-match';
import { Matcher } from './base';

export class BaseBranchesMatcher extends Matcher {
Expand Down
2 changes: 1 addition & 1 deletion 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 '../regex';
import { configRegexPredicate } from '../string-match';
import { Matcher } from './base';

export class CurrentValueMatcher extends Matcher {
Expand Down
2 changes: 1 addition & 1 deletion 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 '../regex';
import { configRegexPredicate } from '../string-match';
import { Matcher } from './base';

export class CurrentVersionMatcher extends Matcher {
Expand Down
2 changes: 1 addition & 1 deletion 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';
import { anyMatchRegexOrMinimatch } from '../string-match';
import { Matcher } from './base';

export class RepositoriesMatcher extends Matcher {
Expand Down
24 changes: 1 addition & 23 deletions lib/util/regex.spec.ts
@@ -1,6 +1,6 @@
import RE2 from 're2';
import { CONFIG_VALIDATION } from '../constants/error-messages';
import { configRegexPredicate, regEx } from './regex';
import { regEx } from './regex';

describe('util/regex', () => {
beforeEach(() => {
Expand Down Expand Up @@ -37,26 +37,4 @@ describe('util/regex', () => {
const regex = await import('./regex');
expect(regex.regEx('foo')).toBeInstanceOf(RegExp);
});

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

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

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

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

it('does not allow non-regex input', () => {
expect(configRegexPredicate('hello')).toBeNull();
});
});
});
39 changes: 0 additions & 39 deletions lib/util/regex.ts
Expand Up @@ -72,42 +72,3 @@ export function escapeRegExp(input: string): string {
}

export const newlineRegex = regEx(/\r?\n/);

const configValStart = regEx(/^!?\//);
const configValEnd = regEx(/\/i?$/);

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

function parseConfigRegex(input: string): RegExp | null {
try {
const regexString = input
.replace(configValStart, '')
.replace(configValEnd, '');
return input.endsWith('i') ? regEx(regexString, 'i') : regEx(regexString);
} catch (err) {
// no-op
}
return null;
}

type ConfigRegexPredicate = (s: string) => boolean;

export function configRegexPredicate(
input: string,
): ConfigRegexPredicate | null {
if (isConfigRegex(input)) {
const configRegex = parseConfigRegex(input);
if (configRegex) {
const isPositive = !input.startsWith('!');
return (x: string): boolean => {
const res = configRegex.test(x);
return isPositive ? res : !res;
};
}
}
return null;
}
25 changes: 25 additions & 0 deletions lib/util/string-match.spec.ts
@@ -0,0 +1,25 @@
import { configRegexPredicate } from './string-match';

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

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

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

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

it('does not allow non-regex input', () => {
expect(configRegexPredicate('hello')).toBeNull();
});
});
});
70 changes: 70 additions & 0 deletions lib/util/string-match.ts
@@ -0,0 +1,70 @@
import is from '@sindresorhus/is';
import { minimatch } from './minimatch';
import { regEx } from './regex';

export function isDockerDigest(input: string): boolean {
return /^sha256:[a-f0-9]{64}$/i.test(input);
}

export function matchRegexOrMinimatch(input: string, pattern: string): boolean {
if (pattern.length > 2 && pattern.startsWith('/') && pattern.endsWith('/')) {
try {
const regex = regEx(pattern.slice(1, -1));
return regex.test(input);
} catch (err) {
return false;
}
}

return minimatch(pattern, { dot: true }).match(input);
}

export function anyMatchRegexOrMinimatch(
input: string,
patterns: string[],
): boolean | null {
return patterns.some((pattern) => matchRegexOrMinimatch(input, pattern));
}

export const UUIDRegex = regEx(
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
);

const configValStart = regEx(/^!?\//);
const configValEnd = regEx(/\/i?$/);

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

function parseConfigRegex(input: string): RegExp | null {
try {
const regexString = input
.replace(configValStart, '')
.replace(configValEnd, '');
return input.endsWith('i') ? regEx(regexString, 'i') : regEx(regexString);
} catch (err) {
// no-op
}
return null;
}

type ConfigRegexPredicate = (s: string) => boolean;

export function configRegexPredicate(
input: string,
): ConfigRegexPredicate | null {
if (isConfigRegex(input)) {
const configRegex = parseConfigRegex(input);
if (configRegex) {
const isPositive = !input.startsWith('!');
return (x: string): boolean => {
const res = configRegex.test(x);
return isPositive ? res : !res;
};
}
}
return null;
}
10 changes: 1 addition & 9 deletions lib/util/string.spec.ts
@@ -1,4 +1,4 @@
import { coerceString, isUUID, looseEquals, replaceAt } from './string';
import { coerceString, looseEquals, replaceAt } from './string';

describe('util/string', () => {
describe('replaceAt', () => {
Expand Down Expand Up @@ -40,12 +40,4 @@ describe('util/string', () => {
expect(coerceString(null)).toBe('');
expect(coerceString(null, 'foo')).toBe('foo');
});

describe('isUUID', () => {
it('proper checks valid and invalid UUID strings', () => {
expect(isUUID('{90b6646d-1724-4a64-9fd9-539515fe94e9}')).toBe(true);
expect(isUUID('{90B6646D-1724-4A64-9FD9-539515FE94E9}')).toBe(true);
expect(isUUID('not-a-uuid')).toBe(false);
});
});
});
35 changes: 0 additions & 35 deletions lib/util/string.ts
@@ -1,6 +1,3 @@
import { minimatch } from './minimatch';
import { regEx } from './regex';

// Return true if the match string is found at index in content
export function matchAt(
content: string,
Expand Down Expand Up @@ -56,10 +53,6 @@ export function looseEquals(
return a.localeCompare(b, undefined, { sensitivity: 'base' }) === 0;
}

export function isDockerDigest(input: string): boolean {
return /^sha256:[a-f0-9]{64}$/i.test(input);
}

export function titleCase(input: string): string {
const words = input.toLowerCase().split(' ');

Expand Down Expand Up @@ -97,31 +90,3 @@ export function coerceString(
): string {
return val ?? def ?? '';
}

export function matchRegexOrMinimatch(input: string, pattern: string): boolean {
if (pattern.length > 2 && pattern.startsWith('/') && pattern.endsWith('/')) {
try {
const regex = regEx(pattern.slice(1, -1));
return regex.test(input);
} catch (err) {
return false;
}
}

return minimatch(pattern, { dot: true }).match(input);
}

export function anyMatchRegexOrMinimatch(
input: string,
patterns: string[],
): boolean | null {
return patterns.some((pattern) => matchRegexOrMinimatch(input, pattern));
}

const UUIDRegex = regEx(
/^\{[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\}$/i,
);

export function isUUID(input: string): boolean {
return UUIDRegex.test(input);
}
2 changes: 1 addition & 1 deletion 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/regex';
import { configRegexPredicate, isConfigRegex } from '../../util/string-match';

// istanbul ignore next
function repoName(value: string | { repository: string }): string {
Expand Down
2 changes: 1 addition & 1 deletion lib/workers/repository/process/index.ts
Expand Up @@ -11,8 +11,8 @@ import { scm } from '../../../modules/platform/scm';
import { getCache } from '../../../util/cache/repository';
import { clone } from '../../../util/clone';
import { getBranchList } from '../../../util/git';
import { configRegexPredicate } from '../../../util/regex';
import { addSplit } from '../../../util/split';
import { configRegexPredicate } from '../../../util/string-match';
import type { BranchConfig } from '../../types';
import { readDashboardBody } from '../dependency-dashboard';
import { ExtractResult, extract, lookup, update } from './extract-update';
Expand Down
2 changes: 1 addition & 1 deletion lib/workers/repository/process/lookup/filter.ts
Expand Up @@ -6,7 +6,7 @@ import type { VersioningApi } from '../../../../modules/versioning';
import * as npmVersioning from '../../../../modules/versioning/npm';
import * as pep440 from '../../../../modules/versioning/pep440';
import * as poetryVersioning from '../../../../modules/versioning/poetry';
import { configRegexPredicate } from '../../../../util/regex';
import { configRegexPredicate } from '../../../../util/string-match';
import type { FilterConfig } from './types';

export function filterVersions(
Expand Down

0 comments on commit de05ef0

Please sign in to comment.