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

feat: Clone objects via wrapper #22970

Merged
merged 15 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
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: 3 additions & 2 deletions lib/config/massage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import is from '@sindresorhus/is';
import { clone } from '../util/clone';
import { getOptions } from './options';
import type { PackageRule, RenovateConfig, UpdateType } from './types';

Expand All @@ -16,7 +17,7 @@ export function massageConfig(config: RenovateConfig): RenovateConfig {
}
});
}
const massagedConfig = structuredClone(config);
const massagedConfig = clone(config);
for (const [key, val] of Object.entries(config)) {
if (allowedStrings.includes(key) && is.string(val)) {
massagedConfig[key] = [val];
Expand Down Expand Up @@ -55,7 +56,7 @@ export function massageConfig(config: RenovateConfig): RenovateConfig {
PackageRule
][]) {
if (updateTypes.includes(key)) {
let newRule = structuredClone(rule);
let newRule = clone(rule);
Object.keys(newRule).forEach((newKey) => {
if (!(newKey.startsWith(`match`) || newKey.startsWith('exclude'))) {
delete newRule[newKey];
Expand Down
3 changes: 2 additions & 1 deletion lib/config/presets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
import { logger } from '../../logger';
import { ExternalHostError } from '../../types/errors/external-host-error';
import * as memCache from '../../util/cache/memory';
import { clone } from '../../util/clone';
import { regEx } from '../../util/regex';
import * as massage from '../massage';
import * as migration from '../migration';
Expand Down Expand Up @@ -273,7 +274,7 @@ export async function resolveConfigPresets(
_ignorePresets?: string[],
existingPresets: string[] = []
): Promise<AllConfig> {
let ignorePresets = structuredClone(_ignorePresets);
let ignorePresets = clone(_ignorePresets);
if (!ignorePresets || ignorePresets.length === 0) {
ignorePresets = inputConfig.ignorePresets ?? [];
}
Expand Down
5 changes: 3 additions & 2 deletions lib/config/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { logger } from '../logger';
import { clone } from '../util/clone';
import { getHighestVulnerabilitySeverity } from '../util/vulnerability/utils';
import * as options from './options';
import type { RenovateConfig } from './types';
Expand All @@ -11,8 +12,8 @@ export function mergeChildConfig<
if (!child) {
return parent as never;
}
const parentConfig = structuredClone(parent);
const childConfig = structuredClone(child);
const parentConfig = clone(parent);
const childConfig = clone(child);
const config: Record<string, any> = { ...parentConfig, ...childConfig };

// Ensure highest severity survives parent / child merge
Expand Down
11 changes: 5 additions & 6 deletions lib/logger/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import bunyan from 'bunyan';
import fs from 'fs-extra';
import { RequestError as HttpError } from 'got';
import { ZodError } from 'zod';
import { clone } from '../util/clone';
import { redactedFields, sanitize } from '../util/sanitize';
import type { BunyanRecord, BunyanStream } from './types';

Expand Down Expand Up @@ -132,7 +133,7 @@ export default function prepareError(err: Error): Record<string, unknown> {
// handle got error
if (err instanceof HttpError) {
const options: Record<string, unknown> = {
headers: structuredClone(err.options.headers),
headers: clone(err.options.headers),
url: err.options.url?.toString(),
hostType: err.options.context.hostType,
};
Expand All @@ -149,11 +150,9 @@ export default function prepareError(err: Error): Record<string, unknown> {
statusCode: err.response?.statusCode,
statusMessage: err.response?.statusMessage,
body:
// istanbul ignore if: not easily testable
err.name === 'TimeoutError'
? undefined
: structuredClone(err.response.body),
headers: structuredClone(err.response.headers),
// istanbul ignore next: not easily testable
err.name === 'TimeoutError' ? undefined : clone(err.response.body),
headers: clone(err.response.headers),
httpVersion: err.response.httpVersion,
retryCount: err.response.retryCount,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ exports[`modules/datasource/cdnjs/index getReleases filters releases by asset pr
"registryUrl": "https://api.cdnjs.com/",
"releases": [
{
"newDigest": undefined,
"version": "0.7.5",
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@ exports[`modules/datasource/galaxy-collection/index getReleases returns only val
"version": "1.2.0",
},
],
"tags": undefined,
}
`;
2 changes: 1 addition & 1 deletion lib/modules/datasource/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ export async function getPkgReleases(
logger.error({ config }, 'Datasource getReleases without packageName');
return null;
}
let res: ReleaseResult;
let res: ReleaseResult | null = null;
try {
res = clone(
await getRawReleases({
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/datasource/jenkins-plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class JenkinsPluginsDatasource extends Datasource {
return null;
}

const result = structuredClone(plugin);
const result = clone(plugin);
const versions = await this.getJenkinsPluginVersions();
const releases = versions[packageName];
result.releases = releases ? clone(releases) : [];
Expand Down
72 changes: 72 additions & 0 deletions lib/modules/datasource/npm/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ exports[`modules/datasource/npm/index should fetch package info from custom regi
"registryUrl": "https://npm.mycustomregistry.com",
"releases": [
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-06T05:21:53.000Z",
"version": "0.0.1",
},
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-07T05:21:53.000Z",
"version": "0.0.2",
},
Expand All @@ -28,10 +34,16 @@ exports[`modules/datasource/npm/index should fetch package info from npm 1`] = `
"registryUrl": "https://registry.npmjs.org",
"releases": [
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-06T05:21:53.000Z",
"version": "0.0.1",
},
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-07T05:21:53.000Z",
"version": "0.0.2",
},
Expand All @@ -50,10 +62,16 @@ exports[`modules/datasource/npm/index should handle foobar 1`] = `
"registryUrl": "https://registry.npmjs.org",
"releases": [
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-06T05:21:53.000Z",
"version": "0.0.1",
},
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-07T05:21:53.000Z",
"version": "0.0.2",
},
Expand All @@ -72,10 +90,16 @@ exports[`modules/datasource/npm/index should handle no time 1`] = `
"registryUrl": "https://registry.npmjs.org",
"releases": [
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-06T05:21:53.000Z",
"version": "0.0.1",
},
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"version": "0.0.2",
},
],
Expand All @@ -93,10 +117,16 @@ exports[`modules/datasource/npm/index should not send an authorization header if
"registryUrl": "https://registry.npmjs.org",
"releases": [
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-06T05:21:53.000Z",
"version": "0.0.1",
},
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-07T05:21:53.000Z",
"version": "0.0.2",
},
Expand All @@ -115,6 +145,9 @@ exports[`modules/datasource/npm/index should parse repo url (string) 1`] = `
"registryUrl": "https://registry.npmjs.org",
"releases": [
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-06T05:21:53.000Z",
"version": "0.0.1",
},
Expand All @@ -132,6 +165,9 @@ exports[`modules/datasource/npm/index should parse repo url 1`] = `
"registryUrl": "https://registry.npmjs.org",
"releases": [
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-06T05:21:53.000Z",
"version": "0.0.1",
},
Expand All @@ -149,10 +185,16 @@ exports[`modules/datasource/npm/index should replace any environment variable in
"registryUrl": "https://registry.from-env.com",
"releases": [
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-06T05:21:53.000Z",
"version": "0.0.1",
},
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-07T05:21:53.000Z",
"version": "0.0.2",
},
Expand All @@ -176,10 +218,16 @@ Marking the latest version of an npm package as deprecated results in the entire
"registryUrl": "https://registry.npmjs.org",
"releases": [
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-06T05:21:53.000Z",
"version": "0.0.1",
},
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"isDeprecated": true,
"releaseTimestamp": "2018-05-07T05:21:53.000Z",
"version": "0.0.2",
Expand All @@ -206,10 +254,16 @@ exports[`modules/datasource/npm/index should send an authorization header if pro
"registryUrl": "https://registry.npmjs.org",
"releases": [
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-06T05:21:53.000Z",
"version": "0.0.1",
},
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-07T05:21:53.000Z",
"version": "0.0.2",
},
Expand All @@ -228,10 +282,16 @@ exports[`modules/datasource/npm/index should use default registry if missing fro
"registryUrl": "https://registry.npmjs.org",
"releases": [
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-06T05:21:53.000Z",
"version": "0.0.1",
},
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-07T05:21:53.000Z",
"version": "0.0.2",
},
Expand All @@ -250,10 +310,16 @@ exports[`modules/datasource/npm/index should use host rules by baseUrl if provid
"registryUrl": "https://npm.mycustomregistry.com/_packaging/mycustomregistry/npm/registry",
"releases": [
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-06T05:21:53.000Z",
"version": "0.0.1",
},
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-07T05:21:53.000Z",
"version": "0.0.2",
},
Expand All @@ -272,10 +338,16 @@ exports[`modules/datasource/npm/index should use host rules by hostName if provi
"registryUrl": "https://npm.mycustomregistry.com",
"releases": [
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-06T05:21:53.000Z",
"version": "0.0.1",
},
{
"dependencies": undefined,
"devDependencies": undefined,
"gitRef": undefined,
"releaseTimestamp": "2018-05-07T05:21:53.000Z",
"version": "0.0.2",
},
Expand Down
3 changes: 2 additions & 1 deletion lib/modules/manager/bazel-module/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import parseGithubUrl from 'github-url-from-git';
import { z } from 'zod';
import { logger } from '../../../logger';
import type { SkipReason } from '../../../types';
import { clone } from '../../../util/clone';
import { regEx } from '../../../util/regex';
import { BazelDatasource } from '../../datasource/bazel';
import { GithubTagsDatasource } from '../../datasource/github-tags';
Expand Down Expand Up @@ -50,7 +51,7 @@ function isMerge(value: BazelModulePackageDep): value is MergePackageDep {
export function bazelModulePackageDepToPackageDependency(
bmpd: BazelModulePackageDep
): PackageDependency {
const copy: BazelModulePackageDep = structuredClone(bmpd);
const copy: BazelModulePackageDep = clone(bmpd);
if (isOverride(copy)) {
const partial = copy as Partial<OverridePackageDep>;
delete partial.bazelDepSkipReason;
Expand Down
3 changes: 2 additions & 1 deletion lib/modules/manager/gradle/parser/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { lexer, parser, query as q } from 'good-enough-parser';
import { clone } from '../../../../util/clone';
import { regEx } from '../../../../util/regex';
import type {
Ctx,
Expand Down Expand Up @@ -51,7 +52,7 @@ export function reduceNestingDepth(ctx: Ctx): Ctx {
}

export function prependNestingDepth(ctx: Ctx): Ctx {
ctx.varTokens = [...structuredClone(ctx.tmpNestingDepth), ...ctx.varTokens];
ctx.varTokens = [...clone(ctx.tmpNestingDepth), ...ctx.varTokens];
return ctx;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { updateLockedDependency } from '../..';
import { Fixtures } from '../../../../../../test/fixtures';
import * as httpMock from '../../../../../../test/http-mock';
import { clone } from '../../../../../util/clone';
import type { UpdateLockedConfig } from '../../../types';

const packageFileContent = Fixtures.get('package.json', './package-lock');
Expand Down Expand Up @@ -113,7 +114,7 @@ describe('modules/manager/npm/update/locked-dependency/index', () => {
});

it('fails to remediate if parent dep cannot support', async () => {
const acceptsModified = structuredClone(acceptsJson);
const acceptsModified = clone(acceptsJson);
acceptsModified.versions['2.0.0'] = {};
httpMock
.scope('https://registry.npmjs.org')
Expand Down
13 changes: 0 additions & 13 deletions lib/util/__snapshots__/clone.spec.ts.snap

This file was deleted.

Loading