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

fix: add deploy:cancel #66

Merged
merged 2 commits into from
Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions command-snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
"wait"
]
},
{
"command": "force:source:deploy:cancel",
"plugin": "@salesforce/plugin-source",
"flags": ["apiversion", "jobid", "json", "loglevel", "targetusername", "wait"]
},
{
"command": "force:source:retrieve",
"plugin": "@salesforce/plugin-source",
Expand Down
12 changes: 12 additions & 0 deletions messages/cancel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"description": "cancel a source deployment",
"examples": [
"$ sfdx force:source:deploy:cancel",
"$ sfdx force:source:deploy:cancel -w 2",
"$ sfdx force:source:deploy:cancel -i <jobid>"
],
"flags": {
"wait": "wait time for command to finish in minutes",
"jobid": "job ID of the deployment you want to cancel; defaults to your most recent CLI deployment if not specified"
}
}
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@
"external": true,
"subtopics": {
"source": {
"description": "commands to interact with source formatted metadata"
"description": "commands to interact with source formatted metadata",
"subtopics": {
"deploy": {
"description": "interact with an active deploy request"
}
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/commands/force/source/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ export class Deploy extends SourceCommand {
.start();
await hookEmitter.emit('postdeploy', results);

const file = this.getConfig();
await file.write({ [SourceCommand.STASH_KEY]: { jobid: results.response.id } });

// skip a lot of steps that would do nothing
if (!this.flags.json) {
this.print(results);
Expand Down
51 changes: 51 additions & 0 deletions src/commands/force/source/deploy/cancel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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 } from '@salesforce/core';
import { DeployResult } from '@salesforce/source-deploy-retrieve';
import { Duration } from '@salesforce/kit';
import { asString } from '@salesforce/ts-types';
import { SourceCommand } from '../../../../sourceCommand';

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

export class Cancel extends SourceCommand {
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(SourceCommand.DEFAULT_SRC_WAIT_MINUTES),
min: Duration.minutes(1),
description: messages.getMessage('flags.wait'),
}),
jobid: flags.id({
char: 'i',
description: messages.getMessage('flags.jobid'),
}),
};

public async run(): Promise<DeployResult> {
let id = asString(this.flags.jobid);
if (!id) {
const stash = this.getConfig();
stash.readSync();
id = asString((stash.get(SourceCommand.STASH_KEY) as { jobid: string }).jobid);
}

// TODO: update to use SDRL we can just do this for now
// this is the toolbelt implementation
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-assignment
return (await this.org.getConnection().metadata['_invoke']('cancelDeploy', {
id,
})) as DeployResult;
}
}
8 changes: 7 additions & 1 deletion src/sourceCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import * as path from 'path';
import { SfdxCommand } from '@salesforce/command';
import { ComponentSet } from '@salesforce/source-deploy-retrieve';
import { fs, SfdxError, Logger } from '@salesforce/core';
import { fs, SfdxError, Logger, ConfigFile } from '@salesforce/core';
import { ComponentLike } from '@salesforce/source-deploy-retrieve/lib/src/common';

export type FlagOptions = {
Expand All @@ -20,6 +20,12 @@ export type FlagOptions = {

export abstract class SourceCommand extends SfdxCommand {
public static DEFAULT_SRC_WAIT_MINUTES = 33;
public static STASH_KEY = 'SOURCE_DEPLOY';

public getConfig(): ConfigFile<{ isGlobal: true; filename: 'stash.json' }> {
return new ConfigFile({ isGlobal: true, filename: 'stash.json' });
}

/**
* will create one ComponentSet to be deployed/retrieved
* will combine from all options passed in
Expand Down
62 changes: 62 additions & 0 deletions test/commands/source/cancel.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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 { Dictionary } from '@salesforce/ts-types';
import { DeployResult } from '@salesforce/source-deploy-retrieve';
import * as sinon from 'sinon';
import { expect } from 'chai';
import { Cancel } from '../../../src/commands/force/source/deploy/cancel';

describe('force:source:cancel', () => {
const jobid = '0Af1k00000r2BebCAE';
const sandbox = sinon.createSandbox();

const run = async (
flags: Dictionary<boolean | string | number | string[]> = {},
id?: string
): Promise<DeployResult> => {
// TODO: fix this test for SDRL
// Run the command
// all the stubs will change with SDRL implementation, just call it good for now
return Cancel.prototype.run.call({
flags: Object.assign({}, flags),
getConfig: () => {
return { readSync: () => {}, get: () => jobid };
},
org: {
getConnection: () => {
return {
metadata: {
_invoke: () => {
return {
id: id || jobid,
};
},
},
};
},
},
}) as Promise<DeployResult>;
};

beforeEach(() => {});

afterEach(() => {
sandbox.restore();
});

it('should read from ~/.sfdx/stash.json', async () => {
const result = await run({ json: true });
expect(result).to.deep.equal({ id: jobid });
});

it('should use the jobid flag', async () => {
const jobIdFlag = '0Af1k00000r29C9CAI';
const result = await run({ json: true, jobid: jobIdFlag }, jobIdFlag);
expect(result).to.deep.equal({ id: jobIdFlag });
});
});
5 changes: 4 additions & 1 deletion test/commands/source/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('force:source:deploy', () => {

// TODO: When output work items have been done we can test result output
// that more closely matches actual output.
const stubbedResults = 'the stubbed results';
const stubbedResults = { response: { id: '0Af1k00000r2BfKCAU' } };

// Stubs
let createComponentSetStub: sinon.SinonStub;
Expand All @@ -44,6 +44,9 @@ describe('force:source:deploy', () => {
getUsername: () => username,
},
createComponentSet: createComponentSetStub,
getConfig: () => {
return { write: () => {} };
},
}) as Promise<DeployResult>;
};

Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@

"@salesforce/[email protected]":
version "1.1.21"
resolved "https://registry.npmjs.org/@salesforce/source-deploy-retrieve/-/source-deploy-retrieve-1.1.21.tgz#afb3b7dfe5b79ce1b3b9a016433fba9e3d83e0c7"
resolved "https://registry.yarnpkg.com/@salesforce/source-deploy-retrieve/-/source-deploy-retrieve-1.1.21.tgz#afb3b7dfe5b79ce1b3b9a016433fba9e3d83e0c7"
integrity sha512-+2nJh/GsSGE6bUolrcV6zA+RAutGIKjzS6IS7uFwHrpy0r7NCElgjRL/EkfahMgKxuVJDg8nQiVzWzi108AgPw==
dependencies:
"@salesforce/core" "2.13.0"
Expand Down