Skip to content

Commit

Permalink
fix: add package:version:uninstall:report command, NUT, cleanup SfErr…
Browse files Browse the repository at this point in the history
…or/message
  • Loading branch information
WillieRuemmele committed Aug 30, 2022
1 parent e67f127 commit d9f72f2
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 11 deletions.
10 changes: 7 additions & 3 deletions messages/package_uninstall_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ retrieve status of package uninstall request

# examples

$ sfdx force:package:uninstall:report -i 06y...
$ sfdx force:package:uninstall:report -i 06y... -u me@example.com
$ sfdx force:package:beta:uninstall:report -i 06y...
$ sfdx force:package:beta:uninstall:report -i 06y... -u me@example.com

# requestId

Expand All @@ -18,7 +18,7 @@ The ID of the package uninstall request you want to check.
# InProgress

PackageUninstallRequest is currently InProgress. You can continue to query the status using
sfdx force:package:uninstall:report -i %s -u %s
sfdx force:package:beta:uninstall:report -i %s -u %s

# Unknown

Expand All @@ -27,3 +27,7 @@ TODO: fix me
# Success

Successfully uninstalled package [%s]

# packageIdInvalid

Verify that you entered a valid package uninstall request ID (starts with 06y) and try again.
40 changes: 36 additions & 4 deletions src/commands/force/package/beta/uninstall/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

import * as os from 'os';
import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';
import { Messages } from '@salesforce/core';
import { Messages, SfError } from '@salesforce/core';
import { Package, PackagingSObjects } from '@salesforce/packaging';

Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_uninstall_report');
const pkgUninstall = Messages.loadMessages('@salesforce/plugin-packaging', 'package_uninstall');

export class PackageUninstallReportCommand extends SfdxCommand {
public static readonly description = messages.getMessage('cliDescription');
Expand All @@ -22,11 +24,41 @@ export class PackageUninstallReportCommand extends SfdxCommand {
description: messages.getMessage('requestId'),
longDescription: messages.getMessage('requestIdLong'),
required: true,
validate: (id) => {
if (id.startsWith('06y') && [18, 15].includes(id.length)) {
return true;
} else {
throw messages.createError('packageIdInvalid');
}
},
}),
};

// eslint-disable-next-line @typescript-eslint/require-await
public async run(): Promise<unknown> {
throw new Error('Beta command not yet implemented');
public async run(): Promise<PackagingSObjects.SubscriberPackageVersionUninstallRequest> {
const requestId = this.flags.requestid as string;
const pkg = new Package({ connection: this.org.getConnection() });
const result = await pkg.uninstallReport(requestId);

if (result.Status === 'Error') {
const err = pkgUninstall.getMessage('defaultErrorMessage', [requestId, result.Id]);
const errorDetails = await this.org
.getConnection()
.tooling.query<{ Message: string }>(
`SELECT Message FROM PackageVersionUninstallRequestError WHERE ParentRequest.Id = '${requestId}' ORDER BY Message`
);
const errors: string[] = [];
errorDetails.records.forEach((record) => {
errors.push(`(${errors.length + 1}) ${record.Message}`);
});
const errHeader = errors.length > 0 ? `\n=== Errors\n${errors.join('\n')}` : '';

throw new SfError(`${err}${errHeader}`, 'UNINSTALL_ERROR', [pkgUninstall.getMessage('action')]);
} else {
const arg =
result.Status === 'Success' ? [result.SubscriberPackageVersionId] : [result.Id, this.org.getUsername()];
this.ux.log(messages.getMessage(result.Status, arg));
}

return result;
}
}
4 changes: 2 additions & 2 deletions src/commands/force/package1/beta/version/display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';
import { Messages, SfError } from '@salesforce/core';
import { Messages } from '@salesforce/core';
import { Package1Display, package1Display } from '@salesforce/packaging';

Messages.importMessagesDirectory(__dirname);
Expand All @@ -25,7 +25,7 @@ export class Package1VersionDisplayCommand extends SfdxCommand {
if (id.startsWith('04t') && [18, 15].includes(id.length)) {
return true;
} else {
throw new SfError(messages.getMessage('packageIdInvalid'));
throw messages.createError('packageIdInvalid');
}
},
}),
Expand Down
4 changes: 2 additions & 2 deletions src/commands/force/package1/beta/version/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';
import { Messages, SfError } from '@salesforce/core';
import { Messages } from '@salesforce/core';
import { Package1Display, package1VersionList } from '@salesforce/packaging';

Messages.importMessagesDirectory(__dirname);
Expand All @@ -24,7 +24,7 @@ export class Package1VersionListCommand extends SfdxCommand {
if (id.startsWith('033') && [18, 15].includes(id.length)) {
return true;
} else {
throw new SfError(messages.getMessage('packageIdInvalid'));
throw messages.createError('packageIdInvalid');
}
},
}),
Expand Down
25 changes: 25 additions & 0 deletions test/commands/force/package/install.nut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import { execCmd, TestSession } from '@salesforce/cli-plugins-testkit';
import { expect } from 'chai';
import { PackagingSObjects } from '@salesforce/packaging';

type PackageInstallRequest = PackagingSObjects.PackageInstallRequest;
type PackageUninstallRequest = PackagingSObjects.SubscriberPackageVersionUninstallRequest;

describe('package install', () => {
let session: TestSession;
Expand Down Expand Up @@ -40,4 +42,27 @@ describe('package install', () => {
expect(reportJson).to.have.property('Status');
expect(['IN_PROGRESS', 'SUCCESS']).to.include(reportJson.Status);
});

it('should start an uninstall request, and report on it', () => {
const uninstallCommand = 'force:package:beta:uninstall -p 04t6A000002zgKSQAY --json -w 0';
const uninstallRequest = execCmd<PackageUninstallRequest>(uninstallCommand, {
ensureExitCode: 0,
}).jsonOutput.result;
expect(uninstallRequest).to.have.property('Status', 'IN_PROGRESS');
expect(uninstallRequest.Id.startsWith('06y')).to.be.true;

const uninstallReportCommand = `force:package:beta:uninstall:report -i ${uninstallRequest.Id} --json`;
const uninstallReportResult = execCmd(uninstallReportCommand, { ensureExitCode: 0 }).jsonOutput.result;
expect(uninstallReportResult).to.have.all.keys(
'Id',
'IsDeleted',
'CreatedDate',
'CreatedById',
'LastModifiedDate',
'LastModifiedById',
'SystemModstamp',
'SubscriberPackageVersionId',
'Status'
);
});
});

0 comments on commit d9f72f2

Please sign in to comment.