Skip to content

Commit

Permalink
refactor(manager/custom): reorganize and update utility functions (#3…
Browse files Browse the repository at this point in the history
  • Loading branch information
RahulGautamSingh authored Dec 12, 2024
1 parent f78d778 commit d094afe
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 59 deletions.
1 change: 1 addition & 0 deletions docs/usage/configuration-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,7 @@ Example:
"customManagers": [
{
"customType": "regex",
"fileMatch": ["values.yaml$"],
"matchStrings": [
"ENV .*?_VERSION=(?<currentValue>.*) # (?<datasource>.*?)/(?<depName>.*?)\\s"
]
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/manager/custom/regex/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import type {
PackageDependency,
PackageFileContent,
} from '../../types';
import { validMatchFields } from '../utils';
import { handleAny, handleCombination, handleRecursive } from './strategies';
import type { RegexManagerConfig, RegexManagerTemplates } from './types';
import { validMatchFields } from './utils';

export const categories: Category[] = ['custom'];

Expand Down
29 changes: 4 additions & 25 deletions lib/modules/manager/custom/regex/strategies.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import is from '@sindresorhus/is';
import { logger } from '../../../../logger';
import { regEx } from '../../../../util/regex';
import type { PackageDependency } from '../../types';
import { checkIsValidDependency } from '../utils';
import type { RecursionParameter, RegexManagerConfig } from './types';
import {
createDependency,
isValidDependency,
mergeExtractionTemplate,
mergeGroups,
regexMatchAll,
Expand All @@ -32,7 +31,7 @@ export function handleAny(
)
.filter(is.truthy)
.filter((dep: PackageDependency) =>
checkIsValidDependency(dep, packageFile),
checkIsValidDependency(dep, packageFile, 'regex'),
);
}

Expand Down Expand Up @@ -61,7 +60,7 @@ export function handleCombination(
return [createDependency(extraction, config)]
.filter(is.truthy)
.filter((dep: PackageDependency) =>
checkIsValidDependency(dep, packageFile),
checkIsValidDependency(dep, packageFile, 'regex'),
);
}

Expand All @@ -84,7 +83,7 @@ export function handleRecursive(
})
.filter(is.truthy)
.filter((dep: PackageDependency) =>
checkIsValidDependency(dep, packageFile),
checkIsValidDependency(dep, packageFile, 'regex'),
);
}

Expand Down Expand Up @@ -116,23 +115,3 @@ function processRecursive(parameters: RecursionParameter): PackageDependency[] {
});
});
}

function checkIsValidDependency(
dep: PackageDependency,
packageFile: string,
): boolean {
const isValid = isValidDependency(dep);
if (!isValid) {
const meta = {
packageDependency: dep,
packageFile,
};
logger.trace(
meta,
'Discovered a package dependency by matching regex, but it did not pass validation. Discarding',
);
return isValid;
}

return isValid;
}
32 changes: 2 additions & 30 deletions lib/modules/manager/custom/regex/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,14 @@ import { migrateDatasource } from '../../../../config/migrations/custom/datasour
import { logger } from '../../../../logger';
import * as template from '../../../../util/template';
import type { PackageDependency } from '../../types';
import type { ValidMatchFields } from '../utils';
import { validMatchFields } from '../utils';
import type {
ExtractionTemplate,
RegexManagerConfig,
RegexManagerTemplates,
} from './types';

export const validMatchFields = [
'depName',
'packageName',
'currentValue',
'currentDigest',
'datasource',
'versioning',
'extractVersion',
'registryUrl',
'depType',
'indentation',
] as const;

type ValidMatchFields = (typeof validMatchFields)[number];

function updateDependency(
dependency: PackageDependency,
field: ValidMatchFields,
Expand Down Expand Up @@ -119,18 +106,3 @@ export function mergeExtractionTemplate(
replaceString: addition.replaceString ?? base.replaceString,
};
}

export function isValidDependency({
depName,
currentValue,
currentDigest,
packageName,
}: PackageDependency): boolean {
// check if all the fields are set
return (
(is.nonEmptyStringAndNotWhitespace(depName) ||
is.nonEmptyStringAndNotWhitespace(packageName)) &&
(is.nonEmptyStringAndNotWhitespace(currentDigest) ||
is.nonEmptyStringAndNotWhitespace(currentValue))
);
}
57 changes: 57 additions & 0 deletions lib/modules/manager/custom/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import is from '@sindresorhus/is';
import { logger } from '../../../logger';
import type { PackageDependency } from '../types';

export const validMatchFields = [
'depName',
'packageName',
'currentValue',
'currentDigest',
'datasource',
'versioning',
'extractVersion',
'registryUrl',
'depType',
'indentation',
] as const;

export type ValidMatchFields = (typeof validMatchFields)[number];

