Skip to content
Snippets Groups Projects
Unverified Commit d847715d authored by Sergei Zharinov's avatar Sergei Zharinov Committed by GitHub
Browse files

refactor(helm): More idiomatic schema usage (#24934)

parent 4b9bb12c
No related branches found
No related tags found
No related merge requests found
......@@ -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,
......@@ -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(
......@@ -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(
......
......@@ -9,7 +9,7 @@ import type {
PackageDependency,
PackageFileContent,
} from '../types';
import type { Doc } from './types';
import type { Doc } from './schema';
import {
kustomizationsKeysUsed,
localChartHasKustomizationsYaml,
......
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(),
......@@ -14,12 +16,15 @@ export const ReleaseSchema = z.object({
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>;
import type { z } from 'zod';
import type {
DocSchema,
LockSchema,
ReleaseSchema,
RepositorySchema,
} from './schema';
export type Release = z.infer<typeof ReleaseSchema>;
export type Repository = z.infer<typeof RepositorySchema>;
export type Doc = z.infer<typeof DocSchema>;
export type Lock = z.infer<typeof LockSchema>;
import yaml from 'js-yaml';
import upath from 'upath';
import { getParentDir, localPathExists } from '../../../util/fs';
......@@ -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 ||
......@@ -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) || '';
......@@ -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;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment