Skip to content

Commit

Permalink
feat: add mdapi:deploy:cancel command, refactor base classes to suppo…
Browse files Browse the repository at this point in the history
…rt MDAPI and SOURCE stash keys (#301)

* fix: add mdapi:deploy:cancel command, refactor base classes to support MDAPI and SOURCE stash keys

* chore: remove helpers from kit

* chore: address QA issues
  • Loading branch information
WillieRuemmele authored Nov 16, 2021
1 parent 25beb65 commit 9dcc3e7
Show file tree
Hide file tree
Showing 15 changed files with 382 additions and 60 deletions.
25 changes: 15 additions & 10 deletions command-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
[
{
"command": "force:mdapi:deploy:cancel",
"plugin": "@salesforce/plugin-source",
"flags": ["apiversion", "jobid", "json", "loglevel", "targetusername", "wait"]
},
{
"command": "force:mdapi:describemetadata",
"plugin": "@salesforce/plugin-source",
"flags": ["apiversion", "filterknown", "json", "loglevel", "resultfile", "targetusername"]
},
{
"command": "force:mdapi:listmetadata",
"plugin": "@salesforce/plugin-source",
"flags": ["apiversion", "folder", "json", "loglevel", "metadatatype", "resultfile", "targetusername"]
},
{
"command": "force:source:beta:pull",
"plugin": "@salesforce/plugin-source",
Expand Down Expand Up @@ -105,15 +120,5 @@
"verbose",
"wait"
]
},
{
"command": "force:mdapi:listmetadata",
"plugin": "@salesforce/plugin-source",
"flags": ["apiversion", "json", "loglevel", "resultfile", "targetusername", "metadatatype", "folder"]
},
{
"command": "force:mdapi:describemetadata",
"plugin": "@salesforce/plugin-source",
"flags": ["apiversion", "json", "loglevel", "resultfile", "targetusername", "filterknown"]
}
]
3 changes: 2 additions & 1 deletion messages/cancel.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
},
"flagsLong": {
"wait": "Number of minutes to wait for the command to complete and display results to the terminal window. If the command continues to run after the wait period, the CLI returns control of the terminal window to you. "
}
},
"CancelFailed": "The cancel command failed due to: %s"
}
3 changes: 2 additions & 1 deletion messages/deploy.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,6 @@
"deployFailed": "Deploy failed.",
"asyncDeployQueued": "Deploy has been queued.",
"asyncDeployCancel": "Run sfdx force:source:deploy:cancel -i %s to cancel the deploy.",
"asyncDeployReport": "Run sfdx force:source:deploy:report -i %s to get the latest status."
"asyncDeployReport": "Run sfdx force:source:deploy:report -i %s to get the latest status.",
"invalidDeployId": "The provided ID is invalid, deploy IDs must start with '0Af'"
}
20 changes: 20 additions & 0 deletions messages/md.cancel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"description": "cancel a metadata deployment \n Use this command to cancel a specified asynchronous metadata deployment. You can also specify a wait time (in minutes) to check for updates to the canceled deploy status.",
"longDescription": "Cancels an asynchronous metadata deployment.",
"flags": {
"wait": "wait time for command to finish in minutes",
"waitLong": "Number of minutes to wait for the command to complete and display results to the terminal window. If the command continues to run after the wait period, the CLI returns control of the terminal window to you. The default is 33 minutes.",
"jobid": "job ID of the deployment you want to cancel; defaults to your most recent CLI deployment if not specified"
},

"examples": [
"Deploy a directory of files to the org",
" $ sfdx force:mdapi:deploy -d <directory>",
"Now cancel this deployment and wait two minutes",
" $ sfdx force:mdapi:deploy:cancel -w 2",
"If you have multiple deployments in progress and want to cancel a specific one, specify the job ID",
" $ sfdx force:mdapi:deploy:cancel -i <jobid>",
"Check the status of the cancel job",
" $ sfdx force:mdapi:deploy:report"
]
}
85 changes: 85 additions & 0 deletions src/commands/force/mdapi/deploy/cancel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import * as os from 'os';
import { flags, FlagsConfig } from '@salesforce/command';
import { Messages, SfdxError } from '@salesforce/core';
import { Duration } from '@salesforce/kit';
import { RequestStatus } from '@salesforce/source-deploy-retrieve';
import { DeployCommand } from '../../../../deployCommand';
import {
DeployCancelCommandResult,
DeployCancelResultFormatter,
} from '../../../../formatters/deployCancelResultFormatter';

Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/plugin-source', 'md.cancel');

