Skip to content

Commit

Permalink
fix(kustomize): lookup Github dependency using HTTP (#7456)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Kriese <[email protected]>
  • Loading branch information
etiennetremel and viceice authored Oct 14, 2020
1 parent da4bc37 commit 3b1ed2f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 73 deletions.
34 changes: 15 additions & 19 deletions lib/manager/kustomize/__snapshots__/extract.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ exports[`manager/kustomize/extract extractPackageFile() extracts http dependency
Array [
Object {
"currentValue": "v0.0.1",
"datasource": "git-tags",
"depName": "github.com/user/repo//deploy",
"lookupName": "github.com/user/repo",
"datasource": "github-tags",
"depName": "user/repo",
},
Object {
"currentValue": "1.19.0",
"datasource": "github-tags",
"depName": "fluxcd/flux/deploy",
"lookupName": "fluxcd/flux",
"depName": "fluxcd/flux",
},
]
`;
Expand All @@ -22,14 +20,16 @@ Array [
Object {
"currentValue": "v0.0.1",
"datasource": "git-tags",
"depName": "https://moredhel/remote-kustomize.git",
"depName": "moredhel/remote-kustomize",
"depNameShort": "moredhel/remote-kustomize",
"lookupName": "https://moredhel/remote-kustomize.git",
},
Object {
"currentValue": "v0.0.1",
"datasource": "git-tags",
"depName": "https://moredhel/remote-kustomize.git//deploy",
"lookupName": "https://moredhel/remote-kustomize.git//deploy",
"depName": "moredhel/remote-kustomize",
"depNameShort": "moredhel/remote-kustomize",
"lookupName": "https://moredhel/remote-kustomize.git",
},
]
`;
Expand All @@ -38,9 +38,8 @@ exports[`manager/kustomize/extract extractPackageFile() extracts ssh dependency
Array [
Object {
"currentValue": "v0.0.1",
"datasource": "git-tags",
"depName": "[email protected]:moredhel/remote-kustomize.git",
"lookupName": "[email protected]:moredhel/remote-kustomize.git",
"datasource": "github-tags",
"depName": "moredhel/remote-kustomize",
},
]
`;
Expand All @@ -49,9 +48,8 @@ exports[`manager/kustomize/extract extractPackageFile() extracts ssh dependency
Array [
Object {
"currentValue": "v2.0.0",
"datasource": "git-tags",
"depName": "[email protected]:kubernetes-sigs/kustomize.git//examples/helloWorld",
"lookupName": "[email protected]:kubernetes-sigs/kustomize.git",
"datasource": "github-tags",
"depName": "kubernetes-sigs/kustomize",
},
]
`;
Expand All @@ -60,15 +58,13 @@ exports[`manager/kustomize/extract extractPackageFile() should extract bases fro
Array [
Object {
"currentValue": "v0.0.1",
"datasource": "git-tags",
"depName": "[email protected]:moredhel/remote-kustomize.git",
"lookupName": "[email protected]:moredhel/remote-kustomize.git",
"datasource": "github-tags",
"depName": "moredhel/remote-kustomize",
},
Object {
"currentValue": "1.19.0",
"datasource": "github-tags",
"depName": "fluxcd/flux/deploy",
"lookupName": "fluxcd/flux",
"depName": "fluxcd/flux",
},
]
`;
Expand Down
57 changes: 37 additions & 20 deletions lib/manager/kustomize/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,52 +69,71 @@ describe('manager/kustomize/extract', () => {
const version = 'v1.0.0';
const sample = {
currentValue: version,
datasource: datasourceGitTags.id,
depName: base,
lookupName: base,
datasource: datasourceGitHubTags.id,
depName: 'user/test-repo',
};

const pkg = extractBase(`${base}?ref=${version}`);
expect(pkg).toEqual(sample);
});

it('should extract the version of a non http base', () => {
const pkg = extractBase(
'ssh://[email protected]/user/test-repo?ref=v1.2.3'
);
expect(pkg).toEqual({
currentValue: 'v1.2.3',
datasource: datasourceGitTags.id,
depName: 'bitbucket.com/user/test-repo',
depNameShort: 'user/test-repo',
lookupName: 'ssh://[email protected]/user/test-repo',
});
});
it('should extract the version of a non http base with subdir', () => {
const pkg = extractBase(
'ssh://[email protected]/user/test-repo/subdir?ref=v1.2.3'
);
expect(pkg).toEqual({
currentValue: 'v1.2.3',
datasource: datasourceGitTags.id,
depName: 'bitbucket.com/user/test-repo',
depNameShort: 'user/test-repo',
lookupName: 'ssh://[email protected]/user/test-repo',
});
});
it('should extract out the version of an github base', () => {
const base = 'fluxcd/flux/deploy';
const base = 'github.com/fluxcd/flux/deploy';
const version = 'v1.0.0';
const sample = {
currentValue: version,
datasource: datasourceGitHubTags.id,
depName: base,
lookupName: 'fluxcd/flux',
depName: 'fluxcd/flux',
};

const pkg = extractBase(`github.com/${base}?ref=${version}`);
const pkg = extractBase(`${base}?ref=${version}`);
expect(pkg).toEqual(sample);
});
it('should extract out the version of a git base', () => {
const base = '[email protected]:user/repo.git';
const version = 'v1.0.0';
const sample = {
currentValue: version,
datasource: datasourceGitTags.id,
depName: base,
lookupName: base,
datasource: datasourceGitHubTags.id,
depName: 'user/repo',
};

const pkg = extractBase(`${base}?ref=${version}`);
expect(pkg).toEqual(sample);
});
it('should extract out the version of a git base with subdir', () => {
const base = '[email protected]:user/repo.git';
const base = '[email protected]:user/repo.git/subdir';
const version = 'v1.0.0';
const sample = {
currentValue: version,
datasource: datasourceGitTags.id,
depName: `${base}//subdir`,
lookupName: base,
datasource: datasourceGitHubTags.id,
depName: 'user/repo',
};

const pkg = extractBase(`${sample.depName}?ref=${version}`);
const pkg = extractBase(`${base}?ref=${version}`);
expect(pkg).toEqual(sample);
});
});
Expand Down Expand Up @@ -217,8 +236,7 @@ describe('manager/kustomize/extract', () => {
expect(res.deps).toHaveLength(2);
expect(res.deps[0].currentValue).toEqual('v0.0.1');
expect(res.deps[1].currentValue).toEqual('1.19.0');
expect(res.deps[1].depName).toEqual('fluxcd/flux/deploy');
expect(res.deps[1].lookupName).toEqual('fluxcd/flux');
expect(res.deps[1].depName).toEqual('fluxcd/flux');
});
it('should extract out image versions', () => {
const res = extractPackageFile(gitImages);
Expand All @@ -240,8 +258,7 @@ describe('manager/kustomize/extract', () => {
expect(res.deps).toHaveLength(2);
expect(res.deps[0].currentValue).toEqual('v0.0.1');
expect(res.deps[1].currentValue).toEqual('1.19.0');
expect(res.deps[1].depName).toEqual('fluxcd/flux/deploy');
expect(res.deps[1].lookupName).toEqual('fluxcd/flux');
expect(res.deps[1].depName).toEqual('fluxcd/flux');
});
});
});
51 changes: 17 additions & 34 deletions lib/manager/kustomize/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,49 +18,32 @@ interface Kustomize {
images: Image[];
}

