Skip to content

Commit

Permalink
Merge pull request #304 from salesforcecli/bm/W-10657353
Browse files Browse the repository at this point in the history
feat: new command org:status
  • Loading branch information
WillieRuemmele authored Apr 22, 2022
2 parents a1bccfb + 4d7722f commit b748410
Show file tree
Hide file tree
Showing 11 changed files with 433 additions and 125 deletions.
127 changes: 85 additions & 42 deletions command-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,43 +1,86 @@
[
{
"command": "force:org:beta:create",
"plugin": "@salesforce/plugin-org",
"flags": [
"apiversion",
"clientid",
"definitionfile",
"durationdays",
"json",
"loglevel",
"noancestors",
"nonamespace",
"retry",
"setalias",
"setdefaultusername",
"targetdevhubusername",
"targetusername",
"type",
"wait"
]
},
{
"command": "force:org:delete",
"plugin": "@salesforce/plugin-org",
"flags": ["apiversion", "json", "loglevel", "noprompt", "targetdevhubusername", "targetusername"]
},
{
"command": "force:org:display",
"plugin": "@salesforce/plugin-org",
"flags": ["apiversion", "json", "loglevel", "targetusername", "verbose"]
},
{
"command": "force:org:list",
"plugin": "@salesforce/plugin-org",
"flags": ["all", "clean", "json", "loglevel", "noprompt", "skipconnectionstatus", "verbose"]
},
{
"command": "force:org:open",
"plugin": "@salesforce/plugin-org",
"flags": ["apiversion", "browser", "json", "loglevel", "path", "targetusername", "urlonly"]
}
]
{
"command": "force:org:beta:create",
"plugin": "@salesforce/plugin-org",
"flags": [
"apiversion",
"clientid",
"definitionfile",
"durationdays",
"json",
"loglevel",
"noancestors",
"nonamespace",
"retry",
"setalias",
"setdefaultusername",
"targetdevhubusername",
"targetusername",
"type",
"wait"
]
},
{
"command": "force:org:delete",
"plugin": "@salesforce/plugin-org",
"flags": [
"apiversion",
"json",
"loglevel",
"noprompt",
"targetdevhubusername",
"targetusername"
]
},
{
"command": "force:org:display",
"plugin": "@salesforce/plugin-org",
"flags": [
"apiversion",
"json",
"loglevel",
"targetusername",
"verbose"
]
},
{
"command": "force:org:list",
"plugin": "@salesforce/plugin-org",
"flags": [
"all",
"clean",
"json",
"loglevel",
"noprompt",
"skipconnectionstatus",
"verbose"
]
},
{
"command": "force:org:open",
"plugin": "@salesforce/plugin-org",
"flags": [
"apiversion",
"browser",
"json",
"loglevel",
"path",
"targetusername",
"urlonly"
]
},
{
"command": "force:org:status",
"plugin": "@salesforce/plugin-org",
"flags": [
"apiversion",
"json",
"loglevel",
"sandboxname",
"setalias",
"setdefaultusername",
"targetusername",
"wait"
]
}
]
13 changes: 13 additions & 0 deletions messages/status.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"examples": [
"sfdx force:org:status --sandboxname DevSbx1 --setalias MySandbox -u prodOrg",
"sfdx force:org:status --sandboxname DevSbx1 --wait 45 --setdefaultusername -u prodOrg"
],
"description": "report status of sandbox creation or clone and authenticate to it\nUse this command to check the status of your sandbox creation or clone and, if the sandbox is ready, authenticate to it.\nUse the --wait (-w) parameter to specify the number of minutes that the command waits for the sandbox creation or clone to complete before returning control of the terminal to you.\nSet the --targetusername (-u) parameter to the username or alias of the production org that contains the sandbox license.",
"flags": {
"sandboxname": "name of the sandbox org to check status for",
"wait": "number of minutes to wait while polling for status",
"setdefaultusername": "set the created or cloned org as your default",
"setalias": "alias for the created or cloned org"
}
}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"dependencies": {
"@oclif/config": "^1.18.1",
"@salesforce/command": "^4.2.2",
"@salesforce/core": "^2.35.0",
"@salesforce/core": "^2.36.3",
"@salesforce/kit": "^1.5.17",
"open": "8.4.0",
"tslib": "^2"
Expand Down Expand Up @@ -43,11 +43,15 @@
"nyc": "^15.1.0",
"prettier": "^2.4.1",
"pretty-quick": "^3.1.0",
"shelljs": "^0.8.5",
"shx": "0.3.4",
"sinon": "10.0.0",
"ts-node": "^10.0.0",
"typescript": "^4.4.4"
},
"resolutions": {
"@salesforce/core": "^2.36.3"
},
"config": {
"commitizen": {
"path": "cz-conventional-changelog"
Expand Down
98 changes: 98 additions & 0 deletions src/commands/force/org/status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright (c) 2021, 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 { EOL } from 'os';
import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';
import {
Config,
Lifecycle,
Messages,
Aliases,
SandboxEvents,
StatusEvent,
ResultEvent,
SandboxProcessObject,
} from '@salesforce/core';
import { Duration } from '@salesforce/kit';
import { SandboxReporter } from '../../../shared/sandboxReporter';

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

export class OrgStatusCommand extends SfdxCommand {
public static readonly examples = messages.getMessage('examples').split(EOL);
public static readonly description = messages.getMessage('description');
public static readonly requiresProject = false;
public static readonly requiresUsername = true;

public static readonly flagsConfig: FlagsConfig = {
sandboxname: flags.string({
char: 'n',
description: messages.getMessage('flags.sandboxname'),
required: true,
}),
setdefaultusername: flags.boolean({
char: 's',
description: messages.getMessage('flags.setdefaultusername'),
}),
setalias: flags.string({
char: 'a',
description: messages.getMessage('flags.setalias'),
}),
wait: flags.minutes({
char: 'w',
description: messages.getMessage('flags.wait'),
min: Duration.minutes(2),
default: Duration.minutes(6),
}),
};

public async run(): Promise<SandboxProcessObject> {
this.logger.debug('Status started with args %s ', this.flags);
const lifecycle = Lifecycle.getInstance();

// eslint-disable-next-line @typescript-eslint/require-await
lifecycle.on(SandboxEvents.EVENT_STATUS, async (results: StatusEvent) => {
this.ux.log(SandboxReporter.sandboxProgress(results));
});

lifecycle.on(SandboxEvents.EVENT_RESULT, async (results: ResultEvent) => {
const resultMsg = `Sandbox ${results.sandboxProcessObj.SandboxName}(${results.sandboxProcessObj.Id}) is ready for use.`;
this.ux.log(resultMsg);
const { data } = SandboxReporter.logSandboxProcessResult(results);
this.ux.styledHeader('Sandbox Org Status');
this.ux.table(data, {
columns: [
{ key: 'key', label: 'Name' },
{ key: 'value', label: 'Value' },
],
});
if (results.sandboxRes?.authUserName) {
if (this.flags.setalias) {
const aliases = await Aliases.create(Aliases.getDefaultOptions());
const result = await aliases.updateValue(this.flags.setalias, results.sandboxRes.authUserName);
this.logger.debug('Set Alias: %s result: %s', this.flags.setalias, result);
}
if (this.flags.setdefaultusername) {
const globalConfig: Config = this.configAggregator.getGlobalConfig();
globalConfig.set(Config.DEFAULT_USERNAME, results.sandboxRes.authUserName);
const result = await globalConfig.write();
this.logger.debug('Set defaultUsername: %s result: %s', this.flags.setdefaultusername, result);
}
}
});

this.logger.debug('Calling auth for SandboxName args: %s ', this.flags.sandboxname);
const results = await this.org.sandboxStatus(this.flags.sandboxname, { wait: this.flags.wait as Duration });
this.logger.debug('Results for auth call: %s ', results);
if (!results) {
this.ux.styledHeader('Sandbox Org Creation Status');
this.ux.log('No SandboxProcess Result Found');
}
return results;
}
}
30 changes: 17 additions & 13 deletions test/commands/force/org/display.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* 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 { $$, expect, test } from '@salesforce/command/lib/test';
import { expect, test } from '@salesforce/command/lib/test';
import { Aliases, AuthInfo, Connection, Org } from '@salesforce/core';

import * as sinon from 'sinon';
import { stubMethod, stubInterface, StubbedType } from '@salesforce/ts-sinon';
import * as utils from '../../../../src/shared/utils';

Expand Down Expand Up @@ -45,15 +45,15 @@ const fakeAuthUrl = 'fakeAuthUrl';

describe('org:display', () => {
let authInfoStub: StubbedType<AuthInfo>;
const sandbox = sinon.createSandbox();

beforeEach(async function () {
$$.SANDBOX.restore();
stubMethod($$.SANDBOX, Aliases, 'fetch')
stubMethod(sandbox, Aliases, 'fetch')
.withArgs('nonscratchalias')
.resolves('[email protected]')
.withArgs('scratchAlias')
.resolves('[email protected]');
stubMethod($$.SANDBOX, utils, 'getAliasByUsername')
stubMethod(sandbox, utils, 'getAliasByUsername')
.withArgs('[email protected]')
.resolves('nonscratchalias')
.withArgs('[email protected]')
Expand All @@ -62,9 +62,13 @@ describe('org:display', () => {
.resolves(undefined);
});

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

// eslint-disable-next-line @typescript-eslint/no-explicit-any
async function prepareStubs(AuthInfoModifications: any = {}) {
authInfoStub = stubInterface<AuthInfo>($$.SANDBOX, {
authInfoStub = stubInterface<AuthInfo>(sandbox, {
getFields: () => ({
...baseAuthInfo,
...AuthInfoModifications,
Expand All @@ -76,7 +80,7 @@ describe('org:display', () => {
return 'badUrl';
},
});
stubMethod($$.SANDBOX, AuthInfo, 'create').callsFake(async () => authInfoStub);
stubMethod(sandbox, AuthInfo, 'create').callsFake(async () => authInfoStub);
}

test
Expand Down Expand Up @@ -206,16 +210,16 @@ describe('org:display', () => {
await prepareStubs({
devHubUsername: devHub.username,
});
stubMethod($$.SANDBOX, Org, 'create').resolves(Org.prototype);
stubMethod($$.SANDBOX, Org.prototype, 'getUsername').returns('[email protected]');
stubMethod(sandbox, Org, 'create').resolves(Org.prototype);
stubMethod(sandbox, Org.prototype, 'getUsername').returns('[email protected]');

stubMethod($$.SANDBOX, Org.prototype, 'getOrgId').resolves(devHub.id);
stubMethod($$.SANDBOX, Org.prototype, 'getDevHubOrg').resolves({
stubMethod(sandbox, Org.prototype, 'getOrgId').resolves(devHub.id);
stubMethod(sandbox, Org.prototype, 'getDevHubOrg').resolves({
getUsername: () => devHub.username,
});
stubMethod($$.SANDBOX, Org.prototype, 'getConnection').returns(Connection.prototype);
stubMethod(sandbox, Org.prototype, 'getConnection').returns(Connection.prototype);

stubMethod($$.SANDBOX, Connection.prototype, 'sobject').returns({
stubMethod(sandbox, Connection.prototype, 'sobject').returns({
find: async () => {
return [
{
Expand Down
Loading

0 comments on commit b748410

Please sign in to comment.