Skip to content

Commit

Permalink
chore: Cleanup and code improvements
Browse files Browse the repository at this point in the history
Signed-off-by: kvanzuijlen <[email protected]>
  • Loading branch information
kvanzuijlen committed Jan 1, 2024
1 parent 0e8eedb commit c3c95ca
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 269 deletions.
21 changes: 16 additions & 5 deletions lib/modules/manager/helm-values/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { z } from 'zod';
import { Yaml } from '../../../util/schema-utils';

export interface HelmDockerImageDependencyBasic {
registry?: string;
repository: string;
Expand All @@ -19,8 +22,16 @@ export type HelmDockerImageDependency =
| HelmDockerImageDependencyTag
| HelmDockerImageDependencyVersion;

export interface ChartDefinition {
apiVersion: string;
name: string;
version: string;
}
export const ChartDefinition = z
.object({
apiVersion: z.string().regex(/v([12])/),
name: z.string().min(1),
version: z.string().regex(
// Copied from https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/,
),
})
.partial();
export type ChartDefinition = z.infer<typeof ChartDefinition>;

export const ChartDefinitionYaml = Yaml.pipe(ChartDefinition);
83 changes: 30 additions & 53 deletions lib/modules/manager/helm-values/update.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,81 +22,58 @@ describe('modules/manager/helm-values/update', () => {
beforeEach(() => {
jest.resetAllMocks();
fs.readLocalFile.mockResolvedValueOnce(chartContent);
fs.getSiblingFileName.mockReturnValueOnce('test/Chart.yaml');
});

it('increments', async () => {
const { bumpedContent, bumpedFiles } =
await helmValuesUpdater.bumpPackageVersion(
helmValuesContent,
'0.0.2',
'patch',
'test/values.yaml',
);
expect(bumpedContent).toEqual(helmValuesContent);
expect(bumpedFiles).toHaveLength(1);
const bumpedFile = bumpedFiles![0];
expect(bumpedFile.fileName).toBe('test/Chart.yaml');
expect(bumpedFile.newContent).toBe(
'apiVersion: v2\nname: test\nversion: 0.0.3\n',
const { bumpedContent } = await helmValuesUpdater.bumpPackageVersion(
helmValuesContent,
'0.0.2',
'patch',
'test/values.yaml',
);
expect(bumpedContent).toEqual(helmValuesContent);
});

it('no ops', async () => {
const { bumpedContent, bumpedFiles } =
await helmValuesUpdater.bumpPackageVersion(
helmValuesContent,
'0.0.1',
'patch',
'values.yaml',
);
const { bumpedContent } = await helmValuesUpdater.bumpPackageVersion(
helmValuesContent,
'0.0.1',
'patch',
'values.yaml',
);
expect(bumpedContent).toEqual(helmValuesContent);
expect(bumpedFiles).toHaveLength(1);
const bumpedFile = bumpedFiles![0];
expect(bumpedFile.newContent).toEqual(chartContent);
});

it('updates', async () => {
const { bumpedContent, bumpedFiles } =
await helmValuesUpdater.bumpPackageVersion(
helmValuesContent,
'0.0.1',
'minor',
'test/values.yaml',
);
expect(bumpedContent).toEqual(helmValuesContent);
expect(bumpedFiles).toHaveLength(1);
const bumpedFile = bumpedFiles![0];
expect(bumpedFile.fileName).toBe('test/Chart.yaml');
expect(bumpedFile.newContent).toBe(
'apiVersion: v2\nname: test\nversion: 0.1.0\n',
const { bumpedContent } = await helmValuesUpdater.bumpPackageVersion(
helmValuesContent,
'0.0.1',
'minor',
'test/values.yaml',
);
expect(bumpedContent).toEqual(helmValuesContent);
});

it('returns content if bumping errors', async () => {
const { bumpedContent, bumpedFiles } =
await helmValuesUpdater.bumpPackageVersion(
helmValuesContent,
'0.0.2',
true as any,
'values.yaml',
);
const { bumpedContent } = await helmValuesUpdater.bumpPackageVersion(
helmValuesContent,
'0.0.2',
true as any,
'values.yaml',
);
expect(bumpedContent).toEqual(helmValuesContent);
expect(bumpedFiles).toBeUndefined();
});

it('returns content if retrieving Chart.yaml fails', async () => {
jest.resetAllMocks();
fs.readLocalFile.mockRejectedValueOnce(null);
const { bumpedContent, bumpedFiles } =
await helmValuesUpdater.bumpPackageVersion(
helmValuesContent,
'0.0.2',
'minor',
'values.yaml',
);
const { bumpedContent } = await helmValuesUpdater.bumpPackageVersion(
helmValuesContent,
'0.0.2',
'minor',
'values.yaml',
);
expect(bumpedContent).toEqual(helmValuesContent);
expect(bumpedFiles).toBeUndefined();
});
});
});
42 changes: 15 additions & 27 deletions lib/modules/manager/helm-values/update.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ReleaseType, inc } from 'semver';
import { logger } from '../../../logger';
import { getSiblingFileName } from '../../../util/fs';
import type { BumpPackageVersionResult } from '../types';
import { getSiblingChartYamlContent } from './util';

