Skip to content

Commit

Permalink
feat(cloudbuild): Parse config using Zod schema (#33768)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov authored Jan 31, 2025
1 parent 7819d02 commit 586d9fd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 36 deletions.
48 changes: 12 additions & 36 deletions lib/modules/manager/cloudbuild/extract.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,25 @@
import is from '@sindresorhus/is';
import { logger } from '../../../logger';
import { parseSingleYaml } from '../../../util/yaml';
import { getDep } from '../dockerfile/extract';
import type { PackageDependency, PackageFileContent } from '../types';
import type { PackageFileContent } from '../types';
import { CloudbuildSteps } from './schema';

export function extractPackageFile(
content: string,
packageFile?: string,
): PackageFileContent | null {
const deps: PackageDependency[] = [];
try {
// TODO: fix types
const doc: any = parseSingleYaml(content);
if (doc?.steps && is.array(doc.steps)) {
for (const step of doc.steps) {
if (step.name) {
const dep = getDep(step.name);
logger.trace(
{
depName: dep.depName,
currentValue: dep.currentValue,
currentDigest: dep.currentDigest,
},
'Cloud Build docker image',
);
const deps = CloudbuildSteps.catch(({ error: err }) => {
logger.debug(
{ err, packageFile },
'Cloud Build: error extracting Docker images from a configuration file.',
);
return [];
})
.transform((steps) => steps.map((step) => getDep(step)))
.parse(content);

deps.push(dep);
}
}
}
} catch (err) /* istanbul ignore next */ {
if (err.stack?.startsWith('YAMLException:')) {
logger.debug(
{ err, packageFile },
'YAML exception extracting Docker images from a Cloud Build configuration file.',
);
} else {
logger.debug(
{ err, packageFile },
'Error extracting Docker images from a Cloud Build configuration file.',
);
}
}
if (!deps.length) {
return null;
}

return { deps };
}
12 changes: 12 additions & 0 deletions lib/modules/manager/cloudbuild/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { z } from 'zod';
import { LooseArray, Yaml } from '../../../util/schema-utils';

export const CloudbuildSteps = Yaml.pipe(
z
.object({
steps: LooseArray(
z.object({ name: z.string() }).transform(({ name }) => name),
),
})
.transform(({ steps }) => steps),
);

0 comments on commit 586d9fd

Please sign in to comment.