Skip to content

Commit d749b0f

Browse files
committed
Merge branch 'main' into phale/pkg-create-delete
2 parents d55fe0f + f9d3c9d commit d749b0f

File tree

8 files changed

+168
-23
lines changed

8 files changed

+168
-23
lines changed

messages/default.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -958,11 +958,11 @@ Verify that you entered a valid package version ID and try again.
958958

959959
# package2VersionUpdateSetAsReleasedYesNo
960960

961-
Are you sure you want to release package version %s? You can't undo this action. Release package (y\nn)?
961+
Are you sure you want to release package version %s? You can't undo this action. Release package (y/n)?
962962

963963
# packageVersionUpdateSetAsReleasedYesNo
964964

965-
Are you sure you want to release package version %s? You can't undo this action. Release package (y\nn)?
965+
Are you sure you want to release package version %s? You can't undo this action. Release package (y/n)?
966966

967967
# attemptingToDeleteExpiredOrDeleted
968968

messages/package_delete.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ Don’t prompt before deleting the package.
4242

4343
Deleted packages can’t be recovered.
4444

45-
Do you want to continue? (y\n)
45+
Do you want to continue? (y/n)
4646

4747
# promptUndelete
4848

4949
This will undelete the package, which may result in unintended consequences for customers. Proceed with caution.
5050

51-
Do you want to continue? (y\n)
51+
Do you want to continue? (y/n)
5252

5353
# promptDeleteDeny
5454

messages/package_version_create.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,4 +298,4 @@ Package version creation failed with unknown error.
298298

299299
# malformedUrl
300300

301-
The %s value "%s" from the command line or sfdx-project.json is not in the correct format for a URL. It must be a valid URL in the format "http:\n\nsalesforce.com". More information: https:\n\nnodejs.org\napi\nurl.html#url_url_strings_and_url_objects
301+
The %s value "%s" from the command line or sfdx-project.json is not in the correct format for a URL. It must be a valid URL in the format "http://salesforce.com". More information: https://nodejs.org/api/url.html#url_url_strings_and_url_objects

messages/package_version_delete.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ Don’t prompt before deleting the package version.
4242

4343
Deleted package versions can’t be recovered.
4444

45-
Do you want to continue? (y\nn)
45+
Do you want to continue? (y/n)
4646

4747
# promptUndelete
4848

4949
This will undelete the package version, which may result in unintended consequences for customers. Proceed with caution.
5050

51-
Do you want to continue? (y\nn)
51+
Do you want to continue? (y/n)
5252

5353
# promptDeleteDeny
5454

messages/package_version_promote.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ The ID (starts with 04t) or alias of the package version to promote.
2525

2626
# packageVersionPromoteSetAsReleasedYesNo
2727

28-
Are you sure you want to release package version %s? You can't undo this action. Release package (y\nn)?
28+
Are you sure you want to release package version %s? You can't undo this action. Release package (y/n)?
2929

3030
# setasreleasedForce
3131

src/commands/force/package/beta/version/delete.ts

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

88
import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';
9-
import { Messages, SfdxPropertyKeys } from '@salesforce/core';
9+
import { Messages, OrgConfigProperties } from '@salesforce/core';
10+
import { PackageVersion, PackageSaveResult } from '@salesforce/packaging';
1011

