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

Sm/pr-comments-as-pr #738

Merged
merged 2 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
61 changes: 32 additions & 29 deletions src/commands/package/installed/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ import {
requiredOrgFlagWithDeprecations,
SfCommand,
} from '@salesforce/sf-plugins-core';
import { SubscriberPackageVersion } from '@salesforce/packaging';
import { InstalledPackages, SubscriberPackageVersion } from '@salesforce/packaging';

Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_installed_list');

export type PackageInstalledListResult = {
Id: string;
SubscriberPackageId: string | undefined;
SubscriberPackageName: string | undefined;
SubscriberPackageNamespace: string | undefined;
SubscriberPackageVersionId: string | undefined;
SubscriberPackageVersionName: string | undefined;
SubscriberPackageVersionNumber: string | undefined;
SubscriberPackageId?: string;
SubscriberPackageName?: string;
SubscriberPackageNamespace?: string;
SubscriberPackageVersionId?: string;
SubscriberPackageVersionName?: string;
SubscriberPackageVersionNumber?: string;
};

export type PackageInstalledCommandResult = PackageInstalledListResult[];
Expand All @@ -43,34 +43,37 @@ export class PackageInstalledListCommand extends SfCommand<PackageInstalledComma

public async run(): Promise<PackageInstalledCommandResult> {
const { flags } = await this.parse(PackageInstalledListCommand);
const result = await SubscriberPackageVersion.installedList(
flags['target-org'].getConnection(flags['api-version'])
);

const records = result.map((record) => ({
Id: record.Id,
SubscriberPackageId: record.SubscriberPackageId,
SubscriberPackageName: record.SubscriberPackage?.Name,
SubscriberPackageNamespace: record.SubscriberPackage?.NamespacePrefix,
SubscriberPackageVersionId: record.SubscriberPackageVersion?.Id,
SubscriberPackageVersionName: record.SubscriberPackageVersion?.Name,
SubscriberPackageVersionNumber: `${record.SubscriberPackageVersion?.MajorVersion}.${record.SubscriberPackageVersion?.MinorVersion}.${record.SubscriberPackageVersion?.PatchVersion}.${record.SubscriberPackageVersion?.BuildNumber}`,
}));
const records = (
await SubscriberPackageVersion.installedList(flags['target-org'].getConnection(flags['api-version']))
).map(transformRow);

this.table(
records,
{
Id: { header: 'ID' },
SubscriberPackageId: { header: 'Package ID' },
SubscriberPackageName: { header: 'Package Name' },
SubscriberPackageNamespace: { header: 'Namespace' },
SubscriberPackageVersionId: { header: 'Package Version ID' },
SubscriberPackageVersionName: { header: 'Version Name' },
SubscriberPackageVersionNumber: { header: 'Version' },
},
columns,

{ 'no-truncate': true }
);

return records;
}
}

const transformRow = (r: InstalledPackages): PackageInstalledListResult => ({
Id: r.Id,
SubscriberPackageId: r.SubscriberPackageId,
SubscriberPackageName: r.SubscriberPackage?.Name,
SubscriberPackageNamespace: r.SubscriberPackage?.NamespacePrefix,
SubscriberPackageVersionId: r.SubscriberPackageVersion?.Id,
SubscriberPackageVersionName: r.SubscriberPackageVersion?.Name,
SubscriberPackageVersionNumber: `${r.SubscriberPackageVersion?.MajorVersion}.${r.SubscriberPackageVersion?.MinorVersion}.${r.SubscriberPackageVersion?.PatchVersion}.${r.SubscriberPackageVersion?.BuildNumber}`,
});

const columns = {
Id: { header: 'ID' },
SubscriberPackageId: { header: 'Package ID' },
SubscriberPackageName: { header: 'Package Name' },
SubscriberPackageNamespace: { header: 'Namespace' },
SubscriberPackageVersionId: { header: 'Package Version ID' },
SubscriberPackageVersionName: { header: 'Version Name' },
SubscriberPackageVersionNumber: { header: 'Version' },
};
148 changes: 68 additions & 80 deletions src/commands/package/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@ import { Flags, loglevel, orgApiVersionFlagWithDeprecations, SfCommand } from '@
import { Messages } from '@salesforce/core/messages';
import { Package, PackagingSObjects } from '@salesforce/packaging';
import chalk from 'chalk';
import { SfProject } from '@salesforce/core';
import { requiredHubFlag } from '../../utils/hubFlag.js';

Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_list');

