Skip to content

Commit 598398d

Browse files
fix: prevent defaultHelp method to throw when there is no target-devops-center value set (#139)
1 parent 0615cc3 commit 598398d

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

src/common/base/abstractPromote.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ export abstract class PromoteCommand<T extends typeof SfCommand> extends SfComma
185185
* @returns
186186
*/
187187
protected catch(error: Error | SfError): Promise<SfCommand.Error> {
188-
if (error.name.includes('GenericTimeoutError')) {
188+
if (error.name?.includes('GenericTimeoutError')) {
189189
const err = messages.createError('error.ClientTimeout', [
190190
this.config.bin,
191191
this.id?.split(':').slice(0, -1).join(' '),

src/common/base/abstractResume.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export abstract class ResumeCommand<T extends typeof SfCommand> extends SfComman
105105
* @returns
106106
*/
107107
protected catch(error: Error | SfError): Promise<SfCommand.Error> {
108-
if (error.name.includes('GenericTimeoutError')) {
108+
if (error.name?.includes('GenericTimeoutError')) {
109109
const err = messages.createError('error.ClientTimeout', [
110110
this.config.bin,
111111
this.id?.split(':').slice(0, -1).join(' '),

src/common/flags/flags.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const requiredDoceOrgFlag = OclifFlags.custom({
4747
summary: messages.getMessage('flags.targetDoceOrg.summary'),
4848
parse: async (input: string | undefined) => getOrgOrThrow(input),
4949
default: async () => getOrgOrThrow(),
50-
defaultHelp: async () => (await getOrgOrThrow())?.getUsername(),
50+
defaultHelp: async () => getDefaultDevopsCenterUsernameAlias(),
5151
required: true,
5252
});
5353

@@ -74,18 +74,26 @@ export const concise: BooleanFlag<boolean> = Flags.boolean({
7474
exclusive: ['verbose'],
7575
});
7676

77+
/**
78+
* Helper to get the userneame or alias of the default Devops Center org
79+
* from the target-devops-center config variable.
80+
*/
81+
export const getDefaultDevopsCenterUsernameAlias = async (): Promise<string | undefined> => {
82+
const config = await ConfigAggregator.create({ customConfigMeta: ConfigMeta });
83+
return config.getInfo(ConfigVars.TARGET_DEVOPS_CENTER)?.value?.toString();
84+
};
85+
7786
/**
7887
*
7988
* @param input alias/username of an org
8089
* @returns instance of an Org that correspons to the alias/username passed in
8190
* or to the alias/username set in --target-devops-center config variable.
8291
*/
8392
const getOrgOrThrow = async (input?: string): Promise<Org> => {
84-
const aggregator = await ConfigAggregator.create({ customConfigMeta: ConfigMeta });
85-
const alias = input ? input : aggregator.getInfo(ConfigVars.TARGET_DEVOPS_CENTER)?.value?.toString();
86-
if (!alias) {
93+
const usernameOrAlias = input ? input : await getDefaultDevopsCenterUsernameAlias();
94+
if (!usernameOrAlias) {
8795
throw messages.createError('errors.NoDefaultDoceEnv');
8896
}
89-
const org: Org = await Org.create({ aliasOrUsername: alias });
97+
const org: Org = await Org.create({ aliasOrUsername: usernameOrAlias });
9098
return org;
9199
};

test/common/flags.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ import { Parser } from '@oclif/core';
1111
import { ConfigAggregator, Org } from '@salesforce/core';
1212
import { Duration } from '@salesforce/kit';
1313
import { ConfigVars } from '../../src/configMeta';
14-
import { requiredDoceOrgFlag, wait, verbose, concise } from '../../src/common/flags/flags';
14+
import {
15+
requiredDoceOrgFlag,
16+
wait,
17+
verbose,
18+
concise,
19+
getDefaultDevopsCenterUsernameAlias,
20+
} from '../../src/common/flags/flags';
1521
import { async } from '../../src/common/flags/promote/promoteFlags';
1622

1723
const TARGET_DEVOPS_CENTER_ALIAS = 'target-devops-center';
@@ -156,6 +162,30 @@ describe('requiredDoceOrgFlag', () => {
156162
expect(err.message).to.include(`No authorization information found for ${invalidTargetDevopsCenter}`);
157163
}
158164
});
165+
166+
it('returns the value of the target-devops-center config variable', async () => {
167+
sandbox.stub(ConfigAggregator.prototype, 'getInfo').returns({
168+
value: TARGET_DEVOPS_CENTER_ALIAS,
169+
key: ConfigVars.TARGET_DEVOPS_CENTER,
170+
isLocal: () => false,
171+
isGlobal: () => true,
172+
isEnvVar: () => false,
173+
});
174+
const defaultDoceAlias = await getDefaultDevopsCenterUsernameAlias();
175+
expect(defaultDoceAlias).to.equal(TARGET_DEVOPS_CENTER_ALIAS);
176+
});
177+
178+
it('returns undefined when there is not a target-devops-center config variable', async () => {
179+
sandbox.stub(ConfigAggregator.prototype, 'getInfo').returns({
180+
value: null,
181+
key: ConfigVars.TARGET_DEVOPS_CENTER,
182+
isLocal: () => false,
183+
isGlobal: () => true,
184+
isEnvVar: () => false,
185+
});
186+
const defaultDoceAlias = await getDefaultDevopsCenterUsernameAlias();
187+
expect(defaultDoceAlias).to.equal(undefined);
188+
});
159189
});
160190

161191
describe('waitFlag', () => {

0 commit comments

Comments
 (0)