Skip to content

Commit d9f72f2

Browse files
fix: add package:version:uninstall:report command, NUT, cleanup SfError/message
1 parent e67f127 commit d9f72f2

File tree

5 files changed

+72
-11
lines changed

5 files changed

+72
-11
lines changed

messages/package_uninstall_report.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ retrieve status of package uninstall request
44

55
# examples
66

7-
$ sfdx force:package:uninstall:report -i 06y...
8-
$ sfdx force:package:uninstall:report -i 06y... -u [email protected]
7+
$ sfdx force:package:beta:uninstall:report -i 06y...
8+
$ sfdx force:package:beta:uninstall:report -i 06y... -u [email protected]
99

1010
# requestId
1111

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

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

2323
# Unknown
2424

@@ -27,3 +27,7 @@ TODO: fix me
2727
# Success
2828

2929
Successfully uninstalled package [%s]
30+
31+
# packageIdInvalid
32+
33+
Verify that you entered a valid package uninstall request ID (starts with 06y) and try again.

src/commands/force/package/beta/uninstall/report.ts

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77

88
import * as os from 'os';
99
import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';
10-
import { Messages } from '@salesforce/core';
10+
import { Messages, SfError } from '@salesforce/core';
11+
import { Package, PackagingSObjects } from '@salesforce/packaging';
1112

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

1517
export class PackageUninstallReportCommand extends SfdxCommand {
1618
public static readonly description = messages.getMessage('cliDescription');
@@ -22,11 +24,41 @@ export class PackageUninstallReportCommand extends SfdxCommand {
2224
description: messages.getMessage('requestId'),
2325
longDescription: messages.getMessage('requestIdLong'),
2426
required: true,
27+
validate: (id) => {
28+
if (id.startsWith('06y') && [18, 15].includes(id.length)) {
29+
return true;
30+
} else {
31+
throw messages.createError('packageIdInvalid');
32+
}
33+
},
2534
}),
2635
};
2736

28-
// eslint-disable-next-line @typescript-eslint/require-await
29-
public async run(): Promise<unknown> {
30-
throw new Error('Beta command not yet implemented');
37+
public async run(): Promise<PackagingSObjects.SubscriberPackageVersionUninstallRequest> {
38+
const requestId = this.flags.requestid as string;
39+
const pkg = new Package({ connection: this.org.getConnection() });
40+
const result = await pkg.uninstallReport(requestId);
41+
42+
if (result.Status === 'Error') {
43+
const err = pkgUninstall.getMessage('defaultErrorMessage', [requestId, result.Id]);
44+
const errorDetails = await this.org
45+
.getConnection()
46+
.tooling.query<{ Message: string }>(
47+
`SELECT Message FROM PackageVersionUninstallRequestError WHERE ParentRequest.Id = '${requestId}' ORDER BY Message`
48+
);
49+
const errors: string[] = [];
50+
errorDetails.records.forEach((record) => {
51+
errors.push(`(${errors.length + 1}) ${record.Message}`);
52+
});
53+
const errHeader = errors.length > 0 ? `\n=== Errors\n${errors.join('\n')}` : '';
54+
55+
throw new SfError(`${err}${errHeader}`, 'UNINSTALL_ERROR', [pkgUninstall.getMessage('action')]);
56+
} else {
57+
const arg =
58+
result.Status === 'Success' ? [result.SubscriberPackageVersionId] : [result.Id, this.org.getUsername()];
59+
this.ux.log(messages.getMessage(result.Status, arg));
60+
}
61+
62+
return result;
3163
}
3264
}

src/commands/force/package1/beta/version/display.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';
9-
import { Messages, SfError } from '@salesforce/core';
9+
import { Messages } from '@salesforce/core';
1010
import { Package1Display, package1Display } from '@salesforce/packaging';
1111

1212
Messages.importMessagesDirectory(__dirname);
@@ -25,7 +25,7 @@ export class Package1VersionDisplayCommand extends SfdxCommand {
2525
if (id.startsWith('04t') && [18, 15].includes(id.length)) {
2626
return true;
2727
} else {
28-
throw new SfError(messages.getMessage('packageIdInvalid'));
28+
throw messages.createError('packageIdInvalid');
2929
}
3030
},
3131
}),

src/commands/force/package1/beta/version/list.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';
9-
import { Messages, SfError } from '@salesforce/core';
9+
import { Messages } from '@salesforce/core';
1010
import { Package1Display, package1VersionList } from '@salesforce/packaging';
1111

1212
Messages.importMessagesDirectory(__dirname);
@@ -24,7 +24,7 @@ export class Package1VersionListCommand extends SfdxCommand {
2424
if (id.startsWith('033') && [18, 15].includes(id.length)) {
2525
return true;
2626
} else {
27-
throw new SfError(messages.getMessage('packageIdInvalid'));
27+
throw messages.createError('packageIdInvalid');
2828
}
2929
},
3030
}),

test/commands/force/package/install.nut.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import { execCmd, TestSession } from '@salesforce/cli-plugins-testkit';
99
import { expect } from 'chai';
1010
import { PackagingSObjects } from '@salesforce/packaging';
11+
1112
type PackageInstallRequest = PackagingSObjects.PackageInstallRequest;
13+
type PackageUninstallRequest = PackagingSObjects.SubscriberPackageVersionUninstallRequest;
1214

1315
describe('package install', () => {
1416
let session: TestSession;
@@ -40,4 +42,27 @@ describe('package install', () => {
4042
expect(reportJson).to.have.property('Status');
4143
expect(['IN_PROGRESS', 'SUCCESS']).to.include(reportJson.Status);
4244
});
45+
46+
it('should start an uninstall request, and report on it', () => {
47+
const uninstallCommand = 'force:package:beta:uninstall -p 04t6A000002zgKSQAY --json -w 0';
48+
const uninstallRequest = execCmd<PackageUninstallRequest>(uninstallCommand, {
49+
ensureExitCode: 0,
50+
}).jsonOutput.result;
51+
expect(uninstallRequest).to.have.property('Status', 'IN_PROGRESS');
52+
expect(uninstallRequest.Id.startsWith('06y')).to.be.true;
53+
54+
const uninstallReportCommand = `force:package:beta:uninstall:report -i ${uninstallRequest.Id} --json`;
55+
const uninstallReportResult = execCmd(uninstallReportCommand, { ensureExitCode: 0 }).jsonOutput.result;
56+
expect(uninstallReportResult).to.have.all.keys(
57+
'Id',
58+
'IsDeleted',
59+
'CreatedDate',
60+
'CreatedById',
61+
'LastModifiedDate',
62+
'LastModifiedById',
63+
'SystemModstamp',
64+
'SubscriberPackageVersionId',
65+
'Status'
66+
);
67+
});
4368
});

0 commit comments

Comments
 (0)