-
Notifications
You must be signed in to change notification settings - Fork 13
/
packageVersion.ts
658 lines (618 loc) · 23.5 KB
/
packageVersion.ts
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/*
* Copyright (c) 2020, 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 { Connection, Lifecycle, Messages, PollingClient, SfError, SfProject, StatusResult } from '@salesforce/core';
import { Duration, env } from '@salesforce/kit';
import { Optional } from '@salesforce/ts-types';
import {
PackageSaveResult,
PackageType,
PackageVersionCreateOptions,
PackageVersionCreateRequestQueryOptions,
PackageVersionCreateRequestResult,
PackageVersionEvents,
PackageVersionOptions,
PackageVersionReportResult,
PackageVersionUpdateOptions,
PackagingSObjects,
} from '../interfaces';
import {
applyErrorAction,
BY_LABEL,
combineSaveErrors,
massageErrorMessage,
validateId,
queryWithInConditionChunking,
} from '../utils/packageUtils';
import { PackageVersionCreate } from './packageVersionCreate';
import { getPackageVersionReport } from './packageVersionReport';
import { getCreatePackageVersionCreateRequestReport } from './packageVersionCreateRequestReport';
import { list } from './packageVersionCreateRequest';
import Package2 = PackagingSObjects.Package2;
import Package2VersionStatus = PackagingSObjects.Package2VersionStatus;
type Package2Version = PackagingSObjects.Package2Version;
Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/packaging', 'package_version');
export const Package2VersionFields: Array<keyof Package2Version> = [
'Id',
'IsDeleted',
'CreatedDate',
'CreatedById',
'LastModifiedDate',
'LastModifiedById',
'SystemModstamp',
'Package2Id',
'SubscriberPackageVersionId',
'Tag',
'Branch',
'AncestorId',
'ValidationSkipped',
'Name',
'Description',
'MajorVersion',
'MinorVersion',
'PatchVersion',
'BuildNumber',
'IsDeprecated',
'IsPasswordProtected',
'CodeCoverage',
'CodeCoveragePercentages',
'HasPassedCodeCoverageCheck',
'InstallKey',
'IsReleased',
'ConvertedFromVersionId',
'ReleaseVersion',
'BuildDurationInSeconds',
'HasMetadataRemoved',
];
export type Package2VersionFieldTypes = Array<(typeof Package2VersionFields)[number]>;
export type Package2VersionQueryOptions = {
/**
* The fields to include in the returned data. Defaults to all fields.
*/
fields?: Package2VersionFieldTypes;
/**
* The where clause to filter the query. E.g., "WHERE Id IN ('%IDS%')";
*/
whereClause?: string;
/**
* An array of where clause items to match. The query is chunked,meaning broken into
* multiple queries when the query length would exceed the maximum char limit.
* When defining items here, the `whereClause` argument must use this token for the
* item replacement: `'%IDS%'`.
*/
whereClauseItems?: string[];
/**
* The order-by clause for the query. Defaults to LastModifiedDate descending.
*/
orderBy?: string;
};
/**
* Provides the ability to create, update, delete, and promote 2nd
* generation package versions.
*
* **Examples**
*
* Create a new instance and get the ID (05i):
*
* `const id = new PackageVersion({connection, project, idOrAlias}).getId();`
*
* Create a new package version in the org:
*
* `const myPkgVersion = await PackageVersion.create(options, pollingOptions);`
*
* Promote a package version:
*
* `new PackageVersion({connection, project, idOrAlias}).promote();`
*/
export class PackageVersion {
private readonly project: SfProject;
private readonly connection: Connection;
private data?: Package2Version;
private packageType: Optional<PackageType>;
private id: string;
public constructor(private options: PackageVersionOptions) {
this.connection = this.options.connection;
this.project = this.options.project;
this.id = this.resolveId();
}
/**
* Sends a request to create a new package version and optionally polls for
* the status of the request until the package version is created or the
* polling timeout is reached.
*
* @param options PackageVersionCreateOptions
* @param polling frequency and timeout Durations to be used in polling
* @returns PackageVersionCreateRequestResult
*/
public static async create(
options: PackageVersionCreateOptions,
polling: { frequency: Duration; timeout: Duration } = {
frequency: Duration.seconds(0),
timeout: Duration.seconds(0),
}
): Promise<Partial<PackageVersionCreateRequestResult>> {
const pvc = new PackageVersionCreate({ ...options });
const createResult = await pvc.createPackageVersion();
if (createResult.Id) {
return PackageVersion.pollCreateStatus(createResult.Id, options.connection, options.project, polling).catch(
(err: SfError) => {
if (err.name === 'PollingClientTimeout') {
err.setData({ VersionCreateRequestId: createResult.Id });
err.message += ` Run 'sf package version create report -i ${createResult.Id}' to check the status.`;
}
// TODO
// until package2 is GA, wrap perm-based errors w/ 'contact sfdc' action (REMOVE once package2 is GA'd)
throw applyErrorAction(massageErrorMessage(err));
}
);
} else {
throw new Error(messages.getMessage('createResultIdCannotBeEmpty'));
}
}
/**
* Gets current state of a package version create request.
*
* @param createPackageRequestId
* @param connection
*/
public static async getCreateStatus(
createPackageRequestId: string,
connection: Connection
): Promise<PackageVersionCreateRequestResult> {
return getCreatePackageVersionCreateRequestReport({
createPackageVersionRequestId: createPackageRequestId,
connection,
}).catch((err: Error) => {
// TODO
// until package2 is GA, wrap perm-based errors w/ 'contact sfdc' action (REMOVE once package2 is GA'd)
throw applyErrorAction(massageErrorMessage(err));
});
}
/**
* Fetch a list of package version create requests based on the given options.
*
* @param connection connection to an org
* @param options PackageVersionCreateRequestQueryOptions
* @returns the list of package version create requests.
*/
public static async getPackageVersionCreateRequests(
connection: Connection,
options?: PackageVersionCreateRequestQueryOptions
): Promise<PackageVersionCreateRequestResult[]> {
return list(connection, options);
}
/**
* Convenience function that will wait for a package version to be created.
*
* This function emits LifeCycle events, "enqueued", "in-progress", "success", "error" and "timed-out" to
* progress and current status. Events also carry a payload of type PackageVersionCreateRequestResult.
*
* @param createPackageVersionRequestId
* @param connection Connection to the org
* @param project SfProject to read/write aliases from
* @param polling frequency and timeout Durations to be used in polling
* */
public static async pollCreateStatus(
createPackageVersionRequestId: string,
connection: Connection,
project: SfProject,
polling: { frequency: Duration; timeout: Duration }
): Promise<PackageVersionCreateRequestResult> {
if (polling.timeout?.milliseconds <= 0) {
return this.getCreateStatus(createPackageVersionRequestId, connection);
}
let remainingWaitTime: Duration = polling.timeout;
const pollingClient = await PollingClient.create({
poll: async (): Promise<StatusResult> => {
const report = await this.getCreateStatus(createPackageVersionRequestId, connection);
switch (report.Status) {
case Package2VersionStatus.queued:
await Lifecycle.getInstance().emit(PackageVersionEvents.create.enqueued, { ...report, remainingWaitTime });
remainingWaitTime = Duration.seconds(remainingWaitTime.seconds - polling.frequency.seconds);
return {
completed: false,
payload: report,
};
case Package2VersionStatus.inProgress:
case Package2VersionStatus.initializing:
case Package2VersionStatus.verifyingFeaturesAndSettings:
case Package2VersionStatus.verifyingDependencies:
case Package2VersionStatus.verifyingMetadata:
case Package2VersionStatus.finalizingPackageVersion:
await Lifecycle.getInstance().emit(PackageVersionEvents.create.progress, {
...report,
remainingWaitTime,
});
remainingWaitTime = Duration.seconds(remainingWaitTime.seconds - polling.frequency.seconds);
return {
completed: false,
payload: report,
};
case Package2VersionStatus.success: {
await Lifecycle.getInstance().emit(PackageVersionEvents.create.success, report);
const packageVersion = new PackageVersion({
connection,
project,
idOrAlias: report.Package2VersionId,
});
await packageVersion.updateProjectWithPackageVersion(report);
return { completed: true, payload: report };
}
case Package2VersionStatus.error:
await Lifecycle.getInstance().emit(PackageVersionEvents.create.error, report);
return { completed: true, payload: report };
}
},
frequency: polling.frequency,
timeout: polling.timeout,
});
try {
return await pollingClient.subscribe<PackageVersionCreateRequestResult>();
} catch (err) {
const report = await this.getCreateStatus(createPackageVersionRequestId, connection);
await Lifecycle.getInstance().emit(PackageVersionEvents.create['timed-out'], report);
if (err instanceof Error) {
throw applyErrorAction(err);
}
throw err;
}
}
/**
* Gets current state of a package version create request.
*
* @param createPackageRequestId
* @param connection
*/
public static async getCreateVersionReport(
createPackageRequestId: string,
connection: Connection
): Promise<PackageVersionCreateRequestResult> {
return getCreatePackageVersionCreateRequestReport({
createPackageVersionRequestId: createPackageRequestId,
connection,
}).catch((err: Error) => {
// TODO
// until package2 is GA, wrap perm-based errors w/ 'contact sfdc' action (REMOVE once package2 is GA'd)
throw applyErrorAction(massageErrorMessage(err));
});
}
/**
* Convenience function that will wait for a package version to be created.
*
* This function emits LifeCycle events, "enqueued", "in-progress", "success", "error" and "timed-out" to
* progress and current status. Events also carry a payload of type PackageVersionCreateRequestResult.
*
* @param createPackageVersionRequestId
* @param project
* @param connection
* @param polling frequency and timeout Durations to be used in polling
* */
public static async waitForCreateVersion(
createPackageVersionRequestId: string,
project: SfProject,
connection: Connection,
polling: { frequency: Duration; timeout: Duration }
): Promise<PackageVersionCreateRequestResult> {
if (polling.timeout?.milliseconds <= 0) {
return PackageVersion.getCreateVersionReport(createPackageVersionRequestId, connection);
}
let remainingWaitTime: Duration = polling.timeout;
const pollingClient = await PollingClient.create({
poll: async (): Promise<StatusResult> => {
const report = await this.getCreateVersionReport(createPackageVersionRequestId, connection);
switch (report.Status) {
case Package2VersionStatus.queued:
await Lifecycle.getInstance().emit(PackageVersionEvents.create.enqueued, { ...report, remainingWaitTime });
remainingWaitTime = Duration.seconds(remainingWaitTime.seconds - polling.frequency.seconds);
return {
completed: false,
payload: report,
};
case Package2VersionStatus.inProgress:
case Package2VersionStatus.initializing:
case Package2VersionStatus.verifyingFeaturesAndSettings:
case Package2VersionStatus.verifyingDependencies:
case Package2VersionStatus.verifyingMetadata:
case Package2VersionStatus.finalizingPackageVersion:
await Lifecycle.getInstance().emit(PackageVersionEvents.create.progress, {
...report,
remainingWaitTime,
});
remainingWaitTime = Duration.seconds(remainingWaitTime.seconds - polling.frequency.seconds);
return {
completed: false,
payload: report,
};
case Package2VersionStatus.success:
await Lifecycle.getInstance().emit(PackageVersionEvents.create.success, report);
await new PackageVersion({
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
idOrAlias: report.SubscriberPackageVersionId!,
project,
connection,
}).updateProjectWithPackageVersion(report);
return { completed: true, payload: report };
case Package2VersionStatus.error:
await Lifecycle.getInstance().emit(PackageVersionEvents.create.error, report);
return { completed: true, payload: report };
}
},
frequency: polling.frequency,
timeout: polling.timeout,
});
try {
return await pollingClient.subscribe<PackageVersionCreateRequestResult>();
} catch (err) {
const report = await this.getCreateVersionReport(createPackageVersionRequestId, connection);
await Lifecycle.getInstance().emit(PackageVersionEvents.create['timed-out'], report);
throw applyErrorAction(err as Error);
}
}
/**
* Query the Package2Version SObject and return data with the provided type.
*
* NOTE: There is a limit of 2000 records that can be returned, otherwise
* a GACK might be thrown. If more than 2000 records are desired you should
* filter the query by date and aggregate all results.
*
* @param connection jsForce Connection to the org.
* @param options Package2Version query options
* @returns Results from querying the Package2Version SObject.
*/
public static async queryPackage2Version(
connection: Connection,
options: Package2VersionQueryOptions = {}
): Promise<Partial<Package2Version[]>> {
const fields = options.fields ?? Package2VersionFields;
const { whereClause, whereClauseItems } = options;
const orderBy = options.orderBy ?? 'ORDER BY LastModifiedDate DESC';
let query = `SELECT ${fields.toString()} FROM Package2Version`;
if (whereClause) {
query += ` ${whereClause} ${orderBy}`;
if (whereClauseItems) {
query += ' LIMIT 2000';
return queryWithInConditionChunking<Package2Version>(query, whereClauseItems, '%IDS%', connection);
}
}
query += ' LIMIT 2000';
const result = await connection.tooling.query<Package2Version>(query);
if (result?.totalSize === 2000) {
const warningMsg = messages.getMessage('maxPackage2VersionRecords');
await Lifecycle.getInstance().emitWarning(warningMsg);
}
return result.records ?? [];
}
/**
* Get the package version ID for this PackageVersion.
*
* @returns The PackageVersionId (05i).
*/
public async getId(): Promise<string | undefined> {
if (!this.data?.Id) {
await this.getData();
}
return this.data?.Id;
}
/**
* Get the subscriber package version ID for this PackageVersion.
*
* @returns The SubscriberPackageVersionId (04t).
*/
public async getSubscriberId(): Promise<string | undefined> {
if (!this.data?.SubscriberPackageVersionId) {
await this.getData();
}
return this.data?.SubscriberPackageVersionId;
}
/**
* Get the package Id for this PackageVersion.
*
* @returns The PackageId (0Ho).
*/
public async getPackageId(): Promise<string | undefined> {
if (!this.data?.Package2Id) {
await this.getData();
}
return this.data?.Package2Id;
}
/**
* Get the package type for this PackageVersion.
*
* @returns The PackageType (Managed, Unlocked).
*/
public async getPackageType(): Promise<PackageType> {
if (!this.packageType) {
this.packageType = (
await this.connection.singleRecordQuery<Package2>(
`select ContainerOptions from Package2 where Id = '${await this.getPackageId()}' limit 1`,
{ tooling: true }
)
).ContainerOptions;
}
return this.packageType;
}
/**
* Get the Package2Version SObject data for this PackageVersion.
*
* @param force force a refresh of the package version data.
* @returns Package2Version
*/
public async getData(force = false): Promise<Package2Version> | never {
let is05i = false;
if (!this.data || force) {
// validate ID
if (this.id.startsWith('04t')) {
validateId(BY_LABEL.SUBSCRIBER_PACKAGE_VERSION_ID, this.id);
is05i = false;
} else if (this.id.startsWith('05i')) {
validateId(BY_LABEL.PACKAGE_VERSION_ID, this.id);
is05i = true;
} else {
throw messages.createError('errorInvalidPackageVersionId', [this.options.idOrAlias]);
}
let queryConfig: { id: string; clause: string; label1: string; label2: string };
if (is05i) {
queryConfig = {
id: this.id,
clause: `Id = '${this.id}'`,
label1: BY_LABEL.PACKAGE_VERSION_ID.label,
label2: BY_LABEL.SUBSCRIBER_PACKAGE_VERSION_ID.label,
};
} else {
queryConfig = {
id: this.id,
clause: `SubscriberPackageVersionId = '${this.id}'`,
label1: BY_LABEL.SUBSCRIBER_PACKAGE_VERSION_ID.label,
label2: BY_LABEL.PACKAGE_VERSION_ID.label,
};
}
const allFields = Package2VersionFields.toString();
const query = `SELECT ${allFields} FROM Package2Version WHERE ${queryConfig.clause} LIMIT 1`;
try {
this.data = await this.connection.singleRecordQuery<Package2Version>(query, { tooling: true });
} catch (err) {
throw messages.createError(
'errorInvalidIdNoMatchingVersionId',
[queryConfig.label1, queryConfig.id, queryConfig.label2],
undefined,
err instanceof Error ? err : new Error(err as string)
);
}
}
return this.data;
}
/**
* Deletes this PackageVersion.
*/
public async delete(): Promise<PackageSaveResult> {
return this.updateDeprecation(true);
}
/**
* Undeletes this PackageVersion.
*/
public async undelete(): Promise<PackageSaveResult> {
return this.updateDeprecation(false);
}
/**
* Reports details about this PackageVersion.
*
* @param verbose Whether to get a detailed version of the report, at the expense of performance.
*/
public async report(verbose = false): Promise<PackageVersionReportResult> {
const packageVersionId = await this.getId();
if (!packageVersionId) {
throw messages.createError('errorInvalidPackageVersionId', [this.options.idOrAlias]);
}
const results = await getPackageVersionReport({
packageVersionId,
connection: this.connection,
project: this.project,
verbose,
}).catch((err: Error) => {
// TODO
// until package2 is GA, wrap perm-based errors w/ 'contact sfdc' action (REMOVE once package2 is GA'd)
throw applyErrorAction(massageErrorMessage(err));
});
return results[0];
}
/**
* Promotes this PackageVersion to released state.
*/
public async promote(): Promise<PackageSaveResult> {
const id = await this.getId();
if (!id) {
throw messages.createError('errorInvalidPackageVersionId', [this.options.idOrAlias]);
}
return this.options.connection.tooling.update('Package2Version', { IsReleased: true, Id: id });
}
public async update(options: PackageVersionUpdateOptions): Promise<PackageSaveResult> {
const id = await this.getId();
if (!id) {
throw messages.createError('errorInvalidPackageVersionId', [this.options.idOrAlias]);
}
const request = Object.fromEntries(
Object.entries({
Id: id,
InstallKey: options.InstallKey,
Name: options.VersionName,
Description: options.VersionDescription,
Branch: options.Branch,
Tag: options.Tag,
}).filter(([, value]) => value !== undefined)
) as Package2Version & { [name: string]: string | undefined };
const result = await this.connection.tooling.update('Package2Version', request);
if (!result.success) {
throw new Error(result.errors.join(', '));
}
// Use the 04t ID for the success message
const subscriberPackageVersionId = await this.getSubscriberId();
if (!subscriberPackageVersionId) {
throw messages.createError('errorNoSubscriberPackageVersionId');
}
result.id = subscriberPackageVersionId;
return result;
}
private async updateDeprecation(isDeprecated: boolean): Promise<PackageSaveResult> {
const id = await this.getId();
if (!id) {
throw messages.createError('errorInvalidPackageVersionId', [this.options.idOrAlias]);
}
// setup the request
const request: { Id: string; IsDeprecated: boolean } = {
Id: id,
IsDeprecated: isDeprecated,
};
const updateResult = await this.connection.tooling.update('Package2Version', request);
if (!updateResult.success) {
throw combineSaveErrors('Package2', 'update', updateResult.errors);
}
const subscriberPackageVersionId = await this.getSubscriberId();
if (!subscriberPackageVersionId) {
throw messages.createError('errorNoSubscriberPackageVersionId');
}
updateResult.id = subscriberPackageVersionId;
return updateResult;
}
private async updateProjectWithPackageVersion(results: PackageVersionCreateRequestResult): Promise<void> {
if (!env.getBoolean('SF_PROJECT_AUTOUPDATE_DISABLE_FOR_PACKAGE_VERSION_CREATE')) {
// get the newly created package version from the server
const versionResult = (
await this.connection.tooling.query<{
Branch: string;
MajorVersion: string;
MinorVersion: string;
PatchVersion: string;
BuildNumber: string;
}>(
`SELECT Branch, MajorVersion, MinorVersion, PatchVersion, BuildNumber FROM Package2Version WHERE SubscriberPackageVersionId='${results.SubscriberPackageVersionId}'`
)
).records[0];
const aliases = this.project.getAliasesFromPackageId(results.Package2Id);
if (aliases.length === 0) {
throw messages.createError('packageAliasNotFound', [results.Package2Id]);
}
const version = `${aliases[0]}@${versionResult.MajorVersion ?? 0}.${versionResult.MinorVersion ?? 0}.${
versionResult.PatchVersion ?? 0
}`;
const build = versionResult.BuildNumber ? `-${versionResult.BuildNumber}` : '';
const branch = versionResult.Branch ? `-${versionResult.Branch}` : '';
const originalPackageAliases = this.project.getSfProjectJson().get('packageAliases') ?? {};
const updatedPackageAliases = {
...originalPackageAliases,
...(results.SubscriberPackageVersionId
? // set packageAliases entry '<package>@<major>.<minor>.<patch>-<build>-<branch>: <result.subscriberPackageVersionId>'
{ [`${version}${build}${branch}`]: results.SubscriberPackageVersionId }
: {}),
};
this.project.getSfProjectJson().set('packageAliases', updatedPackageAliases);
await this.project.getSfProjectJson().write();
}
}
private resolveId(): string {
return this.project.getPackageIdFromAlias(this.options.idOrAlias) ?? this.options.idOrAlias;
}
}