Skip to content

Commit

Permalink
fix: add pvr and pvcr
Browse files Browse the repository at this point in the history
@W-11444281@
add package:version:report and package:version:create:report
  • Loading branch information
peternhale committed Aug 17, 2022
1 parent 8a368fb commit 79d27cf
Show file tree
Hide file tree
Showing 5 changed files with 291 additions and 17 deletions.
12 changes: 12 additions & 0 deletions messages/package_version_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,15 @@ Dependencies
# codeCoveragePercentages

Code Coverage Details

# ancestorId

Ancestor

# ancestorVersion

Ancestor Version

# isReleased

Released
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@
"@salesforce/command": "^5.2.4",
"@salesforce/core": "^3.26.1",
"@salesforce/kit": "^1.5.45",
"@salesforce/packaging": "^0.0.17",
"@salesforce/packaging": "0.0.18-t-02",
"chalk": "^4.1.2",
"tslib": "^2"
},
"devDependencies": {
"@oclif/plugin-command-snapshot": "^3.1.3",
"@salesforce/cli-plugins-testkit": "^1.5.35",
"@salesforce/dev-config": "^2.1.3",
"@salesforce/dev-scripts": "^1.0.4",
"@salesforce/plugin-auth": "^2.2.3",
"@salesforce/plugin-command-reference": "^1.3.20",
"@salesforce/plugin-config": "^1.4.12",
"@salesforce/plugin-auth": "^2.2.3",
"@salesforce/prettier-config": "^0.0.2",
"@salesforce/ts-sinon": "1.3.21",
"@salesforce/ts-types": "^1.5.20",
Expand Down
68 changes: 63 additions & 5 deletions src/commands/force/package/beta/version/create/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@
*/

import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';
import { Messages, SfdxPropertyKeys } from '@salesforce/core';
import { Messages, OrgConfigProperties } from '@salesforce/core';
import { PackageVersion, PackageVersionCreateRequestResult } from '@salesforce/packaging';
import * as pkgUtils from '@salesforce/packaging';
import * as chalk from 'chalk';

Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_version_create_report');
const pvclMessages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_version_create_list');
const plMessages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_list');

export class PackageVersionCreateReportCommand extends SfdxCommand {
public static readonly description = messages.getMessage('cliDescription');
public static readonly longDescription = messages.getMessage('cliLongDescription');
public static readonly help = messages.getMessage('help');
public static readonly;
public static readonly orgType = SfdxPropertyKeys.DEFAULT_DEV_HUB_USERNAME;
public static readonly orgType = OrgConfigProperties.TARGET_DEV_HUB;
public static readonly requiresDevhubUsername = true;
public static readonly flagsConfig: FlagsConfig = {
packagecreaterequestid: flags.id({
Expand All @@ -27,8 +32,61 @@ export class PackageVersionCreateReportCommand extends SfdxCommand {
}),
};

public async run(): Promise<unknown> {
process.exitCode = 1;
return Promise.resolve('Not yet implemented');
public async run(): Promise<PackageVersionCreateRequestResult> {
const packageVersion = new PackageVersion({ connection: this.hubOrg.getConnection(), project: undefined });
const result = await packageVersion.getCreateVersionReport(this.flags.packagecreaterequestid);
this.display(result);
return result;
}

private display(record: PackageVersionCreateRequestResult): void {
const installUrlValue =
record.Status === 'Success' ? `${pkgUtils.INSTALL_URL_BASE.toString()}${record.SubscriberPackageVersionId}` : '';

const data = [
{
key: pvclMessages.getMessage('id'),
value: record.Id,
},
{
key: pvclMessages.getMessage('status'),
value: pkgUtils.convertCamelCaseStringToSentence(record.Status),
},
{
key: pvclMessages.getMessage('packageId'),
value: record.Package2Id,
},
{
key: pvclMessages.getMessage('packageVersionId'),
value: record.Package2VersionId,
},
{
key: pvclMessages.getMessage('subscriberPackageVersionId'),
value: record.SubscriberPackageVersionId,
},
{
key: pvclMessages.getMessage('tag'),
value: record.Tag,
},
{
key: pvclMessages.getMessage('branch'),
value: record.Branch,
},
{ key: 'Created Date', value: record.CreatedDate },
{
key: pvclMessages.getMessage('installUrl'),
value: installUrlValue,
},
{
key: plMessages.getMessage('createdBy'),
value: record.CreatedBy,
},
];

this.ux.styledHeader(chalk.blue('Package Version Create Request'));
this.ux.table(data, {
key: { header: 'Name' },
value: { header: 'Value' },
});
}
}
215 changes: 209 additions & 6 deletions src/commands/force/package/beta/version/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,29 @@
*/

import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';
import { Messages, SfdxPropertyKeys } from '@salesforce/core';
import { Messages, OrgConfigProperties } from '@salesforce/core';
import { CodeCoverage, PackageVersion, PackageVersionReportResult, PackagingSObjects } from '@salesforce/packaging';
import * as pkgUtils from '@salesforce/packaging';
import * as chalk from 'chalk';

Messages.importMessagesDirectory(__dirname);
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');
type PackageVersionReportResultModified = Omit<
PackageVersionReportResult,
'CodeCoverage' | 'HasPassedCodeCoverageCheck' | 'Package2' | 'HasMetadataRemoved'
> & {
CodeCoverage: CodeCoverage | string;
HasPassedCodeCoverageCheck: boolean | string;
Package2: Partial<Omit<PackagingSObjects.Package2, 'IsOrgDependent'> & { IsOrgDependent: boolean | string }>;
HasMetadataRemoved: boolean | string;
};
export class PackageVersionReportCommand extends SfdxCommand {
public static readonly description = messages.getMessage('cliDescription');
public static readonly longDescription = messages.getMessage('cliLongDescription');
public static readonly help = messages.getMessage('help');
public static readonly orgType = SfdxPropertyKeys.DEFAULT_DEV_HUB_USERNAME;
public static readonly orgType = OrgConfigProperties.TARGET_DEV_HUB;
public static readonly requiresDevhubUsername = true;
public static readonly requiresProject = true;
public static readonly flagsConfig: FlagsConfig = {
Expand All @@ -30,9 +43,199 @@ export class PackageVersionReportCommand extends SfdxCommand {
longDescription: messages.getMessage('verboseLongDescription'),
}),
};
protected haveCodeCoverageData = false;

public async run(): Promise<PackageVersionReportResultModified> {
const packageVersion = new PackageVersion({ connection: this.hubOrg.getConnection(), project: this.project });
const results = await packageVersion.report(this.flags.package, this.flags.verbose);
const massagedResults = await this.massageResultsForDisplay(results);
this.display(massagedResults);
return massagedResults;
}

private display(record: PackageVersionReportResultModified): void {
if (this.flags.json) {
return;
}

let dependencies: string = null;

// collect the Dependency 04ts into a comma-separated list for non-json output
if (this.flags.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('packageId'),
value: record.Package2Id,
},
{
key: pvlMessages.getMessage('version'),
value: record.Version,
},
{
key: pvlMessages.getMessage('description'),
value: record.Description === null ? 'null' : record.Description,
},
{
key: pvlMessages.getMessage('packageBranch'),
value: record.Branch === null ? 'null' : record.Branch,
},
{
key: pvlMessages.getMessage('packageTag'),
value: record.Tag === null ? 'null' : record.Tag,
},
{ key: messages.getMessage('isReleased'), value: record.IsReleased.toString() },
{
key: pvlMessages.getMessage('validationSkipped'),
value: record.ValidationSkipped,
},
{ key: messages.getMessage('ancestorId'), value: record.AncestorId },
{ key: messages.getMessage('ancestorVersion'), value: record.AncestorVersion },
{
key: pvlMessages.getMessage('codeCoverage'),
value:
record.CodeCoverage === null
? ' '
: record.CodeCoverage['apexCodeCoveragePercentage'] !== undefined
? `${record.CodeCoverage['apexCodeCoveragePercentage'] as number}%`
: 'N/A', // N/A
},
{
key: pvlMessages.getMessage('hasPassedCodeCoverageCheck'),
value: record.HasPassedCodeCoverageCheck,
},
{
key: pvlMessages.getMessage('convertedFromVersionId'),
value: record.ConvertedFromVersionId === null ? ' ' : 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,
},
{
key: pvlMessages.getMessage('hasMetadataRemoved'),
value: record.HasMetadataRemoved,
},
{
key: messages.getMessage('dependencies'),
value: this.flags.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 = [];
// collect the code coverage data into an array of key value records for non-json output
if (this.flags.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) => {
return {
key: coverageDatum.className,
value: `${coverageDatum.codeCoveragePercentage}%`,
};
});
this.haveCodeCoverageData = displayCoverageRecords.length > 0;
}
}

// Always append code coverage column label ar the end
displayRecords.push({
key: messages.getMessage('codeCoveragePercentages'),
value: this.haveCodeCoverageData === true ? '...' : codeCovStr,
});
if (!this.flags.verbose) {
displayRecords.splice(displayRecords.map((e) => e.key).indexOf('Id'), 1);
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.ux.styledHeader(chalk.blue('Package Version'));
this.ux.table(displayRecords, { key: { header: 'Name' }, value: { header: 'Value' } });
if (displayCoverageRecords.length > 0) {
this.ux.table(displayCoverageRecords, { key: { header: 'Class Name' }, value: { header: 'Code Coverage' } });
}
}

private async massageResultsForDisplay(
results: PackageVersionReportResult
): Promise<PackageVersionReportResultModified> {
const record = results as PackageVersionReportResultModified;
record.Version = [record.MajorVersion, record.MinorVersion, record.PatchVersion, record.BuildNumber].join('.');

let ancestorVersion: string = null;
const containerOptions = await pkgUtils.getContainerOptions([record.Package2Id], this.hubOrg.getConnection());
const packageType = containerOptions.get(record.Package2Id);
if (record.AncestorId) {
// lookup AncestorVersion value
const ancestorVersionMap = await pkgUtils.getPackageVersionStrings(
[record.AncestorId],
this.hubOrg.getConnection()
);
ancestorVersion = ancestorVersionMap.get(record.AncestorId);
} else {
// otherwise display 'N/A' if package is Unlocked Packages
if (packageType !== 'Managed') {
ancestorVersion = 'N/A';
record.AncestorId = 'N/A';
}
}

record.CodeCoverage =
record.Package2.IsOrgDependent === true || record.ValidationSkipped === true ? 'N/A' : record.CodeCoverage;

record.HasPassedCodeCoverageCheck =
record.Package2.IsOrgDependent === true || record.ValidationSkipped === true
? 'N/A'
: record.HasPassedCodeCoverageCheck;

record.Package2.IsOrgDependent =
packageType === 'Managed' ? 'N/A' : record.Package2.IsOrgDependent === true ? 'Yes' : 'No';

// set HasMetadataRemoved to N/A for Unlocked, and No when value is false or absent (pre-230)
record.HasMetadataRemoved = packageType !== 'Managed' ? 'N/A' : record.HasMetadataRemoved === true ? 'Yes' : 'No';

// add AncestorVersion to the json record
record.AncestorVersion = ancestorVersion;

public async run(): Promise<unknown> {
process.exitCode = 1;
return Promise.resolve('Not yet implemented');
return record;
}
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1150,10 +1150,10 @@
shx "^0.3.3"
tslib "^2.2.0"

"@salesforce/packaging@^0.0.17":
version "0.0.17"
resolved "https://registry.yarnpkg.com/@salesforce/packaging/-/packaging-0.0.17.tgz#cdf26185e2744ec606c4cad1e1978e5e97696e5d"
integrity sha512-zq0z0tfBZc3sk9rfBLw6ZcLL80KR12V8pEcStUiNF4LuLDD+oUBflyc2gbLcS9Og0oEXafncQxwhws7GWvpDAw==
"@salesforce/packaging@0.0.18-t-02":
version "0.0.18-t-02"
resolved "http://localhost:4873/@salesforce%2fpackaging/-/packaging-0.0.18-t-02.tgz#fcac198ab25db3bfb834fb011b4bf8a7c177ea5c"
integrity sha512-zVyo/Y141KvLZgnK9bUxttzwY/tiZlAM4DWODXVkrDsH5lHThZklzY5K2zcrAbKImyTOQJB9KKHnjBzWvlvnsQ==
dependencies:
"@salesforce/core" "^3.24.0"
"@salesforce/kit" "^1.5.44"
Expand Down

0 comments on commit 79d27cf

Please sign in to comment.