From d094afeb823b4bcd8817317c7e4e1edb47a71626 Mon Sep 17 00:00:00 2001 From: RahulGautamSingh Date: Fri, 13 Dec 2024 01:42:59 +0530 Subject: refactor(manager/custom): reorganize and update utility functions (#33050) --- docs/usage/configuration-options.md | 1 + lib/modules/manager/custom/regex/index.ts | 2 +- lib/modules/manager/custom/regex/strategies.ts | 29 ++--------- lib/modules/manager/custom/regex/utils.ts | 32 +----------- lib/modules/manager/custom/utils.ts | 57 ++++++++++++++++++++++ lib/modules/manager/devcontainer/extract.ts | 2 +- .../extract/extract-fingerprint-config.ts | 2 +- .../repository/update/branch/auto-replace.spec.ts | 7 ++- 8 files changed, 73 insertions(+), 59 deletions(-) create mode 100644 lib/modules/manager/custom/utils.ts diff --git a/docs/usage/configuration-options.md b/docs/usage/configuration-options.md index 0ce57dc796c..c6205f400ec 100644 --- a/docs/usage/configuration-options.md +++ b/docs/usage/configuration-options.md @@ -769,6 +769,7 @@ Example: "customManagers": [ { "customType": "regex", + "fileMatch": ["values.yaml$"], "matchStrings": [ "ENV .*?_VERSION=(?.*) # (?.*?)/(?.*?)\\s" ] diff --git a/lib/modules/manager/custom/regex/index.ts b/lib/modules/manager/custom/regex/index.ts index 00d9d79b8af..bbd4268369b 100644 --- a/lib/modules/manager/custom/regex/index.ts +++ b/lib/modules/manager/custom/regex/index.ts @@ -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']; diff --git a/lib/modules/manager/custom/regex/strategies.ts b/lib/modules/manager/custom/regex/strategies.ts index 2e489e43bf0..6ff7218bb99 100644 --- a/lib/modules/manager/custom/regex/strategies.ts +++ b/lib/modules/manager/custom/regex/strategies.ts @@ -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, @@ -32,7 +31,7 @@ export function handleAny( ) .filter(is.truthy) .filter((dep: PackageDependency) => - checkIsValidDependency(dep, packageFile), + checkIsValidDependency(dep, packageFile, 'regex'), ); } @@ -61,7 +60,7 @@ export function handleCombination( return [createDependency(extraction, config)] .filter(is.truthy) .filter((dep: PackageDependency) => - checkIsValidDependency(dep, packageFile), + checkIsValidDependency(dep, packageFile, 'regex'), ); } @@ -84,7 +83,7 @@ export function handleRecursive( }) .filter(is.truthy) .filter((dep: PackageDependency) => - checkIsValidDependency(dep, packageFile), + checkIsValidDependency(dep, packageFile, 'regex'), ); } @@ -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; -} diff --git a/lib/modules/manager/custom/regex/utils.ts b/lib/modules/manager/custom/regex/utils.ts index 350f639d9da..3714b1f42de 100644 --- a/lib/modules/manager/custom/regex/utils.ts +++ b/lib/modules/manager/custom/regex/utils.ts @@ -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, @@ -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)) - ); -} diff --git a/lib/modules/manager/custom/utils.ts b/lib/modules/manager/custom/utils.ts new file mode 100644 index 00000000000..024ac0d078d --- /dev/null +++ b/lib/modules/manager/custom/utils.ts @@ -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; +} diff --git a/lib/modules/manager/devcontainer/extract.ts b/lib/modules/manager/devcontainer/extract.ts index 489563507a8..8e96f1eeca4 100644 --- a/lib/modules/manager/devcontainer/extract.ts +++ b/lib/modules/manager/devcontainer/extract.ts @@ -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, diff --git a/lib/workers/repository/extract/extract-fingerprint-config.ts b/lib/workers/repository/extract/extract-fingerprint-config.ts index 8d1ad76c143..efeb296b0de 100644 --- a/lib/workers/repository/extract/extract-fingerprint-config.ts +++ b/lib/workers/repository/extract/extract-fingerprint-config.ts @@ -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 { diff --git a/lib/workers/repository/update/branch/auto-replace.spec.ts b/lib/workers/repository/update/branch/auto-replace.spec.ts index ee0b2c8e8ab..fb06f7bc23b 100644 --- a/lib/workers/repository/update/branch/auto-replace.spec.ts +++ b/lib/workers/repository/update/branch/auto-replace.spec.ts @@ -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 @@ -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'; @@ -252,6 +253,7 @@ describe('workers/repository/update/branch/auto-replace', () => { upgrade.matchStrings = [ 'image:\\s*\\\'?\\"?(?[^:]+):(?[^\\s\\\'\\"]+)\\\'?\\"?\\s*', ]; + upgrade.datasourceTemplate = 'docker'; const res = doAutoReplace(upgrade, yml, reuseExistingBranch); await expect(res).rejects.toThrow(WORKER_FILE_UPDATE_FAILED); }); @@ -316,6 +318,7 @@ describe('workers/repository/update/branch/auto-replace', () => { upgrade.matchStrings = [ 'image:\\s*\\\'?\\"?(?[^:]+):(?[^\\s\\\'\\"]+)\\\'?\\"?\\s*', ]; + upgrade.datasourceTemplate = 'docker'; const res = await doAutoReplace(upgrade, yml, reuseExistingBranch); expect(res).toBe(yml); }); @@ -1190,6 +1193,7 @@ describe('workers/repository/update/branch/auto-replace', () => { upgrade.matchStrings = [ 'image:\\s*?\\\'?\\"?(?[^:\\\'\\"]+):(?[^@\\\'\\"]+)@?(?[^\\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"'); }); @@ -1213,6 +1217,7 @@ describe('workers/repository/update/branch/auto-replace', () => { upgrade.matchStrings = [ 'image:\\s*[\\\'\\"]?(?[^:]+):(?[^@]+)?@?(?[^\\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"', -- cgit v1.2.3