-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathmetadataApiDeploy.ts
More file actions
525 lines (484 loc) · 20.6 KB
/
metadataApiDeploy.ts
File metadata and controls
525 lines (484 loc) · 20.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
* Copyright (c) 2021, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { join, relative, resolve as pathResolve, sep } from 'node:path';
import { format } from 'node:util';
import { isString } from '@salesforce/ts-types';
import JSZip from 'jszip';
import fs from 'graceful-fs';
import { Lifecycle, Messages, SfError, envVars } from '@salesforce/core';
import { ensureArray } from '@salesforce/kit';
import { RegistryAccess } from '../registry/registryAccess';
import { ReplacementEvent } from '../convert/types';
import { MetadataConverter } from '../convert/metadataConverter';
import { ComponentSet } from '../collections/componentSet';
import { MetadataTransfer, MetadataTransferOptions } from './metadataTransfer';
import {
AsyncResult,
ComponentStatus,
DeployMessage,
FileResponse,
MetadataApiDeployOptions as ApiOptions,
MetadataApiDeployStatus,
MetadataTransferResult,
} from './types';
import {
createResponses,
getDeployMessages,
getState,
isComponentNotFoundWarningMessage,
toKey,
} from './deployMessages';
Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/source-deploy-retrieve', 'sdr');
// TODO: (NEXT MAJOR) this should just be a readonly object and not a class.
export class DeployResult implements MetadataTransferResult {
private fileResponses?: FileResponse[];
public constructor(
public readonly response: MetadataApiDeployStatus,
public readonly components?: ComponentSet,
public readonly replacements = new Map<string, string[]>(),
public readonly zipMeta?: { zipSize: number; zipFileCount?: number }
) {}
public getFileResponses(): FileResponse[] {
// this involves FS operations, so only perform once!
if (!this.fileResponses) {
this.fileResponses = [
// removes duplicates from the file responses by parsing the object into a string, used as the key of the map
...new Map(
(this.components
? buildFileResponsesFromComponentSet(this.components)(this.response)
: buildFileResponses(this.response)
).map((v) => [JSON.stringify(v), v])
).values(),
];
}
return this.fileResponses;
}
}
export type MetadataApiDeployOptions = {
apiOptions?: ApiOptions;
/**
* Path to a zip file containing mdapi-formatted code and a package.xml
*/
zipPath?: string;
/**
* Path to a directory containing mdapi-formatted code and a package.xml
*/
mdapiPath?: string;
registry?: RegistryAccess;
} & MetadataTransferOptions;
export class MetadataApiDeploy extends MetadataTransfer<
MetadataApiDeployStatus,
DeployResult,
MetadataApiDeployOptions
> {
public static readonly DEFAULT_OPTIONS: Partial<MetadataApiDeployOptions> = {
apiOptions: {
rollbackOnError: true,
ignoreWarnings: false,
checkOnly: false,
singlePackage: true,
rest: false,
},
};
private options: MetadataApiDeployOptions;
private replacements: Map<string, Set<string>> = new Map();
private orgId?: string;
// Keep track of rest deploys separately since Connection.deploy() removes it
// from the apiOptions and we need it for telemetry.
private readonly isRestDeploy: boolean;
private readonly registry: RegistryAccess;
private zipSize?: number;
private zipFileCount?: number;
public constructor(options: MetadataApiDeployOptions) {
super(options);
options.apiOptions = { ...MetadataApiDeploy.DEFAULT_OPTIONS.apiOptions, ...options.apiOptions };
this.options = Object.assign({}, options);
this.isRestDeploy = !!options.apiOptions?.rest;
this.registry = options.registry ?? new RegistryAccess();
if (this.mdapiTempDir) {
this.mdapiTempDir = join(this.mdapiTempDir, `${new Date().toISOString()}_deploy`);
}
}
/**
* Deploy recently validated components without running Apex tests. Requires the operation to have been
* created with the `{ checkOnly: true }` API option.
*
* Ensure that the following requirements are met before deploying a recent validation:
* - The components have been validated successfully for the target environment within the last 10 days.
* - As part of the validation, Apex tests in the target org have passed.
* - Code coverage requirements are met.
* - If all tests in the org or all local tests are run, overall code coverage is at least 75%, and Apex triggers have some coverage.
* - If specific tests are run with the RunSpecifiedTests test level, each class and trigger that was deployed is covered by at least 75% individually.
*
* See [deployRecentValidation()](https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_deployRecentValidation.htm)
*
* @param rest - Set to `true` to use the REST API, otherwise defaults to using SOAP
* @returns The ID of the quick deployment
*/
public async deployRecentValidation(rest = false): Promise<string> {
if (!this.id) {
throw new SfError(messages.getMessage('error_no_job_id', ['deploy']), 'MissingJobIdError');
}
const conn = await this.getConnection();
const response = (await conn.metadata.deployRecentValidation({
id: this.id,
rest,
})) as unknown as AsyncResult | string;
return isString(response) ? response : response.id;
}
/**
* Check the status of the deploy operation.
*
* @returns Status of the deploy
*/
public async checkStatus(): Promise<MetadataApiDeployStatus> {
if (!this.id) {
throw new SfError(messages.getMessage('error_no_job_id', ['deploy']), 'MissingJobIdError');
}
const connection = await this.getConnection();
// Recasting to use the project's version of the type
return (await connection.metadata.checkDeployStatus(this.id, true)) as unknown as MetadataApiDeployStatus;
}
/**
* Cancel the deploy operation.
*
* Deploys are asynchronously canceled. Once the cancel request is made to the org,
* check the status of the cancellation with `checkStatus`.
*/
public async cancel(): Promise<void> {
if (!this.id) {
throw new SfError(messages.getMessage('error_no_job_id', ['deploy']), 'MissingJobIdError');
}
const connection = await this.getConnection();
await connection.metadata.cancelDeploy(this.id);
}
protected async pre(): Promise<AsyncResult> {
const LifecycleInstance = Lifecycle.getInstance();
const connection = await this.getConnection();
const apiVersion = connection.getApiVersion();
// store for use in the scopedPostDeploy event
this.orgId = connection.getAuthInfoFields().orgId;
// If we have a ComponentSet but no version info, use the apiVersion from the Connection.
if (this.components) {
// this is the SOAP/REST API version of the connection
this.components.apiVersion ??= apiVersion;
// this is used as the version in the manifest (package.xml).
this.components.sourceApiVersion ??= apiVersion;
}
// only do event hooks if source, (NOT a metadata format) deploy
if (this.options.components) {
await LifecycleInstance.emit('scopedPreDeploy', {
componentSet: this.options.components,
orgId: this.orgId,
} as ScopedPreDeploy);
}
LifecycleInstance.on(
'replacement',
async (replacement: ReplacementEvent) =>
// lifecycle have to be async, so wrapped in a promise
new Promise((resolve) => {
if (!this.replacements.has(replacement.filename)) {
this.replacements.set(replacement.filename, new Set([replacement.replaced]));
} else {
this.replacements.get(replacement.filename)?.add(replacement.replaced);
}
resolve();
})
);
const [{ zipBuffer, zipFileCount }] = await Promise.all([
this.getZipBuffer(),
this.maybeSaveTempDirectory('metadata'),
]);
// SDR modifies what the mdapi expects by adding a rest param
const { rest, ...optionsWithoutRest } = this.options.apiOptions ?? {};
// Event and Debug output for API version and source API version used for deploy
const manifestVersion = this.components?.sourceApiVersion;
const webService = rest ? 'REST' : 'SOAP';
const manifestMsg = manifestVersion ? ` in v${manifestVersion} shape` : '';
const debugMsg = format(`Deploying metadata source%s using ${webService} v${apiVersion}`, manifestMsg);
this.logger.debug(debugMsg);
// Event and Debug output for the zip file used for deploy
this.zipSize = zipBuffer.byteLength;
let zipMessage = `Deployment zip file size = ${this.zipSize} Bytes`;
if (zipFileCount) {
this.zipFileCount = zipFileCount;
zipMessage += ` containing ${zipFileCount} files`;
}
this.logger.debug(zipMessage);
await LifecycleInstance.emit('apiVersionDeploy', { webService, manifestVersion, apiVersion });
await LifecycleInstance.emit('deployZipData', { zipSize: this.zipSize, zipFileCount });
await this.warnIfDeployThresholdExceeded(this.zipSize, zipFileCount);
return this.isRestDeploy
? connection.metadata.deployRest(zipBuffer, optionsWithoutRest)
: connection.metadata.deploy(zipBuffer, optionsWithoutRest);
}
protected async post(result: MetadataApiDeployStatus): Promise<DeployResult> {
const lifecycle = Lifecycle.getInstance();
const connection = await this.getConnection();
try {
const apiVersion = connection.getApiVersion();
// Creates an array of unique metadata types that were deployed, uses Set to avoid duplicates.
let listOfMetadataTypesDeployed: string[];
if (this.options.components) {
listOfMetadataTypesDeployed = Array.from(new Set(this.options.components.map((c) => c.type.name)));
} else {
// mdapi deploys don't have a ComponentSet, so using the result
const types = new Set<string>();
const successes = ensureArray(result.details?.componentSuccesses);
const failures = ensureArray(result.details?.componentFailures);
[...successes, ...failures].forEach((c) => c.componentType && types.add(c.componentType));
listOfMetadataTypesDeployed = Array.from(types);
}
void lifecycle.emitTelemetry({
eventName: 'metadata_api_deploy_result',
library: 'SDR',
status: result.status,
apiVersion,
sourceApiVersion: this.components?.sourceApiVersion,
createdDate: result.createdDate,
startDate: result.startDate,
completedDate: result.completedDate,
rollbackOnError: result.rollbackOnError,
runTestsEnabled: result.runTestsEnabled,
isRestDeploy: this.isRestDeploy,
checkOnly: result.checkOnly,
done: result.done,
ignoreWarnings: result.ignoreWarnings,
metadataTypesDeployed: listOfMetadataTypesDeployed.toString(),
numberComponentErrors: result.numberComponentErrors,
numberComponentsDeployed: result.numberComponentsDeployed,
numberComponentsTotal: result.numberComponentsTotal,
numberTestErrors: result.numberTestErrors,
numberTestsCompleted: result.numberTestsCompleted,
numberTestsTotal: result.numberTestsTotal,
testsTotalTime: result.details?.runTestResult?.totalTime,
filesWithReplacementsQuantity: this.replacements.size ?? 0,
zipSize: this.zipSize ?? 0,
zipFileCount: this.zipFileCount ?? 0,
});
} catch (err) {
const error = err as Error;
this.logger.debug(
`Error trying to compile/send deploy telemetry data for deploy ID: ${this.id ?? '<not provided>'}\nError: ${
error.message
}`
);
}
const deployResult = new DeployResult(
result,
this.components,
new Map(Array.from(this.replacements).map(([k, v]) => [k, Array.from(v)])),
{ zipSize: this.zipSize ?? 0, zipFileCount: this.zipFileCount }
);
// only do event hooks if source, (NOT a metadata format) deploy
if (this.options.components) {
// this may not be set if you resume a deploy so that `pre` is skipped.
this.orgId ??= connection.getAuthInfoFields().orgId;
// previous step ensures string exists
if (this.orgId) {
await lifecycle.emit<ScopedPostDeploy>('scopedPostDeploy', { deployResult, orgId: this.orgId });
}
}
return deployResult;
}
// By default, an 80% deploy size threshold is used to warn users when their deploy size
// is approaching the limit enforced by the Metadata API. This includes the number of files
// being deployed as well as the byte size of the deployment. The threshold can be overridden
// to be a different percentage using the SF_DEPLOY_SIZE_THRESHOLD env var. An env var value
// of 100 would disable the client side warning. An env var value of 0 would always warn.
private async warnIfDeployThresholdExceeded(zipSize: number, zipFileCount: number | undefined): Promise<void> {
const thresholdPercentage = Math.abs(envVars.getNumber('SF_DEPLOY_SIZE_THRESHOLD', 80));
if (thresholdPercentage >= 100) {
this.logger.debug(
`Deploy size warning is disabled since SF_DEPLOY_SIZE_THRESHOLD is overridden to: ${thresholdPercentage}`
);
return;
}
if (thresholdPercentage !== 80) {
this.logger.debug(
`Deploy size warning threshold has been overridden by SF_DEPLOY_SIZE_THRESHOLD to: ${thresholdPercentage}`
);
}
// 39_000_000 is 39 MB in decimal format, which is the format used in buffer.byteLength
const fileSizeThreshold = Math.round(39_000_000 * (thresholdPercentage / 100));
const fileCountThreshold = Math.round(10_000 * (thresholdPercentage / 100));
if (zipSize > fileSizeThreshold) {
await Lifecycle.getInstance().emitWarning(
`Deployment zip file size is approaching the Metadata API limit (~39MB). Warning threshold is ${thresholdPercentage}% and size ${zipSize} > ${fileSizeThreshold}`
);
}
if (zipFileCount && zipFileCount > fileCountThreshold) {
await Lifecycle.getInstance().emitWarning(
`Deployment zip file count is approaching the Metadata API limit (10,000). Warning threshold is ${thresholdPercentage}% and count ${zipFileCount} > ${fileCountThreshold}`
);
}
}
private async getZipBuffer(): Promise<{ zipBuffer: Buffer; zipFileCount?: number }> {
const mdapiPath = this.options.mdapiPath;
// Zip a directory of metadata format source
if (mdapiPath) {
if (!fs.existsSync(mdapiPath) || !fs.lstatSync(mdapiPath).isDirectory()) {
throw messages.createError('error_directory_not_found_or_not_directory', [mdapiPath]);
}
const zip = JSZip();
let zipFileCount = 0;
const zipDirRecursive = (dir: string): void => {
const dirents = fs.readdirSync(dir, { withFileTypes: true });
for (const dirent of dirents) {
const fullPath = pathResolve(dir, dirent.name);
if (dirent.isDirectory()) {
zipDirRecursive(fullPath);
} else {
// Add relative file paths to a root of "zip" for MDAPI.
const relPath = join('zip', relative(mdapiPath, fullPath));
// Ensure only posix paths are added to zip files
const relPosixPath = relPath.replace(/\\/g, '/');
zip.file(relPosixPath, fs.createReadStream(fullPath));
zipFileCount++;
}
}
};
this.logger.debug(`Zipping directory for metadata deploy: ${mdapiPath}`);
zipDirRecursive(mdapiPath);
return {
zipBuffer: await zip.generateAsync({
type: 'nodebuffer',
compression: 'DEFLATE',
compressionOptions: { level: 9 },
}),
zipFileCount,
};
}
// Read a zip of metadata format source into a buffer
if (this.options.zipPath) {
if (!fs.existsSync(this.options.zipPath)) {
throw new SfError(messages.getMessage('error_path_not_found', [this.options.zipPath]));
}
// does encoding matter for zip files? I don't know
return { zipBuffer: await fs.promises.readFile(this.options.zipPath) };
}
// Convert a ComponentSet of metadata in source format and zip
if (this.options.components && this.components) {
const converter = new MetadataConverter(this.registry);
const { zipBuffer, zipFileCount } = await converter.convert(this.components, 'metadata', { type: 'zip' });
if (!zipBuffer) {
throw new SfError(messages.getMessage('zipBufferError'));
}
return { zipBuffer, zipFileCount };
}
throw new Error('Options should include components, zipPath, or mdapiPath');
}
}
/**
* If a component fails to delete because it doesn't exist in the org, you get a message like
* key: 'ApexClass#destructiveChanges.xml'
* value:[{
* fullName: 'destructiveChanges.xml',
* fileName: 'destructiveChanges.xml',
* componentType: 'ApexClass',
* problem: 'No ApexClass named: test1 found',
* problemType: 'Warning'
* }]
*/
const deleteNotFoundToFileResponses =
(cs: ComponentSet) =>
(messageMap: Map<string, DeployMessage[]>): FileResponse[] =>
Array.from(messageMap)
.filter(([key]) => key.includes('destructiveChanges') && key.endsWith('.xml'))
.flatMap(
([, messageArray]): Array<DeployMessage & Required<Pick<DeployMessage, 'componentType' | 'problem'>>> =>
messageArray.filter(isComponentNotFoundWarningMessage)
)
.flatMap((message) => {
const fullName = message.problem.replace(`No ${message.componentType} named: `, '').replace(' found', '');
return cs
? cs.getComponentFilenamesByNameAndType({ fullName, type: message.componentType }).map((fileName) => ({
fullName,
type: message.componentType,
filePath: fileName,
state: ComponentStatus.Deleted,
}))
: [];
});
const warnIfUnmatchedServerResult =
(fr: FileResponse[]) =>
(messageMap: Map<string, DeployMessage[]>): void[] =>
// keep the parents and children separated for MPD scenarios where we have a parent in one, children in another package
[...messageMap.keys()].flatMap((key) => {
const [type, fullName] = key.split('#');
if (
!fr.find((c) => c.type === type && c.fullName === fullName) &&
!['package.xml', 'destructiveChanges.xml', 'destructiveChangesPost.xml', 'destructiveChangesPre.xml'].includes(
fullName
)
) {
const deployMessage = messageMap.get(key)!.at(0)!;
// warn that this component is found in server response, but not in component set
void Lifecycle.getInstance().emitWarning(
`${deployMessage.componentType ?? '<no component type in deploy message>'}, ${
deployMessage.fullName
}, returned from org, but not found in the local project`
);
}
});
const buildFileResponses = (response: MetadataApiDeployStatus): FileResponse[] =>
ensureArray(response.details?.componentSuccesses)
.concat(ensureArray(response.details?.componentFailures))
.filter((c) => c.fullName !== 'package.xml')
.map(
(c) =>
({
...(getState(c) === ComponentStatus.Failed
? {
error: c.problem,
problemType: c.problemType,
columnNumber: c.columnNumber ? parseInt(c.columnNumber, 10) : undefined,
lineNumber: c.lineNumber ? parseInt(c.lineNumber, 10) : undefined,
}
: {}),
fullName: c.fullName,
type: c.componentType,
state: getState(c),
filePath: c.fileName.replace(`zip${sep}`, ''),
} as FileResponse)
);
const buildFileResponsesFromComponentSet =
(cs: ComponentSet) =>
(response: MetadataApiDeployStatus): FileResponse[] => {
const responseMessages = getDeployMessages(response);
const fileResponses = (cs.getSourceComponents().toArray() ?? [])
.flatMap((deployedComponent) =>
createResponses(deployedComponent, responseMessages.get(toKey(deployedComponent)) ?? []).concat(
deployedComponent.type.children
? deployedComponent.getChildren().flatMap((child) => {
const childMessages = responseMessages.get(toKey(child));
return childMessages ? createResponses(child, childMessages) : [];
})
: []
)
)
.concat(deleteNotFoundToFileResponses(cs)(responseMessages));
if (cs.size) {
warnIfUnmatchedServerResult(fileResponses)(responseMessages);
}
return fileResponses;
};
/**
* register a listener to `scopedPreDeploy`
*/
export type ScopedPreDeploy = {
componentSet: ComponentSet;
orgId: string;
};
/**
* register a listener to `scopedPostDeploy`
*/
export type ScopedPostDeploy = {
deployResult: DeployResult;
orgId: string;
};