Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use yaml instead of js-yaml for parsing YAML files #31336

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions lib/modules/datasource/bitrise/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,9 @@ export class BitriseDatasource extends Datasource {
customSchema: BitriseStepFile,
});

const releaseTimestamp = is.string(published_at)
? published_at
: published_at.toISOString();
result.releases.push({
version: versionDir.name,
releaseTimestamp,
releaseTimestamp: published_at,
sourceUrl: source_code_url,
});
}
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/datasource/bitrise/schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { z } from 'zod';

export const BitriseStepFile = z.object({
published_at: z.date().or(z.string()),
published_at: z.string(),
viceice marked this conversation as resolved.
Show resolved Hide resolved
source_code_url: z.string().optional(),
});
4 changes: 1 addition & 3 deletions lib/modules/datasource/conan/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ export class ConanDatasource extends Datasource {
headers: { accept: 'application/vnd.github.v3.raw' },
});
// TODO: use schema (#9610)
const doc = parseSingleYaml<ConanYAML>(res.body, {
json: true,
});
const doc = parseSingleYaml<ConanYAML>(res.body);
return {
releases: Object.keys(doc?.versions ?? {}).map((version) => ({
version,
Expand Down
4 changes: 1 addition & 3 deletions lib/modules/datasource/helm/common.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import { findSourceUrl } from './common';
import type { HelmRepository } from './types';

// Truncated index.yaml file
const repo = parseSingleYaml<HelmRepository>(Fixtures.get('sample.yaml'), {
json: true,
});
const repo = parseSingleYaml<HelmRepository>(Fixtures.get('sample.yaml'));

describe('modules/datasource/helm/common', () => {
describe('findSourceUrl', () => {
Expand Down
4 changes: 1 addition & 3 deletions lib/modules/datasource/helm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ export class HelmDatasource extends Datasource {
}
try {
// TODO: use schema (#9610)
const doc = parseSingleYaml<HelmRepository>(res.body, {
json: true,
});
const doc = parseSingleYaml<HelmRepository>(res.body);
if (!is.plainObject<HelmRepository>(doc)) {
logger.warn(
{ helmRepository },
Expand Down
16 changes: 5 additions & 11 deletions lib/modules/manager/argocd/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,11 @@ export function extractPackageFile(
return null;
}

let definitions: ApplicationDefinition[];
try {
definitions = parseYaml(content, {
customSchema: ApplicationDefinition,
failureBehaviour: 'filter',
removeTemplates: true,
});
} catch (err) {
logger.debug({ err, packageFile }, 'Failed to parse ArgoCD definition.');
return null;
}
const definitions = parseYaml(content, {
customSchema: ApplicationDefinition,
failureBehaviour: 'filter',
removeTemplates: true,
});

const deps = definitions.flatMap(processAppSpec);

Expand Down
6 changes: 2 additions & 4 deletions lib/modules/manager/crossplane/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ describe('modules/manager/crossplane/extract', () => {
expect(extractPackageFile('nothing here', 'packages.yml')).toBeNull();
});

it('returns null for invalid', () => {
expect(
extractPackageFile(`${malformedPackages}\n123`, 'packages.yml'),
).toBeNull();
it('strips invalid templates', () => {
expect(extractPackageFile(`test: test: 123`, 'packages.yml')).toBeNull();
});

it('return null for kubernetes manifest', () => {
Expand Down
20 changes: 6 additions & 14 deletions lib/modules/manager/crossplane/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
PackageDependency,
PackageFileContent,
} from '../types';
import { type XPKG, XPKGSchema } from './schema';
import { XPKGSchema } from './schema';

export function extractPackageFile(
content: string,
Expand All @@ -19,19 +19,11 @@ export function extractPackageFile(
return null;
}

let list: XPKG[] = [];
try {
list = parseYaml(content, {
customSchema: XPKGSchema,
failureBehaviour: 'filter',
});
} catch (err) {
logger.debug(
{ err, packageFile },
'Failed to parse Crossplane package file.',
);
return null;
}
// not try and catching this as failureBehaviour is set to filter and therefore it will not throw
const list = parseYaml(content, {
customSchema: XPKGSchema,
failureBehaviour: 'filter',
});

const deps: PackageDependency[] = [];
for (const xpkg of list) {
Expand Down
1 change: 0 additions & 1 deletion lib/modules/manager/docker-compose/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export function extractPackageFile(
let config: DockerComposeFile;
try {
config = parseSingleYaml(content, {
json: true,
customSchema: DockerComposeFile,
});
} catch (err) {
Expand Down
37 changes: 15 additions & 22 deletions lib/modules/manager/fleet/extract.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import is from '@sindresorhus/is';
import { logger } from '../../../logger';
import { regEx } from '../../../util/regex';
import { parseYaml } from '../../../util/yaml';
import { GitTagsDatasource } from '../../datasource/git-tags';
Expand Down Expand Up @@ -137,27 +136,21 @@ export function extractPackageFile(
}
const deps: PackageDependency[] = [];

try {
if (regEx('fleet.ya?ml').test(packageFile)) {
const docs = parseYaml(content, {
json: true,
customSchema: FleetFile,
failureBehaviour: 'filter',
});
const fleetDeps = docs.flatMap(extractFleetFile);

deps.push(...fleetDeps);
} else {
const docs = parseYaml(content, {
json: true,
customSchema: GitRepo,
failureBehaviour: 'filter',
});
const gitRepoDeps = docs.flatMap(extractGitRepo);
deps.push(...gitRepoDeps);
}
} catch (err) {
logger.debug({ error: err, packageFile }, 'Failed to parse fleet YAML');
if (regEx('fleet.ya?ml').test(packageFile)) {
const docs = parseYaml(content, {
customSchema: FleetFile,
failureBehaviour: 'filter',
});
const fleetDeps = docs.flatMap(extractFleetFile);

deps.push(...fleetDeps);
} else {
const docs = parseYaml(content, {
customSchema: GitRepo,
failureBehaviour: 'filter',
});
const gitRepoDeps = docs.flatMap(extractGitRepo);
deps.push(...gitRepoDeps);
}

return deps.length ? { deps } : null;
Expand Down
23 changes: 8 additions & 15 deletions lib/modules/manager/flux/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,14 @@ function readManifest(
};
}

try {
const manifest: FluxManifest = {
kind: 'resource',
file: packageFile,
resources: parseYaml(content, {
json: true,
customSchema: FluxResource,
failureBehaviour: 'filter',
}),
};
return manifest;
} catch (err) {
logger.debug({ err, packageFile }, 'Failed to parse Flux manifest');
return null;
}
return {
kind: 'resource',
file: packageFile,
resources: parseYaml(content, {
customSchema: FluxResource,
failureBehaviour: 'filter',
}),
};
}

const githubUrlRegex = regEx(
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/manager/github-actions/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ function extractWithYAMLParser(
let pkg: Workflow;
try {
// TODO: use schema (#9610)
pkg = parseSingleYaml(content, { json: true });
pkg = parseSingleYaml(content);
} catch (err) {
logger.debug(
{ packageFile, err },
Expand Down
4 changes: 1 addition & 3 deletions lib/modules/manager/gitlabci-include/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ export function extractPackageFile(
const endpoint = GlobalConfig.get('endpoint');
try {
// TODO: use schema (#9610)
const doc = parseSingleYaml<GitlabPipeline>(replaceReferenceTags(content), {
json: true,
});
const doc = parseSingleYaml<GitlabPipeline>(replaceReferenceTags(content));
const includes = getAllIncludeProjects(doc);
for (const includeObj of includes) {
const dep = extractDepFromIncludeFile(includeObj);
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/manager/gitlabci/__fixtures__/gitlab-ci.4.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
include:
- local: 'lib/modules/manager/gitlabci/__fixtures__/include.yml'

test:
test: test:
script:
- !abc [.setup, script]
- echo running my own command
10 changes: 5 additions & 5 deletions lib/modules/manager/gitlabci/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import type {
Job,
Services,
} from './types';
import { getGitlabDep, replaceReferenceTags } from './utils';
import { getGitlabDep } from './utils';

// See https://docs.gitlab.com/ee/ci/components/index.html#use-a-component
const componentReferenceRegex = regEx(
Expand Down Expand Up @@ -170,8 +170,8 @@ export function extractPackageFile(
let deps: PackageDependency[] = [];
try {
// TODO: use schema (#9610)
const docs = parseYaml<GitlabPipeline>(replaceReferenceTags(content), {
json: true,
const docs = parseYaml<GitlabPipeline>(content, {
uniqueKeys: false,
});
for (const doc of docs) {
if (is.object(doc)) {
Expand Down Expand Up @@ -259,8 +259,8 @@ export async function extractAllPackageFiles(
let docs: GitlabPipeline[];
try {
// TODO: use schema (#9610)
docs = parseYaml(replaceReferenceTags(content), {
json: true,
docs = parseYaml(content, {
uniqueKeys: false,
});
} catch (err) {
logger.debug(
Expand Down
1 change: 0 additions & 1 deletion lib/modules/manager/glasskube/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ function parseResources(
packageFile: string,
): GlasskubeResources {
const resources: GlasskubeResource[] = parseYaml(content, {
json: true,
customSchema: GlasskubeResource,
failureBehaviour: 'filter',
});
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/manager/helm-requirements/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function extractPackageFile(
// TODO: fix type
let doc: any;
try {
doc = parseSingleYaml(content, { json: true }); // TODO #9610
doc = parseSingleYaml(content); // TODO #9610
} catch {
logger.debug({ packageFile }, `Failed to parse helm requirements.yaml`);
return null;
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/manager/helm-values/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function extractPackageFile(
// a parser that allows extracting line numbers would be preferable, with
// the current approach we need to match anything we find again during the update
// TODO: fix me (#9610)
parsedContent = parseYaml(content, { json: true }) as any;
parsedContent = parseYaml(content) as any;
} catch (err) {
logger.debug({ err, packageFile }, 'Failed to parse helm-values YAML');
return null;
Expand Down
20 changes: 5 additions & 15 deletions lib/modules/manager/helmfile/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,14 @@ export async function extractPackageFile(
config: ExtractConfig,
): Promise<PackageFileContent | null> {
const deps: PackageDependency[] = [];
let docs: Doc[];
let registryData: Record<string, HelmRepository> = {};
// Record kustomization usage for all deps, since updating artifacts is run on the helmfile.yaml as a whole.
let needKustomize = false;
try {
docs = parseYaml(content, {
customSchema: documentSchema,
failureBehaviour: 'filter',
removeTemplates: true,
json: true,
});
} catch (err) {
logger.debug(
{ err, packageFile },
'Failed to parse helmfile helmfile.yaml',
);
return null;
}
const docs: Doc[] = parseYaml(content, {
customSchema: documentSchema,
failureBehaviour: 'filter',
removeTemplates: true,
});

for (const doc of docs) {
// Always check for repositories in the current document and override the existing ones if any (as YAML does)
Expand Down
4 changes: 1 addition & 3 deletions lib/modules/manager/helmsman/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ export function extractPackageFile(
): PackageFileContent | null {
try {
// TODO: use schema (#9610)
const doc = parseSingleYaml<HelmsmanDocument>(content, {
json: true,
});
const doc = parseSingleYaml<HelmsmanDocument>(content);
if (!doc.apps) {
logger.debug({ packageFile }, `Missing apps keys`);
return null;
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/manager/helmv3/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function extractPackageFile(
};
try {
// TODO: use schema (#9610)
chart = parseSingleYaml(content, { json: true });
chart = parseSingleYaml(content);
if (!(chart?.apiVersion && chart.name && chart.version)) {
logger.debug(
{ packageFile },
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/manager/jenkins/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function extractYaml(

try {
// TODO: use schema (#9610)
const doc = parseSingleYaml<JenkinsPlugins>(content, { json: true });
const doc = parseSingleYaml<JenkinsPlugins>(content);
if (is.nonEmptyArray(doc?.plugins)) {
for (const plugin of doc.plugins) {
if (plugin.artifactId) {
Expand Down
2 changes: 0 additions & 2 deletions lib/modules/manager/kubernetes/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ function extractApis(
try {
// TODO: use schema (#9610)
doc = parseYaml(content, {
filename: packageFile,
removeTemplates: true,
json: true,
});
} catch (err) {
logger.debug({ err, packageFile }, 'Failed to parse Kubernetes manifest.');
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/manager/kustomize/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export function parseKustomize(
let pkg: Kustomize | null = null;
try {
// TODO: use schema (#9610)
pkg = parseSingleYaml(content, { json: true });
pkg = parseSingleYaml(content);
} catch /* istanbul ignore next */ {
logger.debug({ packageFile }, 'Error parsing kustomize file');
return null;
Expand Down
3 changes: 0 additions & 3 deletions lib/modules/manager/npm/extract/pnpm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ export async function extractPnpmFilters(
// TODO: use schema (#9610,#22198)
const contents = parseSingleYaml<PnpmWorkspaceFile>(
(await readLocalFile(fileName, 'utf8'))!,
{
json: true,
},
);
if (
!Array.isArray(contents.packages) ||
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/manager/pre-commit/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export function extractPackageFile(
let parsedContent: ParsedContent;
try {
// TODO: use schema (#9610)
parsedContent = parseSingleYaml(content, { json: true });
parsedContent = parseSingleYaml(content);
} catch (err) {
logger.debug(
{ filename: packageFile, err },
Expand Down
Loading
Loading