From 88c37f304852dc60b4b5f761b30bb7b44b502c98 Mon Sep 17 00:00:00 2001 From: Mart Roosmaa Date: Tue, 26 Nov 2024 12:48:17 +0100 Subject: [PATCH] Add support for additional query parameters in kustomize resource URLs. Kustomize allows the following query string parameter in it's remote resource URLs: - ref - version - timeout - submodules (refer to https://github.com/kubernetes-sigs/kustomize/blob/master/examples/remoteBuild.md for full description) This makes renovate aware of these and allows to correctly extract the current version in case more parameters are being used by the end-user. --- lib/modules/manager/kustomize/extract.spec.ts | 28 +++++++++++++++++++ lib/modules/manager/kustomize/extract.ts | 22 ++++++++++----- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/lib/modules/manager/kustomize/extract.spec.ts b/lib/modules/manager/kustomize/extract.spec.ts index f6f2f67ac613b4..2aaee75d345d1f 100644 --- a/lib/modules/manager/kustomize/extract.spec.ts +++ b/lib/modules/manager/kustomize/extract.spec.ts @@ -148,6 +148,34 @@ describe('modules/manager/kustomize/extract', () => { const pkg = extractResource(`${base}?ref=${version}`); expect(pkg).toEqual(sample); }); + + it('should extract out the version of an http base with additional params', () => { + const base = 'https://github.com/user/test-repo.git'; + const version = 'v1.0.0'; + const sample = { + currentValue: version, + datasource: GithubTagsDatasource.id, + depName: 'user/test-repo', + }; + + const pkg = extractResource( + `${base}?timeout=120&ref=${version}&submodules=false&version=v1`, + ); + expect(pkg).toEqual(sample); + }); + + it('should extract out the version of an http base from version param', () => { + const base = 'https://github.com/user/test-repo.git'; + const version = 'v1.0.0'; + const sample = { + currentValue: version, + datasource: GithubTagsDatasource.id, + depName: 'user/test-repo', + }; + + const pkg = extractResource(`${base}?version=${version}`); + expect(pkg).toEqual(sample); + }); }); describe('extractHelmChart', () => { diff --git a/lib/modules/manager/kustomize/extract.ts b/lib/modules/manager/kustomize/extract.ts index 25378a1643dd2e..f29a20eb673d30 100644 --- a/lib/modules/manager/kustomize/extract.ts +++ b/lib/modules/manager/kustomize/extract.ts @@ -1,3 +1,4 @@ +import querystring from 'querystring'; import is from '@sindresorhus/is'; import { logger } from '../../../logger'; import { coerceArray } from '../../../util/array'; @@ -18,19 +19,19 @@ import type { HelmChart, Image, Kustomize } from './types'; // URL specifications should follow the hashicorp URL format // https://github.com/hashicorp/go-getter#url-format const gitUrl = regEx( - /^(?:git::)?(?(?:(?:(?:http|https|ssh):\/\/)?(?:.*@)?)?(?(?:[^:/\s]+(?::[0-9]+)?[:/])?(?[^/\s]+\/[^/\s]+)))(?[^?\s]*)\?ref=(?.+)$/, + /^(?:git::)?(?(?:(?:(?:http|https|ssh):\/\/)?(?:.*@)?)?(?(?:[^:/\s]+(?::[0-9]+)?[:/])?(?[^/\s]+\/[^/\s]+)))(?[^?\s]*)\?(?.+)$/, ); // regex to match URLs with ".git" delimiter const dotGitRegex = regEx( - /^(?:git::)?(?(?:(?:(?:http|https|ssh):\/\/)?(?:.*@)?)?(?(?:[^:/\s]+(?::[0-9]+)?[:/])?(?[^?\s]*(\.git))))(?[^?\s]*)\?ref=(?.+)$/, + /^(?:git::)?(?(?:(?:(?:http|https|ssh):\/\/)?(?:.*@)?)?(?(?:[^:/\s]+(?::[0-9]+)?[:/])?(?[^?\s]*(\.git))))(?[^?\s]*)\?(?.+)$/, ); // regex to match URLs with "_git" delimiter const underscoreGitRegex = regEx( - /^(?:git::)?(?(?:(?:(?:http|https|ssh):\/\/)?(?:.*@)?)?(?(?:[^:/\s]+(?::[0-9]+)?[:/])?(?[^?\s]*)(_git\/[^/\s]+)))(?[^?\s]*)\?ref=(?.+)$/, + /^(?:git::)?(?(?:(?:(?:http|https|ssh):\/\/)?(?:.*@)?)?(?(?:[^:/\s]+(?::[0-9]+)?[:/])?(?[^?\s]*)(_git\/[^/\s]+)))(?[^?\s]*)\?(?.+)$/, ); // regex to match URLs having an extra "//" const gitUrlWithPath = regEx( - /^(?:git::)?(?(?:(?:(?:http|https|ssh):\/\/)?(?:.*@)?)?(?(?:[^:/\s]+(?::[0-9]+)?[:/])(?[^?\s]+)))(?:\/\/)(?[^?\s]+)\?ref=(?.+)$/, + /^(?:git::)?(?(?:(?:(?:http|https|ssh):\/\/)?(?:.*@)?)?(?(?:[^:/\s]+(?::[0-9]+)?[:/])(?[^?\s]+)))(?:\/\/)(?[^?\s]+)\?(?.+)$/, ); export function extractResource(base: string): PackageDependency | null { @@ -50,10 +51,17 @@ export function extractResource(base: string): PackageDependency | null { return null; } - const { path } = match.groups; + const { path, queryString } = match.groups; + const params = querystring.parse(queryString); + const refParam = Array.isArray(params.ref) ? params.ref[0] : params.ref; + const versionParam = Array.isArray(params.version) + ? params.version[0] + : params.version; + const currentValue = refParam ?? versionParam; + if (regEx(/(?:github\.com)(:|\/)/).test(path)) { return { - currentValue: match.groups.currentValue, + currentValue, datasource: GithubTagsDatasource.id, depName: match.groups.project.replace('.git', ''), }; @@ -63,7 +71,7 @@ export function extractResource(base: string): PackageDependency | null { datasource: GitTagsDatasource.id, depName: path.replace('.git', ''), packageName: match.groups.url, - currentValue: match.groups.currentValue, + currentValue, }; }