export type Package2Result = Partial<
Pick<
PackagingSObjects.Package2,
| 'Id'
| 'SubscriberPackageId'
| 'Name'
| 'Description'
| 'NamespacePrefix'
| 'ContainerOptions'
| 'ConvertedFromPackageId'
| 'PackageErrorUsername'
| 'AppAnalyticsEnabled'
> & {
Alias: string;
CreatedBy: string;
IsOrgDependent: string;
}
>;
export type Package2Result = Required<Pick<PackagingSObjects.Package2, 'Id' | 'Name'>> &
Partial<
Pick<
PackagingSObjects.Package2,
| 'SubscriberPackageId'
| 'Description'
| 'NamespacePrefix'
| 'ContainerOptions'
| 'ConvertedFromPackageId'
| 'PackageErrorUsername'
| 'AppAnalyticsEnabled'
> & {
Alias: string;
CreatedBy: string;
IsOrgDependent: string;
}
>;

export type PackageListCommandResult = Package2Result[];

Expand All @@ -50,55 +50,17 @@ export class PackageListCommand extends SfCommand<PackageListCommandResult> {
}),
};

private results: Package2Result[] = [];

public async run(): Promise<PackageListCommandResult> {
const { flags } = await this.parse(PackageListCommand);
const connection = flags['target-dev-hub'].getConnection(flags['api-version']);
const queryResult = await Package.list(connection);
this.mapRecordsToResults(queryResult);
this.displayResults(flags.verbose, connection.getApiVersion());
return this.results;
}

private mapRecordsToResults(records: PackagingSObjects.Package2[]): void {
if (records && records.length > 0) {
this.results = records
.filter((record) => record.IsDeprecated === false)
.map(
({
Id,
SubscriberPackageId,
Name,
Description,
NamespacePrefix,
ContainerOptions,
ConvertedFromPackageId,
IsOrgDependent,
PackageErrorUsername,
AppAnalyticsEnabled,
CreatedById,
}) =>
({
Id,
SubscriberPackageId,
Name,
Description,
NamespacePrefix,
ContainerOptions,
ConvertedFromPackageId,
Alias: this.project ? this.project.getAliasesFromPackageId(Id).join() : '',
IsOrgDependent: ContainerOptions === 'Managed' ? 'N/A' : IsOrgDependent ? 'Yes' : 'No',
PackageErrorUsername,
AppAnalyticsEnabled,
CreatedBy: CreatedById,
} as Package2Result)
);
}
const results = mapRecordsToResults(queryResult);
this.displayResults(results, flags.verbose, connection.getApiVersion());
return results;
}

