Skip to content

Commit

Permalink
fix(helm-requirements): remove the need for a Chart.yaml file (#7544)
Browse files Browse the repository at this point in the history
  • Loading branch information
eh-am authored Feb 6, 2021
1 parent 150092a commit 6b15f6e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 66 deletions.
63 changes: 20 additions & 43 deletions lib/manager/helm-requirements/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('lib/manager/helm-requirements/extract', () => {
jest.resetAllMocks();
fs.readLocalFile = jest.fn();
});
it('skips invalid registry urls', async () => {
it('skips invalid registry urls', () => {
fs.readLocalFile.mockResolvedValueOnce(`
apiVersion: v1
appVersion: "1.0"
Expand All @@ -30,7 +30,7 @@ describe('lib/manager/helm-requirements/extract', () => {
version: 0.8.1
`;
const fileName = 'requirements.yaml';
const result = await extractPackageFile(content, fileName, {
const result = extractPackageFile(content, fileName, {
aliases: {
stable: 'https://charts.helm.sh/stable/',
},
Expand All @@ -39,7 +39,7 @@ describe('lib/manager/helm-requirements/extract', () => {
expect(result).toMatchSnapshot();
expect(result.deps.every((dep) => dep.skipReason)).toEqual(true);
});
it('parses simple requirements.yaml correctly', async () => {
it('parses simple requirements.yaml correctly', () => {
fs.readLocalFile.mockResolvedValueOnce(`
apiVersion: v1
appVersion: "1.0"
Expand All @@ -57,30 +57,30 @@ describe('lib/manager/helm-requirements/extract', () => {
repository: https://charts.helm.sh/stable/
`;
const fileName = 'requirements.yaml';
const result = await extractPackageFile(content, fileName, {
const result = extractPackageFile(content, fileName, {
aliases: {
stable: 'https://charts.helm.sh/stable/',
},
});
expect(result).not.toBeNull();
expect(result).toMatchSnapshot();
});
it('parses simple requirements.yaml but skips if necessary fields missing', async () => {
it('parses simple requirements.yaml but skips if necessary fields missing', () => {
fs.readLocalFile.mockResolvedValueOnce(`
apiVersion: v1
appVersion: "1.0"
description: A Helm chart for Kubernetes
name: example
`);
const fileName = 'requirements.yaml';
const result = await extractPackageFile('', fileName, {
const result = extractPackageFile('', fileName, {
aliases: {
stable: 'https://charts.helm.sh/stable/',
},
});
expect(result).toBeNull();
});
it('resolves aliased registry urls', async () => {
it('resolves aliased registry urls', () => {
fs.readLocalFile.mockResolvedValueOnce(`
apiVersion: v1
appVersion: "1.0"
Expand All @@ -95,7 +95,7 @@ describe('lib/manager/helm-requirements/extract', () => {
repository: '@placeholder'
`;
const fileName = 'requirements.yaml';
const result = await extractPackageFile(content, fileName, {
const result = extractPackageFile(content, fileName, {
aliases: {
placeholder: 'https://my-registry.gcr.io/',
},
Expand All @@ -104,30 +104,7 @@ describe('lib/manager/helm-requirements/extract', () => {
expect(result).toMatchSnapshot();
expect(result.deps.every((dep) => dep.skipReason)).toEqual(false);
});
it("doesn't fail if Chart.yaml is invalid", async () => {
fs.readLocalFile.mockResolvedValueOnce(`
Invalid Chart.yaml content.
arr:
[
`);
const content = `
dependencies:
- name: redis
version: 0.9.0
repository: https://charts.helm.sh/stable/
- name: postgresql
version: 0.8.1
repository: https://charts.helm.sh/stable/
`;
const fileName = 'requirements.yaml';
const result = await extractPackageFile(content, fileName, {
aliases: {
stable: 'https://charts.helm.sh/stable/',
},
});
expect(result).toBeNull();
});
it('skips local dependencies', async () => {
it('skips local dependencies', () => {
fs.readLocalFile.mockResolvedValueOnce(`
apiVersion: v1
appVersion: "1.0"
Expand All @@ -145,15 +122,15 @@ describe('lib/manager/helm-requirements/extract', () => {
repository: file:///some/local/path/
`;
const fileName = 'requirements.yaml';
const result = await extractPackageFile(content, fileName, {
const result = extractPackageFile(content, fileName, {
aliases: {
stable: 'https://charts.helm.sh/stable/',
},
});
expect(result).not.toBeNull();
expect(result).toMatchSnapshot();
});
it('returns null if no dependencies', async () => {
it('returns null if no dependencies', () => {
fs.readLocalFile.mockResolvedValueOnce(`
apiVersion: v1
appVersion: "1.0"
Expand All @@ -165,14 +142,14 @@ describe('lib/manager/helm-requirements/extract', () => {
hello: world
`;
const fileName = 'requirements.yaml';
const result = await extractPackageFile(content, fileName, {
const result = extractPackageFile(content, fileName, {
aliases: {
stable: 'https://charts.helm.sh/stable/',
},
});
expect(result).toBeNull();
});
it('returns null if requirements.yaml is invalid', async () => {
it('returns null if requirements.yaml is invalid', () => {
fs.readLocalFile.mockResolvedValueOnce(`
apiVersion: v1
appVersion: "1.0"
Expand All @@ -186,17 +163,17 @@ describe('lib/manager/helm-requirements/extract', () => {
[
`;
const fileName = 'requirements.yaml';
const result = await extractPackageFile(content, fileName, {
const result = extractPackageFile(content, fileName, {
aliases: {
stable: 'https://charts.helm.sh/stable/',
},
});
expect(result).toBeNull();
});
it('returns null if Chart.yaml is empty', async () => {
it('returns null if Chart.yaml is empty', () => {
const content = '';
const fileName = 'requirements.yaml';
const result = await extractPackageFile(content, fileName, {
const result = extractPackageFile(content, fileName, {
aliases: {
stable: 'https://charts.helm.sh/stable/',
},
Expand Down Expand Up @@ -258,7 +235,7 @@ describe('lib/manager/helm-requirements/extract', () => {
},
},
])('validates required fields', (params) => {
it(`validates ${params.fieldName} is required`, async () => {
it(`validates ${params.fieldName} is required`, () => {
fs.readLocalFile.mockResolvedValueOnce(`
apiVersion: v1
appVersion: "1.0"
Expand All @@ -267,11 +244,11 @@ describe('lib/manager/helm-requirements/extract', () => {
version: 0.1.0
`);
const fileName = 'requirements.yaml';
const result = await extractPackageFile(params.content, fileName, {});
const result = extractPackageFile(params.content, fileName, {});
expect(result).toEqual(params.want);
});
});
it('skips only invalid dependences', async () => {
it('skips only invalid dependences', () => {
fs.readLocalFile.mockResolvedValueOnce(`
apiVersion: v1
appVersion: "1.0"
Expand All @@ -292,7 +269,7 @@ describe('lib/manager/helm-requirements/extract', () => {
repository: https://charts.helm.sh/stable/
`;
const fileName = 'requirements.yaml';
const result = await extractPackageFile(content, fileName, {});
const result = extractPackageFile(content, fileName, {});
expect(result).toEqual({
datasource: 'helm',
deps: [
Expand Down
25 changes: 2 additions & 23 deletions lib/manager/helm-requirements/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,13 @@ import yaml from 'js-yaml';
import * as datasourceHelm from '../../datasource/helm';
import { logger } from '../../logger';
import { SkipReason } from '../../types';
import { getSiblingFileName, readLocalFile } from '../../util/fs';
import { ExtractConfig, PackageDependency, PackageFile } from '../common';

export async function extractPackageFile(
export function extractPackageFile(
content: string,
fileName: string,
config: ExtractConfig
): Promise<PackageFile> {
try {
const chartFileName = getSiblingFileName(fileName, 'Chart.yaml');
const chartContents = await readLocalFile(chartFileName, 'utf8');
if (!chartContents) {
logger.debug({ fileName }, 'Failed to find helm Chart.yaml');
return null;
}
// TODO: fix me
const chart = yaml.safeLoad(chartContents, { json: true }) as any;
if (!(chart?.apiVersion && chart.name && chart.version)) {
logger.debug(
{ fileName },
'Failed to find required fields in Chart.yaml'
);
return null;
}
} catch (err) {
logger.debug({ fileName }, 'Failed to parse helm Chart.yaml');
return null;
}
): PackageFile {
let deps = [];
let doc;
try {
Expand Down

0 comments on commit 6b15f6e

Please sign in to comment.