Skip to content

Commit

Permalink
feat: allow i flag in regex patterns (#26815)
Browse files Browse the repository at this point in the history
Co-authored-by: Rhys Arkins <rhys@arkins.net>
  • Loading branch information
RahulGautamSingh and rarkins committed Jan 23, 2024
1 parent 91ced24 commit 2949e13
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 9 deletions.
16 changes: 15 additions & 1 deletion lib/config/validation.spec.ts
Expand Up @@ -89,6 +89,10 @@ describe('config/validation', () => {
matchPackageNames: ['quack'],
allowedVersions: '!/***$}{]][/',
},
{
matchPackageNames: ['quack'],
allowedVersions: '/quaCk/i',
},
],
};
const { errors } = await configValidation.validateConfig(false, config);
Expand All @@ -114,6 +118,11 @@ describe('config/validation', () => {
matchCurrentValue: '<1.0.0',
enabled: true,
},
{
matchPackageNames: ['foo'],
matchCurrentValue: '/^2/i',
enabled: true,
},
],
};
const { errors } = await configValidation.validateConfig(false, config);
Expand Down Expand Up @@ -143,6 +152,11 @@ describe('config/validation', () => {
matchCurrentVersion: '!/***$}{]][/',
enabled: true,
},
{
matchPackageNames: ['foo'],
matchCurrentVersion: '/^2/i',
enabled: true,
},
],
};
const { errors } = await configValidation.validateConfig(false, config);
Expand Down Expand Up @@ -218,7 +232,7 @@ describe('config/validation', () => {

it('catches invalid baseBranches regex', async () => {
const config = {
baseBranches: ['/***$}{]][/'],
baseBranches: ['/***$}{]][/', '/branch/i'],
};
const { errors } = await configValidation.validateConfig(false, config);
expect(errors).toEqual([
Expand Down
12 changes: 12 additions & 0 deletions lib/util/package-rules/current-value.spec.ts
Expand Up @@ -28,6 +28,18 @@ describe('util/package-rules/current-value', () => {
expect(result).toBeFalse();
});

it('case insensitive match', () => {
const result = matcher.matches(
{
currentValue: '"V1.1.0"',
},
{
matchCurrentValue: '/^"v/i',
},
);
expect(result).toBeTrue();
});

it('return true for regex version match', () => {
const result = matcher.matches(
{
Expand Down
13 changes: 13 additions & 0 deletions lib/util/package-rules/current-version.spec.ts
Expand Up @@ -50,6 +50,19 @@ describe('util/package-rules/current-version', () => {
expect(result).toBeFalse();
});

it('case insensitive match', () => {
const result = matcher.matches(
{
versioning: 'pep440',
currentValue: 'bbbbbb',
},
{
matchCurrentVersion: '/BBB.*/i',
},
);
expect(result).toBeTrue();
});

it('return false for regex version non match', () => {
const result = matcher.matches(
{
Expand Down
24 changes: 23 additions & 1 deletion lib/util/regex.spec.ts
@@ -1,6 +1,6 @@
import RE2 from 're2';
import { CONFIG_VALIDATION } from '../constants/error-messages';
import { isUUID, regEx } from './regex';
import { configRegexPredicate, isUUID, regEx } from './regex';

describe('util/regex', () => {
beforeEach(() => {
Expand Down Expand Up @@ -45,4 +45,26 @@ describe('util/regex', () => {
expect(isUUID('not-a-uuid')).toBe(false);
});
});

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();
});
});
});
4 changes: 2 additions & 2 deletions lib/util/regex.ts
Expand Up @@ -65,7 +65,7 @@ export function escapeRegExp(input: string): string {
export const newlineRegex = regEx(/\r?\n/);

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

export function isConfigRegex(input: unknown): input is string {
return (
Expand All @@ -78,7 +78,7 @@ function parseConfigRegex(input: string): RegExp | null {
const regexString = input
.replace(configValStart, '')
.replace(configValEnd, '');
return regEx(regexString);
return input.endsWith('i') ? regEx(regexString, 'i') : regEx(regexString);
} catch (err) {
// no-op
}
Expand Down
2 changes: 1 addition & 1 deletion lib/workers/global/autodiscover.spec.ts
Expand Up @@ -107,7 +107,7 @@ describe('workers/global/autodiscover', () => {

it('filters autodiscovered github repos with regex', async () => {
config.autodiscover = true;
config.autodiscoverFilter = ['/project/re*./'];
config.autodiscoverFilter = ['/project/RE*./i'];
config.platform = 'github';
hostRules.find = jest.fn(() => ({
token: 'abc',
Expand Down
18 changes: 14 additions & 4 deletions lib/workers/repository/process/index.spec.ts
Expand Up @@ -126,26 +126,36 @@ describe('workers/repository/process/index', () => {

it('finds baseBranches via regular expressions', async () => {
extract.mockResolvedValue({} as never);
config.baseBranches = ['/^release\\/.*/', 'dev', '!/^pre-release\\/.*/'];
config.baseBranches = ['/^release\\/.*/i', 'dev', '!/^pre-release\\/.*/'];
git.getBranchList.mockReturnValue([
'dev',
'pre-release/v0',
'RELEASE/v0',
'release/v1',
'release/v2',
'some-other',
]);
scm.branchExists.mockResolvedValue(true);
const res = await extractDependencies(config);
expect(res).toStrictEqual({
branchList: [undefined, undefined, undefined, undefined],
branches: [undefined, undefined, undefined, undefined],
branchList: [undefined, undefined, undefined, undefined, undefined],
branches: [undefined, undefined, undefined, undefined, undefined],
packageFiles: undefined,
});

expect(logger.logger.debug).toHaveBeenCalledWith(
{ baseBranches: ['release/v1', 'release/v2', 'dev', 'some-other'] },
{
baseBranches: [
'RELEASE/v0',
'release/v1',
'release/v2',
'dev',
'some-other',
],
},
'baseBranches',
);
expect(addMeta).toHaveBeenCalledWith({ baseBranch: 'RELEASE/v0' });
expect(addMeta).toHaveBeenCalledWith({ baseBranch: 'release/v1' });
expect(addMeta).toHaveBeenCalledWith({ baseBranch: 'release/v2' });
expect(addMeta).toHaveBeenCalledWith({ baseBranch: 'dev' });
Expand Down

0 comments on commit 2949e13

Please sign in to comment.