// extract the version from the url
const versionMatch = /(?<basename>.*)\?ref=(?<version>.*)\s*$/;

// extract the url from the base of a url with a subdir
const extractUrl = /^(?<url>.*)(?:\/\/.*)$/;

const githubUrl = /^github\.com\/(?<depName>(?<lookupName>[^/]+?\/[^/]+?)(?:\/[^/]+?)*)\?ref=(?<currentValue>.+)$/;
// URL specifications should follow the hashicorp URL format
// https://github.com/hashicorp/go-getter#url-format
const gitUrl = /^(?:git::)?(?<url>(?:(?:(?:http|https|ssh):\/\/)?(?:.*@)?)?(?<path>(?:[^:/]+[:/])?(?<project>[^/]+\/[^/]+)))(?<subdir>[^?]*)\?ref=(?<currentValue>.+)$/;

export function extractBase(base: string): PackageDependency | null {
const githubMatch = githubUrl.exec(base);

if (githubMatch?.groups) {
const { currentValue, depName, lookupName } = githubMatch.groups;
const match = gitUrl.exec(base);

return {
datasource: datasourceGitHubTags.id,
depName,
lookupName,
currentValue,
};
if (!match) {
return null;
}

const basenameVersion = versionMatch.exec(base);
if (basenameVersion) {
const currentValue = basenameVersion.groups.version;
const root = basenameVersion.groups.basename;

const urlResult = extractUrl.exec(root);
let url = root;
// if a match, then there was a subdir, update
if (urlResult && !url.startsWith('http')) {
url = urlResult.groups.url;
}

if (match?.groups.path.startsWith('github.com')) {
return {
datasource: datasourceGitTags.id,
depName: root,
lookupName: url,
currentValue,
currentValue: match.groups.currentValue,
datasource: datasourceGitHubTags.id,
depName: match.groups.project.replace('.git', ''),
};
}

return null;
return {
datasource: datasourceGitTags.id,
depName: match.groups.path.replace('.git', ''),
depNameShort: match.groups.project.replace('.git', ''),
lookupName: match.groups.url,
currentValue: match.groups.currentValue,
};
}

export function extractImage(image: Image): PackageDependency | null {
Expand Down

0 comments on commit 3b1ed2f

Please sign in to comment.