-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cloudbuild): Parse config using Zod schema (#33768)
- Loading branch information
Showing
2 changed files
with
24 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
); |