export function isValidDependency({
depName,
currentValue,
currentDigest,
packageName,
datasource,
}: PackageDependency): boolean {
// check if all the fields are set
return (
(is.nonEmptyStringAndNotWhitespace(depName) ||
is.nonEmptyStringAndNotWhitespace(packageName)) &&
(is.nonEmptyStringAndNotWhitespace(currentDigest) ||
is.nonEmptyStringAndNotWhitespace(currentValue)) &&
is.nonEmptyStringAndNotWhitespace(datasource)
);
}

export function checkIsValidDependency(
dep: PackageDependency,
packageFile: string,
manager: string,
): boolean {
const isValid = isValidDependency(dep);
if (!isValid) {
const meta = {
packageDependency: dep,
packageFile,
manager,
};
logger.trace(
meta,
'Discovered a package dependency, but it did not pass validation. Discarding',
);
return isValid;
}

return isValid;
}
2 changes: 1 addition & 1 deletion lib/modules/manager/devcontainer/extract.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { logger } from '../../../logger';
import { isValidDependency } from '../custom/regex/utils';
import { isValidDependency } from '../custom/utils';
import { getDep as getDockerDep } from '../dockerfile/extract';
import type {
ExtractConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type { RenovateConfig } from '../../../config/types';
import { getEnabledManagersList } from '../../../modules/manager';
import { isCustomManager } from '../../../modules/manager/custom';
import type { RegexManagerTemplates } from '../../../modules/manager/custom/regex/types';
import { validMatchFields } from '../../../modules/manager/custom/regex/utils';
import type { CustomExtractConfig } from '../../../modules/manager/custom/types';
import { validMatchFields } from '../../../modules/manager/custom/utils';
import type { WorkerExtractConfig } from '../../types';

export interface FingerprintExtractConfig {
Expand Down
7 changes: 6 additions & 1 deletion lib/workers/repository/update/branch/auto-replace.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ describe('workers/repository/update/branch/auto-replace', () => {
upgrade.packageFile = '.gitlab-ci.yml';
upgrade.autoReplaceStringTemplate =
"'{{{depName}}}'\nref: {{{newValue}}}";
upgrade.datasourceTemplate = 'docker';
upgrade.matchStringsStrategy = 'combination';

// If the new "name" is not added to the matchStrings, the regex matcher fails to extract from `newContent` as
Expand All @@ -237,7 +238,7 @@ describe('workers/repository/update/branch/auto-replace', () => {
);
});

it('fails with oldversion in depname', async () => {
it('fails with oldversion in depName', async () => {
const yml =
'image: "1111111111.dkr.ecr.us-east-1.amazonaws.com/my-repository:1"\n\n';
upgrade.manager = 'regex';
Expand All @@ -252,6 +253,7 @@ describe('workers/repository/update/branch/auto-replace', () => {
upgrade.matchStrings = [
'image:\\s*\\\'?\\"?(?<depName>[^:]+):(?<currentValue>[^\\s\\\'\\"]+)\\\'?\\"?\\s*',
];
upgrade.datasourceTemplate = 'docker';
const res = doAutoReplace(upgrade, yml, reuseExistingBranch);
await expect(res).rejects.toThrow(WORKER_FILE_UPDATE_FAILED);
});
Expand Down Expand Up @@ -316,6 +318,7 @@ describe('workers/repository/update/branch/auto-replace', () => {
upgrade.matchStrings = [
'image:\\s*\\\'?\\"?(?<depName>[^:]+):(?<currentValue>[^\\s\\\'\\"]+)\\\'?\\"?\\s*',
];
upgrade.datasourceTemplate = 'docker';
const res = await doAutoReplace(upgrade, yml, reuseExistingBranch);
expect(res).toBe(yml);
});
Expand Down Expand Up @@ -1190,6 +1193,7 @@ describe('workers/repository/update/branch/auto-replace', () => {
upgrade.matchStrings = [
'image:\\s*?\\\'?\\"?(?<depName>[^:\\\'\\"]+):(?<currentValue>[^@\\\'\\"]+)@?(?<currentDigest>[^\\s\\\'\\"]+)?\\"?\\\'?\\s*',
];
upgrade.datasourceTemplate = 'docker';
const res = await doAutoReplace(upgrade, yml, reuseExistingBranch);
expect(res).toBe('image: "some.other.url.com/some-new-repo:3.16"');
});
Expand All @@ -1213,6 +1217,7 @@ describe('workers/repository/update/branch/auto-replace', () => {
upgrade.matchStrings = [
'image:\\s*[\\\'\\"]?(?<depName>[^:]+):(?<currentValue>[^@]+)?@?(?<currentDigest>[^\\s\\\'\\"]+)?[\\\'\\"]?\\s*',
];
upgrade.datasourceTemplate = 'docker';
const res = await doAutoReplace(upgrade, yml, reuseExistingBranch);
expect(res).toBe(
'image: "some.other.url.com/some-new-repo:3.16@sha256:p0o9i8u7z6t5r4e3w2q1"',
Expand Down

0 comments on commit d094afe

Please sign in to comment.