private displayResults(verbose = false, apiVersion: string): void {
this.styledHeader(chalk.blue(`Packages [${this.results.length}]`));
private displayResults(results: Package2Result[], verbose = false, apiVersion: string): void {
this.styledHeader(chalk.blue(`Packages [${results.length}]`));
const columns = {
NamespacePrefix: { header: messages.getMessage('namespace') },
Name: { header: messages.getMessage('name') },
Expand All @@ -108,26 +70,52 @@ export class PackageListCommand extends SfCommand<PackageListCommandResult> {
ContainerOptions: {
header: messages.getMessage('package-type'),
},
...(verbose
? {
SubscriberPackageId: { header: messages.getMessage('package-id') },
ConvertedFromPackageId: { header: messages.getMessage('convertedFromPackageId') },
IsOrgDependent: { header: messages.getMessage('isOrgDependent') },
PackageErrorUsername: { header: messages.getMessage('error-notification-username') },
CreatedBy: { header: messages.getMessage('createdBy') },
}
: {}),
...(verbose && parseInt(apiVersion, 10) >= 59
? { AppAnalyticsEnabled: { header: messages.getMessage('app-analytics-enabled') } }
: {}),
};

if (verbose) {
Object.assign(columns, {
SubscriberPackageId: { header: messages.getMessage('package-id') },
ConvertedFromPackageId: { header: messages.getMessage('convertedFromPackageId') },
IsOrgDependent: { header: messages.getMessage('isOrgDependent') },
PackageErrorUsername: { header: messages.getMessage('error-notification-username') },
});
if (apiVersion >= '59.0') {
Object.assign(columns, {
AppAnalyticsEnabled: { header: messages.getMessage('app-analytics-enabled') },
});
}
Object.assign(columns, {
CreatedBy: {
header: messages.getMessage('createdBy'),
},
});
}
this.table(this.results, columns);
this.table(results, columns);
}
}

const mapRecordsToResults = (records: PackagingSObjects.Package2[], project?: SfProject): Package2Result[] =>
records
.filter((record) => record.IsDeprecated === false)
.map(
({
Id,
SubscriberPackageId,
Name,
Description,
NamespacePrefix,
ContainerOptions,
ConvertedFromPackageId,
IsOrgDependent,
PackageErrorUsername,
AppAnalyticsEnabled,
CreatedById,
}) => ({
Id,
SubscriberPackageId,
Name,
Description,
NamespacePrefix,
ContainerOptions,
ConvertedFromPackageId,
Alias: project ? project.getAliasesFromPackageId(Id).join() : '',
IsOrgDependent: ContainerOptions === 'Managed' ? 'N/A' : IsOrgDependent ? 'Yes' : 'No',
PackageErrorUsername,
AppAnalyticsEnabled,
CreatedBy: CreatedById,
})
);
14 changes: 7 additions & 7 deletions test/commands/package/packageList.nut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@
*/

import { execCmd, TestSession } from '@salesforce/cli-plugins-testkit';
import chai from 'chai';
import { expect, config } from 'chai';
import { Org } from '@salesforce/core';
import { Package } from '@salesforce/packaging';

const { expect } = chai;
config.truncateThreshold = 50_000;

describe('package list', () => {
let session: TestSession;
let hubOrg: Org;
let apiVersion: string;
let apiVersion: number;
before(async () => {
session = await TestSession.create({
devhubAuthStrategy: 'AUTO',
});
hubOrg = await Org.create({ aliasOrUsername: session.hubOrg.username });
apiVersion = hubOrg.getConnection().getApiVersion();
apiVersion = parseInt(hubOrg.getConnection().getApiVersion(), 10);
});

after(async () => {
Expand All @@ -45,7 +45,7 @@ describe('package list', () => {
'CreatedBy',
'IsOrgDependent',
];
if (apiVersion < '59.0') {
if (apiVersion < 59) {
keys.splice(keys.indexOf('AppAnalyticsEnabled'), 1);
}
const deprecatedPackages = packages.filter((pkg) => pkg.IsDeprecated);
Expand All @@ -66,8 +66,8 @@ describe('package list', () => {
const output = execCmd(command, { ensureExitCode: 0 }).shellOutput.stdout;
expect(output).to.contain('=== Packages');
let headerExpression =
'Namespace Prefix\\s+?Name\\s+?Id\\s+?Alias\\s+?Description\\s+?Type\\s+?Subscriber Package Id\\s+?Converted From Package Id\\s+?Org-Dependent Unlocked Package\\s+?Error Notification Username\\s+?App Analytics Enabled\\s+?Created By';
if (apiVersion < '59.0') {
'Namespace Prefix\\s+?Name\\s+?Id\\s+?Alias\\s+?Description\\s+?Type\\s+?Subscriber Package Id\\s+?Converted From Package Id\\s+?Org-Dependent Unlocked Package\\s+?Error Notification Username\\s+?Created By\\s+?App Analytics Enabled';
if (apiVersion < 59.0) {
headerExpression = headerExpression.replace('App Analytics Enabled\\s+?', '');
}
expect(output).to.match(new RegExp(headerExpression));
Expand Down
Loading