Expand All @@ -14,33 +13,9 @@ export async function bumpPackageVersion(
{ bumpVersion, currentValue },
'Checking if we should bump Chart.yaml version',
);
const chartFileName = getSiblingFileName(packageFile, 'Chart.yaml');
const chartYamlContent = await getSiblingChartYamlContent(packageFile);
try {
const newChartVersion = inc(currentValue, bumpVersion);
if (!newChartVersion) {
throw new Error('semver inc failed');
}
if (chartYamlContent === null) {
throw new Error(
'Cannot bump chart version because Chart.yaml could not be read.',
);
}
logger.debug({ newChartVersion });
const bumpedContent = chartYamlContent?.replace(
/^(version:\s*).*$/m,
`$1${newChartVersion}`,
);
if (bumpedContent === chartYamlContent) {
logger.debug('Version was already bumped');
} else {
logger.debug('Bumped Chart.yaml version');
}
return {
bumpedContent: content,
bumpedFiles: [{ fileName: chartFileName, newContent: bumpedContent }],
};
} catch (err) {
const newChartVersion = inc(currentValue, bumpVersion);
if (!newChartVersion || chartYamlContent === null) {
logger.warn(
{
chartYamlContent,
Expand All @@ -53,4 +28,17 @@ export async function bumpPackageVersion(
bumpedContent: content,
};
}
logger.debug({ newChartVersion });
const bumpedContent = chartYamlContent?.replace(
/^(version:\s*).*$/m,
`$1${newChartVersion}`,
);
if (bumpedContent === chartYamlContent) {
logger.debug('Version was already bumped');
} else {
logger.debug('Bumped Chart.yaml version');
}
return {
bumpedContent: content,
};
}
17 changes: 6 additions & 11 deletions lib/modules/manager/helm-values/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import { logger } from '../../../logger';
import { getSiblingFileName, readLocalFile } from '../../../util/fs';
import { hasKey } from '../../../util/object';
import { regEx } from '../../../util/regex';
import { parseSingleYaml } from '../../../util/yaml';
import type { ChartDefinition, HelmDockerImageDependency } from './types';
import {
type ChartDefinition,
ChartDefinitionYaml,
type HelmDockerImageDependency,
} from './types';

const parentKeyRe = regEx(/image$/i);

Expand Down Expand Up @@ -78,15 +81,7 @@ export async function getParsedSiblingChartYaml(
logger.debug({ fileName }, 'Failed to find helm Chart.yaml');
return null;
}
const chart = parseSingleYaml(chartContents) as ChartDefinition;
if (!(chart?.apiVersion && chart.name && chart.version)) {
logger.debug(
{ fileName },
'Failed to find required fields in Chart.yaml',
);
return null;
}
return chart;
return ChartDefinitionYaml.parse(chartContents);
} catch (err) {
logger.debug({ fileName }, 'Failed to parse helm Chart.yaml');
return null;
Expand Down
6 changes: 0 additions & 6 deletions lib/modules/manager/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,6 @@ export interface UpdateDependencyConfig<T = Record<string, any>> {

export interface BumpPackageVersionResult {
bumpedContent: string | null;
// describes files that was changed instead of or in addition to the packageFile
bumpedFiles?: BumpedPackageFile[];
}
export interface BumpedPackageFile {
fileName: string;
newContent: string;
}

export interface UpdateLockedConfig {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`workers/repository/update/branch/get-updated getUpdatedPackageFiles() bumps versions in autoReplace managers 1`] = `
{
"artifactErrors": [],
"reuseExistingBranch": undefined,
"updatedArtifacts": [],
"updatedPackageFiles": [
{
"contents": "version: 0.0.2",
"path": "Chart.yaml",
"type": "addition",
},
],
}
`;

exports[`workers/repository/update/branch/get-updated getUpdatedPackageFiles() bumps versions in updateDependency managers 1`] = `
{
"artifactErrors": [],
Expand Down Expand Up @@ -175,75 +190,6 @@ exports[`workers/repository/update/branch/get-updated getUpdatedPackageFiles() h
}
`;

exports[`workers/repository/update/branch/get-updated getUpdatedPackageFiles() in autoReplace managers bumps versions 1`] = `
{
"artifactErrors": [],
"reuseExistingBranch": undefined,
"updatedArtifacts": [],
"updatedPackageFiles": [
{
"contents": "version: 0.0.2",
"path": "Chart.yaml",
"type": "addition",
},
],
}
`;

exports[`workers/repository/update/branch/get-updated getUpdatedPackageFiles() in autoReplace managers bumps versions in all files if multiple files were bumped 1`] = `
{
"artifactErrors": [],
"reuseExistingBranch": undefined,
"updatedArtifacts": [],
"updatedPackageFiles": [
{
"contents": "new content",
"path": "values.yaml",
"type": "addition",
},
{
"contents": "version: 0.0.2",
"path": "/test/Chart.yaml",
"type": "addition",
},
{
"contents": "# Version 0.0.2",
"path": "/test/README.md",
"type": "addition",
},
],
}
`;

exports[`workers/repository/update/branch/get-updated getUpdatedPackageFiles() in autoReplace managers bumps versions with a bumpPackageFile different from the packageFile 1`] = `
{
"artifactErrors": [],
"reuseExistingBranch": undefined,
"updatedArtifacts": [],
"updatedPackageFiles": [
{
"contents": "new content",
"path": "values.yaml",
"type": "addition",
},
{
"contents": "version: 0.0.2",
"path": "/test/Chart.yaml",
"type": "addition",
},
],
}
`;

exports[`workers/repository/update/branch/get-updated getUpdatedPackageFiles() in autoReplace managers doesn't bump versions if content wasn't changed 1`] = `
{
"artifactErrors": [],
"reuseExistingBranch": undefined,
"updatedArtifacts": [],
"updatedPackageFiles": [],
}
`;

exports[`workers/repository/update/branch/get-updated getUpdatedPackageFiles() update artifacts on update-lockfile strategy 1`] = `
{
"artifactErrors": [],
Expand Down
Loading

0 comments on commit c3c95ca

Please sign in to comment.