Skip to content

Commit

Permalink
refactor(helm): More idiomatic schema usage (#24934)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Oct 2, 2023
1 parent 4b9bb12 commit d847715
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 49 deletions.
17 changes: 9 additions & 8 deletions lib/modules/manager/helmfile/artifacts.ts
Expand Up @@ -12,14 +12,12 @@ import {
} from '../../../util/fs';
import { getFile } from '../../../util/git';
import { regEx } from '../../../util/regex';
import { Result } from '../../../util/result';
import { Yaml } from '../../../util/schema-utils';
import { generateHelmEnvs } from '../helmv3/common';
import type { UpdateArtifact, UpdateArtifactsResult } from '../types';
import {
generateRegistryLoginCmd,
isOCIRegistry,
parseDoc,
parseLock,
} from './utils';
import { Doc, LockVersion } from './schema';
import { generateRegistryLoginCmd, isOCIRegistry } from './utils';

export async function updateArtifacts({
packageFileName,
Expand Down Expand Up @@ -58,7 +56,7 @@ export async function updateArtifacts({
toolName: 'helmfile',
constraint:
config.constraints?.helmfile ??
parseLock(existingLockFileContent).version,
Result.parse(existingLockFileContent, LockVersion).unwrapOrNull(),
},
];
const needKustomize = updatedDeps.some(
Expand All @@ -72,7 +70,10 @@ export async function updateArtifacts({
}

const cmd: string[] = [];
const doc = parseDoc(newPackageFileContent);
const doc = Result.parse(
newPackageFileContent,
Yaml.pipe(Doc)
).unwrapOrThrow();

for (const value of coerceArray(doc.repositories).filter(isOCIRegistry)) {
const loginCmd = await generateRegistryLoginCmd(
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/manager/helmfile/extract.ts
Expand Up @@ -9,7 +9,7 @@ import type {
PackageDependency,
PackageFileContent,
} from '../types';
import type { Doc } from './types';
import type { Doc } from './schema';
import {
kustomizationsKeysUsed,
localChartHasKustomizationsYaml,
Expand Down
21 changes: 13 additions & 8 deletions lib/modules/manager/helmfile/schema.ts
@@ -1,25 +1,30 @@
import { z } from 'zod';
import { Yaml } from '../../../util/schema-utils';

export const RepositorySchema = z.object({
export const HelmRepository = z.object({
name: z.string(),
url: z.string(),
oci: z.boolean().optional(),
});
export type HelmRepository = z.infer<typeof HelmRepository>;

export const ReleaseSchema = z.object({
export const HelmRelease = z.object({
name: z.string(),
chart: z.string(),
version: z.string(),
strategicMergePatches: z.unknown().optional(),
jsonPatches: z.unknown().optional(),
transformers: z.unknown().optional(),
});
export type HelmRelease = z.infer<typeof HelmRelease>;

export const DocSchema = z.object({
releases: z.array(ReleaseSchema).optional(),
repositories: z.array(RepositorySchema).optional(),
export const Doc = z.object({
releases: z.array(HelmRelease).optional(),
repositories: z.array(HelmRepository).optional(),
});
export type Doc = z.infer<typeof Doc>;

export const LockSchema = z.object({
version: z.string(),
});
export const LockVersion = Yaml.pipe(
z.object({ version: z.string() }).transform(({ version }) => version)
);
export type LockVersion = z.infer<typeof LockVersion>;
16 changes: 0 additions & 16 deletions lib/modules/manager/helmfile/types.ts

This file was deleted.

20 changes: 4 additions & 16 deletions lib/modules/manager/helmfile/utils.ts
@@ -1,4 +1,3 @@
import yaml from 'js-yaml';
import upath from 'upath';

import { getParentDir, localPathExists } from '../../../util/fs';
Expand All @@ -7,11 +6,10 @@ import { DockerDatasource } from '../../datasource/docker';
import { generateLoginCmd } from '../helmv3/common';
import type { RepositoryRule } from '../helmv3/types';

import { DocSchema, LockSchema } from './schema';
import type { Doc, Lock, Release, Repository } from './types';
import type { HelmRelease, HelmRepository } from './schema';

/** Returns true if a helmfile release contains kustomize specific keys **/
export function kustomizationsKeysUsed(release: Release): boolean {
export function kustomizationsKeysUsed(release: HelmRelease): boolean {
return (
release.strategicMergePatches !== undefined ||
release.jsonPatches !== undefined ||
Expand All @@ -22,7 +20,7 @@ export function kustomizationsKeysUsed(release: Release): boolean {
/** Returns true if a helmfile release uses a local chart with a kustomization.yaml file **/
// eslint-disable-next-line require-await
export async function localChartHasKustomizationsYaml(
release: Release,
release: HelmRelease,
helmFileYamlFileName: string
): Promise<boolean> {
const helmfileYamlParentDir = getParentDir(helmFileYamlFileName) || '';
Expand All @@ -31,17 +29,7 @@ export async function localChartHasKustomizationsYaml(
);
}

export function parseDoc(packageFileContent: string): Doc {
const doc = yaml.load(packageFileContent);
return DocSchema.parse(doc);
}

export function parseLock(lockFileContent: string): Lock {
const lock = yaml.load(lockFileContent);
return LockSchema.parse(lock);
}

export function isOCIRegistry(repository: Repository): boolean {
export function isOCIRegistry(repository: HelmRepository): boolean {
return repository.oci === true;
}

Expand Down

0 comments on commit d847715

Please sign in to comment.