generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 8
/
report.ts
261 lines (240 loc) · 10 KB
/
report.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
/*
* Copyright (c) 2022, 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 { Flags, loglevel, orgApiVersionFlagWithDeprecations, SfCommand } from '@salesforce/sf-plugins-core';
import { Messages } from '@salesforce/core/messages';
import {
CodeCoverage,
PackageVersion,
PackageType,
PackageVersionReportResult,
PackagingSObjects,
} from '@salesforce/packaging';
import chalk from 'chalk';
import { requiredHubFlag } from '../../../utils/hubFlag.js';
import { maybeGetProject } from '../../../utils/getProject.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_version_report');
const pvlMessages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_version_list');
const plMessages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_list');
const omissions = ['CodeCoverage', 'HasPassedCodeCoverageCheck', 'Package2', 'HasMetadataRemoved'] as const;
type Omission = (typeof omissions)[number];
export type PackageVersionReportResultModified = Omit<PackageVersionReportResult, Omission> & {
CodeCoverage: CodeCoverage | string | undefined;
HasPassedCodeCoverageCheck: boolean | undefined | string;
Package2: Partial<Omit<PackagingSObjects.Package2, 'IsOrgDependent'> & { IsOrgDependent: string }>;
PackageType?: PackageType;
HasMetadataRemoved: boolean | string;
};
export class PackageVersionReportCommand extends SfCommand<PackageVersionReportResultModified> {
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessages('examples');
public static readonly deprecateAliases = true;
public static readonly aliases = ['force:package:version:report'];
public static readonly flags = {
loglevel,
'target-dev-hub': requiredHubFlag,
'api-version': orgApiVersionFlagWithDeprecations,
package: Flags.string({
char: 'p',
summary: messages.getMessage('flags.package.summary'),
required: true,
}),
verbose: Flags.boolean({
summary: messages.getMessage('flags.verbose.summary'),
}),
};
protected haveCodeCoverageData = false;
public async run(): Promise<PackageVersionReportResultModified> {
const { flags } = await this.parse(PackageVersionReportCommand);
const packageVersion = new PackageVersion({
connection: flags['target-dev-hub'].getConnection(flags['api-version']),
project: await maybeGetProject(),
idOrAlias: flags.package,
});
const results = await packageVersion.report(flags.verbose);
const massagedResults = this.massageResultsForDisplay(results);
this.display(massagedResults, flags.verbose);
return massagedResults;
}
private display(record: PackageVersionReportResultModified, verbose: boolean): void {
if (this.jsonEnabled()) {
return;
}
let dependencies: string | undefined;
// collect the Dependency 04ts into a comma-separated list for non-json output
if (verbose && record.SubscriberPackageVersion?.Dependencies != null) {
dependencies = record.SubscriberPackageVersion.Dependencies.ids
.map((d) => d.subscriberPackageVersionId)
.join(', ');
}
// transform the results into a table
const displayRecords = [
{
key: pvlMessages.getMessage('name'),
value: record.Name,
},
{
key: pvlMessages.getMessage('subscriberPackageVersionId'),
value: record.SubscriberPackageVersionId,
},
{ key: 'Id', value: record.Id },
{
key: pvlMessages.getMessage('package-id'),
value: record.Package2Id,
},
{
key: pvlMessages.getMessage('version'),
value: record.Version,
},
{
key: pvlMessages.getMessage('description'),
value: record.Description,
},
{
key: pvlMessages.getMessage('packageBranch'),
value: record.Branch,
},
{
key: pvlMessages.getMessage('packageTag'),
value: record.Tag,
},
{ key: messages.getMessage('isReleased'), value: `${record.IsReleased ?? '<undefined>'} ` },
{
key: pvlMessages.getMessage('validationSkipped'),
value: record.ValidationSkipped,
},
{
key: pvlMessages.getMessage('validatedAsync'),
value: record.ValidatedAsync,
},
{ key: messages.getMessage('ancestorId'), value: record.AncestorId },
{ key: messages.getMessage('ancestorVersion'), value: record.AncestorVersion },
{
key: pvlMessages.getMessage('codeCoverage'),
value:
typeof record.CodeCoverage === 'string'
? record.CodeCoverage
: !record.CodeCoverage
? 'N/A'
: `${record.CodeCoverage.apexCodeCoveragePercentage.toFixed(2)}%`,
},
{
key: pvlMessages.getMessage('hasPassedCodeCoverageCheck'),
value: record.HasPassedCodeCoverageCheck,
},
{
key: pvlMessages.getMessage('convertedFromVersionId'),
value: record.ConvertedFromVersionId,
},
{
key: plMessages.getMessage('isOrgDependent'),
value: record.Package2.IsOrgDependent,
},
{
key: pvlMessages.getMessage('releaseVersion'),
value: record.ReleaseVersion === null ? '' : record.ReleaseVersion?.toFixed(1),
},
{
key: pvlMessages.getMessage('buildDurationInSeconds'),
value: record.BuildDurationInSeconds === null ? '' : record.BuildDurationInSeconds?.toFixed(1),
},
{
key: pvlMessages.getMessage('hasMetadataRemoved'),
value: record.HasMetadataRemoved,
},
{
key: messages.getMessage('dependencies'),
value: verbose && dependencies != null ? dependencies : ' ',
},
{
key: plMessages.getMessage('createdBy'),
value: record.CreatedById,
},
];
const maximumNumClasses = 15; // Number of least code covered classes displayed on the cli output for better UX.
let codeCovStr = ''; // String to display when code coverage data is empty or null
let displayCoverageRecords: Array<{ value: string; key: string }> = [];
// collect the code coverage data into an array of key value records for non-json output
if (verbose) {
const coverageData = record.CodeCoveragePercentages?.codeCovPercentages;
if (!coverageData) {
codeCovStr = 'N/A'; // Code coverage isn't calculated as part of version create command
} else if (!coverageData.length) {
// Calculated code coverage data is too big to fit into a DB field. Retrieve it from the packageZip
codeCovStr =
'The code coverage details are too large to display. To request code coverage details for this package version, log a case in the Salesforce Partner Community.';
} else {
displayCoverageRecords = coverageData.slice(0, maximumNumClasses).map((coverageDatum) => ({
key: coverageDatum.className,
value: `${coverageDatum.codeCoveragePercentage}%`,
}));
this.haveCodeCoverageData = displayCoverageRecords.length > 0;
}
displayRecords.push({
key: pvlMessages.getMessage('endToEndBuildDurationInSeconds'),
value: record.EndToEndBuildDurationInSeconds?.toFixed(1) ?? '',
});
}
// Always append code coverage column label ar the end
displayRecords.push({
key: messages.getMessage('codeCoveragePercentages'),
value: this.haveCodeCoverageData ? '...' : codeCovStr,
});
if (!verbose) {
displayRecords.splice(displayRecords.map((e) => e.key).indexOf('Id'), 1);
if (!record.ConvertedFromVersionId?.trim()) {
displayRecords.splice(
displayRecords.map((e) => e.key).indexOf(pvlMessages.getMessage('convertedFromVersionId')),
1
);
}
displayRecords.splice(displayRecords.map((e) => e.key).indexOf(messages.getMessage('dependencies')), 1);
displayRecords.splice(
displayRecords.map((e) => e.key).indexOf(messages.getMessage('codeCoveragePercentages')),
1
);
displayCoverageRecords.splice(0, displayCoverageRecords.length);
}
this.table({ data: displayRecords, title: chalk.blue('Package Version') });
if (displayCoverageRecords.length > 0) {
this.table({ data: displayCoverageRecords });
}
}
// eslint-disable-next-line class-methods-use-this
private massageResultsForDisplay(results: PackageVersionReportResult): PackageVersionReportResultModified {
const record = Object.fromEntries(
Object.entries(results).filter(([key, value]) => {
if (key === 'PackageType' && typeof value === 'string' && value === 'Unlocked') {
return false;
}
return !omissions.some((o) => o === key);
})
) as PackageVersionReportResultModified;
record.Version = [record.MajorVersion, record.MinorVersion, record.PatchVersion, record.BuildNumber].join('.');
if (results.PackageType !== 'Managed') {
record.AncestorVersion = 'N/A';
record.AncestorId = 'N/A';
}
if (results.Package2.IsOrgDependent === true || results.ValidationSkipped === true) {
record.CodeCoverage = 'N/A';
} else {
record.CodeCoverage = results.CodeCoverage;
}
record.HasPassedCodeCoverageCheck =
Boolean(results.Package2.IsOrgDependent) || results.ValidationSkipped
? 'N/A'
: results.HasPassedCodeCoverageCheck ?? undefined;
record.Package2 = {};
record.Package2.IsOrgDependent =
results.PackageType === 'Managed' ? 'N/A' : results.Package2.IsOrgDependent === true ? 'Yes' : 'No';
// set HasMetadataRemoved to N/A for Unlocked, and No when value is false or absent (pre-230)
record.HasMetadataRemoved = results.PackageType !== 'Managed' ? 'N/A' : results.HasMetadataRemoved ? 'Yes' : 'No';
record.ConvertedFromVersionId ??= ' ';
return record;
}
}