1112
Messages.importMessagesDirectory(__dirname);
1213
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_version_delete');
@@ -15,7 +16,7 @@ export class PackageVersionDeleteCommand extends SfdxCommand {
1516
public static readonly description = messages.getMessage('cliDescription');
1617
public static readonly longDescription = messages.getMessage('cliLongDescription');
1718
public static readonly help = messages.getMessage('help', []);
18-
public static readonly orgType = SfdxPropertyKeys.DEFAULT_DEV_HUB_USERNAME;
19+
public static readonly orgType = OrgConfigProperties.TARGET_DEV_HUB;
1920
public static readonly requiresDevhubUsername = true;
2021
public static readonly requiresProject = true;
2122
public static readonly flagsConfig: FlagsConfig = {
@@ -33,13 +34,32 @@ export class PackageVersionDeleteCommand extends SfdxCommand {
3334
undelete: flags.boolean({
3435
description: messages.getMessage('undelete'),
3536
longDescription: messages.getMessage('undeleteLong'),
36-
3737
hidden: true,
3838
}),
3939
};
4040

41-
public async run(): Promise<unknown> {
42-
process.exitCode = 1;
43-
return Promise.resolve('Not yet implemented');
41+
public async run(): Promise<PackageSaveResult> {
42+
const packageVersion = new PackageVersion({ project: this.project, connection: this.hubOrg.getConnection() });
43+
await this.confirmDelete();
44+
const results = this.flags.undelete
45+
? await packageVersion.undelete(this.flags.package)
46+
: await packageVersion.delete(this.flags.package);
47+
this.ux.log(this.getHumanSuccessMessage(results));
48+
return results;
49+
}
50+
51+
private async confirmDelete(): Promise<boolean> {
52+
if (this.flags.noprompt || this.flags.json) {
53+
return true;
54+
}
55+
const message = this.flags.undelete ? messages.getMessage('promptUndelete') : messages.getMessage('promptDelete');
56+
const accepted = await this.ux.confirm(message);
57+
if (!accepted) {
58+
throw new Error(messages.getMessage('promptDeleteDeny'));
59+
}
60+
}
61+
62+
private getHumanSuccessMessage(result: PackageSaveResult): string {
63+
return messages.getMessage(this.flags.undelete ? 'humanSuccessUndelete' : 'humanSuccess', [result.id]);
4464
}
4565
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* Copyright (c) 2020, salesforce.com, inc.
3+
* All rights reserved.
4+
* Licensed under the BSD 3-Clause license.
5+
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6+
*/
7+
import { Org } from '@salesforce/core';
8+
import { testSetup } from '@salesforce/core/lib/testSetup';
9+
import { fromStub, stubInterface, stubMethod } from '@salesforce/ts-sinon';
10+
import { Config } from '@oclif/core';
11+
import { expect } from 'chai';
12+
import { PackageSaveResult, PackageVersion } from '@salesforce/packaging';
13+
import { Result } from '@salesforce/command';
14+
import { PackageVersionDeleteCommand } from '../../../../src/commands/force/package/beta/version/delete';
15+
const $$ = testSetup();
16+
const oclifConfigStub = fromStub(stubInterface<Config>($$.SANDBOX));
17+
let uxLogStub: sinon.SinonStub;
18+
let uxConfirmStub: sinon.SinonStub;
19+
let apiVersionStub: sinon.SinonStub;
20+
let queryStub: sinon.SinonStub;
21+
let packageVersionStub: sinon.SinonStub;
22+
let deleteStub: sinon.SinonStub;
23+
let undeleteStub: sinon.SinonStub;
24+
25+
class TestCommand extends PackageVersionDeleteCommand {
26+
public async runIt(confirm: boolean) {
27+
this.result = new Result(this.statics.result);
28+
await this.init();
29+
uxLogStub = stubMethod($$.SANDBOX, this.ux, 'log');
30+
uxConfirmStub = stubMethod($$.SANDBOX, this.ux, 'confirm');
31+
if (confirm) {
32+
uxConfirmStub.resolves(confirm);
33+
}
34+
this.result.data = await this.run();
35+
await this.finally(undefined);
36+
return this.result.data;
37+
}
38+
public setHubOrg(org: Org) {
39+
this.hubOrg = org;
40+
}
41+
}
42+
43+
const runCmd = async (params: string[], confirm?: boolean) => {
44+
const cmd = new TestCommand(params, oclifConfigStub);
45+
stubMethod($$.SANDBOX, cmd, 'assignOrg').callsFake(() => {
46+
const orgStub = fromStub(
47+
stubInterface<Org>($$.SANDBOX, {
48+
getUsername: () => '[email protected]',
49+
getConnection: () => ({
50+
getApiVersion: apiVersionStub,
51+
tooling: {
52+
query: queryStub,
53+
},
54+
}),
55+
})
56+
);
57+
cmd.setHubOrg(orgStub);
58+
});
59+
return cmd.runIt(confirm);
60+
};
61+
62+
describe('force:package:version:delete', () => {
63+
beforeEach(() => {
64+
apiVersionStub = $$.SANDBOX.stub().returns('55.0');
65+
queryStub = $$.SANDBOX.stub();
66+
deleteStub = $$.SANDBOX.stub();
67+
undeleteStub = $$.SANDBOX.stub();
68+
69+
// The PackageVersion class is tested in the packaging library, so
70+
// we just stub the public APIs used by the command.
71+
packageVersionStub = $$.SANDBOX.stub().callsFake(() => ({
72+
delete: deleteStub,
73+
undelete: undeleteStub,
74+
}));
75+
Object.setPrototypeOf(PackageVersion, packageVersionStub);
76+
});
77+
afterEach(() => {
78+
$$.SANDBOX.restore();
79+
});
80+
81+
it('should error without required --package param', async () => {
82+
try {
83+
await runCmd([]);
84+
expect(false, 'Expected required flag error').to.be.true;
85+
} catch (err) {
86+
const error = err as Error;
87+
expect(error.name).to.equal('Error');
88+
expect(error.message).to.include('Missing required flag:\n -p, --package');
89+
}
90+
});
91+
92+
it('should error pkg version not found in project', async () => {
93+
try {
94+
await runCmd(['-p', '04t6A000002zgKSQAY', '-v', '[email protected]'], true);
95+
expect(false, 'Expected invalid id error').to.be.true;
96+
} catch (err) {
97+
const error = err as Error;
98+
expect(error.name).to.equal('ErrorInvalidIdNoMatchingVersionIdError');
99+
}
100+
});
101+
it('should delete a package version', async () => {
102+
deleteStub.reset();
103+
deleteStub = $$.SANDBOX.stub(PackageVersion.prototype, 'delete').resolves({
104+
errors: [],
105+
id: 'testId',
106+
success: true,
107+
} as PackageSaveResult);
108+
const results: PackageSaveResult = (await runCmd(
109+
['-p', '04t6A000002zgKSQAY', '-v', '[email protected]'],
110+
true
111+
)) as PackageSaveResult;
112+
expect(results.id).to.equal('testId');
113+
expect(uxLogStub.calledOnce).to.be.true;
114+
const msg = 'Successfully deleted the package version. testId';
115+
expect(uxLogStub.args[0][0]).to.equal(msg);
116+
expect(results.id).to.equal('testId');
117+
});
118+
it('should undelete a package version', async () => {
119+
deleteStub.reset();
120+
deleteStub = $$.SANDBOX.stub(PackageVersion.prototype, 'undelete').resolves({
121+
errors: [],
122+
id: 'testId',
123+
success: true,
124+
} as PackageSaveResult);
125+
const results: PackageSaveResult = (await runCmd(
126+
['-p', '04t6A000002zgKSQAY', '-v', '[email protected]', '--undelete'],
127+
true
128+
)) as PackageSaveResult;
129+
expect(uxLogStub.calledOnce).to.be.true;
130+
const msg = 'Successfully undeleted the package version. testId';
131+
expect(uxLogStub.args[0][0]).to.equal(msg);
132+
expect(results.id).to.equal('testId');
133+
});
134+
});

test/commands/force/placeholder.test.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)