export class Cancel extends DeployCommand {
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessage('examples').split(os.EOL);
public static readonly requiresUsername = true;
public static readonly flagsConfig: FlagsConfig = {
wait: flags.minutes({
char: 'w',
default: Duration.minutes(DeployCommand.DEFAULT_WAIT_MINUTES),
min: Duration.minutes(1),
description: messages.getMessage('flags.wait'),
longDescription: messages.getMessage('flags.waitLong'),
}),
jobid: flags.id({
char: 'i',
description: messages.getMessage('flags.jobid'),
validate: (val) => {
if (val.startsWith('0Af')) {
return true;
} else {
throw SfdxError.create('@salesforce/plugin-source', 'deploy', 'invalidDeployId');
}
},
}),
};
// The most important difference between this and source:deploy:cancel
public isSourceStash = false;

public async run(): Promise<DeployCancelCommandResult> {
await this.cancel();
this.resolveSuccess();
return this.formatResult();
}

protected async cancel(): Promise<void> {
const deployId = this.resolveDeployId(this.getFlag<string>('jobid'));
try {
const deploy = this.createDeploy(deployId);
await deploy.cancel();

this.deployResult = await this.poll(deployId);
} catch (e) {
if (e instanceof Error) {
throw SfdxError.create('@salesforce/plugin-source', 'cancel', 'CancelFailed', [e.message]);
} else {
throw SfdxError.wrap(e);
}
}
}

protected resolveSuccess(): void {
const status = this.deployResult.response.status;
if (status !== RequestStatus.Canceled) {
this.setExitCode(1);
}
}

protected formatResult(): DeployCancelCommandResult {
const formatter = new DeployCancelResultFormatter(this.logger, this.ux, this.deployResult);
if (!this.isJsonOutput()) {
formatter.display();
}
return formatter.getJson();
}
}
2 changes: 1 addition & 1 deletion src/commands/force/source/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class Delete extends DeployCommand {
}),
wait: flags.minutes({
char: 'w',
default: Duration.minutes(Delete.DEFAULT_SRC_WAIT_MINUTES),
default: Duration.minutes(Delete.DEFAULT_WAIT_MINUTES),
min: Duration.minutes(1),
description: messages.getMessage('flags.wait'),
longDescription: messages.getMessage('flagsLong.wait'),
Expand Down
2 changes: 1 addition & 1 deletion src/commands/force/source/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class Deploy extends DeployCommand {
}),
wait: flags.minutes({
char: 'w',
default: Duration.minutes(Deploy.DEFAULT_SRC_WAIT_MINUTES),
default: Duration.minutes(Deploy.DEFAULT_WAIT_MINUTES),
min: Duration.minutes(0), // wait=0 means deploy is asynchronous
description: messages.getMessage('flags.wait'),
longDescription: messages.getMessage('flagsLong.wait'),
Expand Down
32 changes: 21 additions & 11 deletions src/commands/force/source/deploy/cancel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@

import * as os from 'os';
import { flags, FlagsConfig } from '@salesforce/command';
import { Messages } from '@salesforce/core';
import { Messages, SfdxError } from '@salesforce/core';
import { Duration } from '@salesforce/kit';
import { getString } from '@salesforce/ts-types';
import { RequestStatus } from '@salesforce/source-deploy-retrieve';
import { DeployCommand } from '../../../../deployCommand';
import {
Expand All @@ -27,14 +26,21 @@ export class Cancel extends DeployCommand {
public static readonly flagsConfig: FlagsConfig = {
wait: flags.minutes({
char: 'w',
default: Duration.minutes(DeployCommand.DEFAULT_SRC_WAIT_MINUTES),
default: Duration.minutes(DeployCommand.DEFAULT_WAIT_MINUTES),
min: Duration.minutes(1),
description: messages.getMessage('flags.wait'),
longDescription: messages.getMessage('flagsLong.wait'),
}),
jobid: flags.id({
char: 'i',
description: messages.getMessage('flags.jobid'),
validate: (val) => {
if (val.startsWith('0Af')) {
return true;
} else {
throw SfdxError.create('@salesforce/plugin-source', 'deploy', 'invalidDeployId');
}
},
}),
};

Expand All @@ -46,18 +52,22 @@ export class Cancel extends DeployCommand {

protected async cancel(): Promise<void> {
const deployId = this.resolveDeployId(this.getFlag<string>('jobid'));
try {
const deploy = this.createDeploy(deployId);
await deploy.cancel();

// TODO: update to use SDRL. This matches the toolbelt implementation.
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-assignment
await this.org.getConnection().metadata['_invoke']('cancelDeploy', {
deployId,
});

this.deployResult = await this.poll(deployId);
this.deployResult = await this.poll(deployId);
} catch (e) {
if (e instanceof Error) {
throw SfdxError.create('@salesforce/plugin-source', 'cancel', 'CancelFailed', [e.message]);
} else {
throw SfdxError.wrap(e);
}
}
}

protected resolveSuccess(): void {
const status = getString(this.deployResult, 'response.status');
const status = this.deployResult.response.status;
if (status !== RequestStatus.Canceled) {
this.setExitCode(1);
}
Expand Down
21 changes: 9 additions & 12 deletions src/commands/force/source/deploy/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
*/

import * as os from 'os';
import { Messages, SfdxProject } from '@salesforce/core';
import { Messages, SfdxError, SfdxProject } from '@salesforce/core';
import { flags, FlagsConfig } from '@salesforce/command';
import { Duration, env } from '@salesforce/kit';
import { MetadataApiDeploy } from '@salesforce/source-deploy-retrieve';
import { DeployCommand } from '../../../../deployCommand';
import {
DeployReportCommandResult,
Expand All @@ -30,7 +29,7 @@ export class Report extends DeployCommand {
public static readonly flagsConfig: FlagsConfig = {
wait: flags.minutes({
char: 'w',
default: Duration.minutes(DeployCommand.DEFAULT_SRC_WAIT_MINUTES),
default: Duration.minutes(DeployCommand.DEFAULT_WAIT_MINUTES),
min: Duration.minutes(1),
description: messages.getMessage('flags.wait'),
longDescription: messages.getMessage('flagsLong.wait'),
Expand All @@ -39,6 +38,13 @@ export class Report extends DeployCommand {
char: 'i',
description: messages.getMessage('flags.jobid'),
longDescription: messages.getMessage('flagsLong.jobid'),
validate: (val) => {
if (val.startsWith('0Af')) {
return true;
} else {
throw SfdxError.create('@salesforce/plugin-source', 'deploy', 'invalidDeployId');
}
},
}),
verbose: flags.builtin({
description: messages.getMessage('flags.verbose'),
Expand All @@ -50,15 +56,6 @@ export class Report extends DeployCommand {
return this.formatResult();
}

/**
* This method is here to provide a workaround to stubbing a constructor in the tests.
*
* @param id
*/
public createDeploy(id?: string): MetadataApiDeploy {
return new MetadataApiDeploy({ usernameOrConnection: this.org.getUsername(), id });
}

protected async doReport(): Promise<void> {
const deployId = this.resolveDeployId(this.getFlag<string>('jobid'));

Expand Down
8 changes: 4 additions & 4 deletions src/commands/force/source/retrieve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { join } from 'path';
import { flags, FlagsConfig } from '@salesforce/command';
import { Messages, SfdxProject } from '@salesforce/core';
import { Duration } from '@salesforce/kit';
import { RetrieveResult, ComponentSet, RequestStatus } from '@salesforce/source-deploy-retrieve';
import { ComponentSet, RequestStatus, RetrieveResult } from '@salesforce/source-deploy-retrieve';
import { SourceCommand } from '../../../sourceCommand';
import {
RetrieveResultFormatter,
RetrieveCommandResult,
PackageRetrieval,
RetrieveCommandResult,
RetrieveResultFormatter,
} from '../../../formatters/retrieveResultFormatter';
import { ComponentSetBuilder } from '../../../componentSetBuilder';

Expand All @@ -41,7 +41,7 @@ export class Retrieve extends SourceCommand {
}),
wait: flags.minutes({
char: 'w',
default: Duration.minutes(SourceCommand.DEFAULT_SRC_WAIT_MINUTES),
default: Duration.minutes(SourceCommand.DEFAULT_WAIT_MINUTES),
min: Duration.minutes(1),
description: messages.getMessage('flags.wait'),
longDescription: messages.getMessage('flagsLong.wait'),
Expand Down
Loading

0 comments on commit 9dcc3e7

Please sign in to comment.