Skip to content

Commit

Permalink
fix(manager/kustomize): fix parsing kustomize resource URLs with addi…
Browse files Browse the repository at this point in the history
…tional query parameters (#32746)

Co-authored-by: Rhys Arkins <[email protected]>
  • Loading branch information
roosmaa and rarkins authored Nov 28, 2024
1 parent ed8c37c commit d949a91
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
47 changes: 47 additions & 0 deletions lib/modules/manager/kustomize/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ describe('modules/manager/kustomize/extract', () => {
expect(res).toBeNull();
});

it('should return null for an http base without ref/version', () => {
const base = 'https://github.com/user/test-repo.git';
const res = extractResource(`${base}?timeout=10s`);
expect(res).toBeNull();
});

it('should extract out the version of an http base', () => {
const base = 'https://github.com/user/test-repo.git';
const version = 'v1.0.0';
Expand Down Expand Up @@ -148,6 +154,47 @@ 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 first 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}&version=v0`);
expect(pkg).toEqual(sample);
});

it('should extract out the version of an http base from first ref 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}?ref=${version}&ref=v0`);
expect(pkg).toEqual(sample);
});
});

describe('extractHelmChart', () => {
Expand Down
25 changes: 18 additions & 7 deletions lib/modules/manager/kustomize/extract.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import querystring from 'node:querystring';
import is from '@sindresorhus/is';
import { logger } from '../../../logger';
import { coerceArray } from '../../../util/array';
Expand All @@ -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::)?(?<url>(?:(?:(?:http|https|ssh):\/\/)?(?:.*@)?)?(?<path>(?:[^:/\s]+(?::[0-9]+)?[:/])?(?<project>[^/\s]+\/[^/\s]+)))(?<subdir>[^?\s]*)\?ref=(?<currentValue>.+)$/,
/^(?:git::)?(?<url>(?:(?:(?:http|https|ssh):\/\/)?(?:.*@)?)?(?<path>(?:[^:/\s]+(?::[0-9]+)?[:/])?(?<project>[^/\s]+\/[^/\s]+)))(?<subdir>[^?\s]*)\?(?<queryString>.+)$/,
);
// regex to match URLs with ".git" delimiter
const dotGitRegex = regEx(
/^(?:git::)?(?<url>(?:(?:(?:http|https|ssh):\/\/)?(?:.*@)?)?(?<path>(?:[^:/\s]+(?::[0-9]+)?[:/])?(?<project>[^?\s]*(\.git))))(?<subdir>[^?\s]*)\?ref=(?<currentValue>.+)$/,
/^(?:git::)?(?<url>(?:(?:(?:http|https|ssh):\/\/)?(?:.*@)?)?(?<path>(?:[^:/\s]+(?::[0-9]+)?[:/])?(?<project>[^?\s]*(\.git))))(?<subdir>[^?\s]*)\?(?<queryString>.+)$/,
);
// regex to match URLs with "_git" delimiter
const underscoreGitRegex = regEx(
/^(?:git::)?(?<url>(?:(?:(?:http|https|ssh):\/\/)?(?:.*@)?)?(?<path>(?:[^:/\s]+(?::[0-9]+)?[:/])?(?<project>[^?\s]*)(_git\/[^/\s]+)))(?<subdir>[^?\s]*)\?ref=(?<currentValue>.+)$/,
/^(?:git::)?(?<url>(?:(?:(?:http|https|ssh):\/\/)?(?:.*@)?)?(?<path>(?:[^:/\s]+(?::[0-9]+)?[:/])?(?<project>[^?\s]*)(_git\/[^/\s]+)))(?<subdir>[^?\s]*)\?(?<queryString>.+)$/,
);
// regex to match URLs having an extra "//"
const gitUrlWithPath = regEx(
/^(?:git::)?(?<url>(?:(?:(?:http|https|ssh):\/\/)?(?:.*@)?)?(?<path>(?:[^:/\s]+(?::[0-9]+)?[:/])(?<project>[^?\s]+)))(?:\/\/)(?<subdir>[^?\s]+)\?ref=(?<currentValue>.+)$/,
/^(?:git::)?(?<url>(?:(?:(?:http|https|ssh):\/\/)?(?:.*@)?)?(?<path>(?:[^:/\s]+(?::[0-9]+)?[:/])(?<project>[^?\s]+)))(?:\/\/)(?<subdir>[^?\s]+)\?(?<queryString>.+)$/,
);

export function extractResource(base: string): PackageDependency | null {
Expand All @@ -50,10 +51,20 @@ 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 (!currentValue) {
return null;
}

if (regEx(/(?:github\.com)(:|\/)/).test(path)) {
return {
currentValue: match.groups.currentValue,
currentValue,
datasource: GithubTagsDatasource.id,
depName: match.groups.project.replace('.git', ''),
};
Expand All @@ -63,7 +74,7 @@ export function extractResource(base: string): PackageDependency | null {
datasource: GitTagsDatasource.id,
depName: path.replace('.git', ''),
packageName: match.groups.url,
currentValue: match.groups.currentValue,
currentValue,
};
}

Expand Down

0 comments on commit d949a91

Please sign in to comment.