From 437bd9ba8229a5abd2a4a05e8a3a32e1c7a97dbf Mon Sep 17 00:00:00 2001 From: Jamie Tanna Date: Fri, 21 Jun 2024 09:04:49 +0100 Subject: [PATCH 1/3] chore(regex): add additional logging for `isValidDependency` failures When writing regexes to match custom file formats, it may not be obvious why a valid regex may not be resulting in a match. This is often because we have written a perfectly correct regex which has matched the line(s), but then our `isValidDependency` check fails. To make it easier to discover this, we can add a TRACE log, and note that we will discard the dependency, giving suitable context for what is being rejected. --- .../manager/custom/regex/strategies.ts | 47 +++++++++++++++++-- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/lib/modules/manager/custom/regex/strategies.ts b/lib/modules/manager/custom/regex/strategies.ts index b2b1a3f3f9f609..f75fc3403f5427 100644 --- a/lib/modules/manager/custom/regex/strategies.ts +++ b/lib/modules/manager/custom/regex/strategies.ts @@ -1,4 +1,5 @@ import is from '@sindresorhus/is'; +import { logger } from '../../../../logger'; import { regEx } from '../../../../util/regex'; import type { PackageDependency } from '../../types'; import type { RecursionParameter, RegexManagerConfig } from './types'; @@ -12,7 +13,7 @@ import { export function handleAny( content: string, - _packageFile: string, + packageFile: string, config: RegexManagerConfig, ): PackageDependency[] { return config.matchStrings @@ -30,12 +31,24 @@ export function handleAny( ), ) .filter(is.truthy) - .filter(isValidDependency); + .filter((dep: PackageDependency) => { + 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 + }); } export function handleCombination( content: string, - _packageFile: string, + packageFile: string, config: RegexManagerConfig, ): PackageDependency[] { const matches = config.matchStrings @@ -57,7 +70,19 @@ export function handleCombination( .reduce((base, addition) => mergeExtractionTemplate(base, addition)); return [createDependency(extraction, config)] .filter(is.truthy) - .filter(isValidDependency); + .filter((dep: PackageDependency) => { + 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 + }); } export function handleRecursive( @@ -78,7 +103,19 @@ export function handleRecursive( regexes, }) .filter(is.truthy) - .filter(isValidDependency); + .filter((dep: PackageDependency) => { + 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 + }); } function processRecursive(parameters: RecursionParameter): PackageDependency[] { From dd7d5b58285913486884b40f769510b0e3f22bc8 Mon Sep 17 00:00:00 2001 From: Jamie Tanna Date: Fri, 21 Jun 2024 09:38:50 +0100 Subject: [PATCH 2/3] fixup! chore(regex): add additional logging for `isValidDependency` failures --- .../manager/custom/regex/strategies.ts | 70 ++++++++----------- 1 file changed, 30 insertions(+), 40 deletions(-) diff --git a/lib/modules/manager/custom/regex/strategies.ts b/lib/modules/manager/custom/regex/strategies.ts index f75fc3403f5427..874f69e5d4535f 100644 --- a/lib/modules/manager/custom/regex/strategies.ts +++ b/lib/modules/manager/custom/regex/strategies.ts @@ -31,19 +31,9 @@ export function handleAny( ), ) .filter(is.truthy) - .filter((dep: PackageDependency) => { - 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 - }); + .filter((dep: PackageDependency) => + checkIsValidDependency(dep, packageFile), + ); } export function handleCombination( @@ -70,19 +60,9 @@ export function handleCombination( .reduce((base, addition) => mergeExtractionTemplate(base, addition)); return [createDependency(extraction, config)] .filter(is.truthy) - .filter((dep: PackageDependency) => { - 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 - }); + .filter((dep: PackageDependency) => + checkIsValidDependency(dep, packageFile), + ); } export function handleRecursive( @@ -103,19 +83,9 @@ export function handleRecursive( regexes, }) .filter(is.truthy) - .filter((dep: PackageDependency) => { - 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 - }); + .filter((dep: PackageDependency) => + checkIsValidDependency(dep, packageFile), + ); } function processRecursive(parameters: RecursionParameter): PackageDependency[] { @@ -135,7 +105,7 @@ function processRecursive(parameters: RecursionParameter): PackageDependency[] { }, config, ); - return result ? [result] : /* istanbul ignore next: can this happen? */ []; + return result ? [result] : /* istanbul ignore next: can this happen? */[]; } return regexMatchAll(regexes[index], content).flatMap((match) => { return processRecursive({ @@ -146,3 +116,23 @@ 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; +} From 95c9afc8e23b46cff28f91249ba0a92eff5aec10 Mon Sep 17 00:00:00 2001 From: Jamie Tanna Date: Fri, 21 Jun 2024 09:42:51 +0100 Subject: [PATCH 3/3] fixup! fixup! chore(regex): add additional logging for `isValidDependency` failures --- lib/modules/manager/custom/regex/strategies.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/modules/manager/custom/regex/strategies.ts b/lib/modules/manager/custom/regex/strategies.ts index 874f69e5d4535f..1424745417de0c 100644 --- a/lib/modules/manager/custom/regex/strategies.ts +++ b/lib/modules/manager/custom/regex/strategies.ts @@ -105,7 +105,7 @@ function processRecursive(parameters: RecursionParameter): PackageDependency[] { }, config, ); - return result ? [result] : /* istanbul ignore next: can this happen? */[]; + return result ? [result] : /* istanbul ignore next: can this happen? */ []; } return regexMatchAll(regexes[index], content).flatMap((match) => { return processRecursive({