Skip to content

Commit

Permalink
feat: save hubOrg to session instance
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Oct 4, 2022
1 parent 4ac7bce commit 104f6ea
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/execCmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export type ExecCmdOptions = ExecOptions & BaseExecOptions;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type ExcludeMethods<T> = Pick<T, NonNullable<{ [K in keyof T]: T[K] extends (_: any) => any ? never : K }[keyof T]>>;

type JsonOutput<T> = { status: number; result: T } & Partial<ExcludeMethods<SfError>>;
export type JsonOutput<T> = { status: number; result: T } & Partial<ExcludeMethods<SfError>>;

export interface ExecCmdResult<T> {
/**
Expand Down
9 changes: 6 additions & 3 deletions src/hubAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,13 @@ export const testkitHubAuth = (homeDir: string, authStrategy: DevhubAuthStrategy
const jwtKey = prepareForJwt(homeDir);

const results = shell.exec(
`sfdx auth:jwt:grant -d -u ${env.getString('TESTKIT_HUB_USERNAME', '')} -i ${env.getString(
'TESTKIT_JWT_CLIENT_ID',
`sf login jwt org --set-default-dev-hub --username ${env.getString(
'TESTKIT_HUB_USERNAME',
''
)} -f ${jwtKey} -r ${env.getString('TESTKIT_HUB_INSTANCE', DEFAULT_INSTANCE_URL)}`,
)} --clientid ${env.getString('TESTKIT_JWT_CLIENT_ID', '')} --keyfile ${jwtKey} --instance-url ${env.getString(
'TESTKIT_HUB_INSTANCE',
DEFAULT_INSTANCE_URL
)}`,
execOpts
) as shell.ShellString;

Expand Down
18 changes: 17 additions & 1 deletion src/testSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import { Optional } from '@salesforce/ts-types';
import { createSandbox, SinonStub } from 'sinon';
import * as shell from 'shelljs';
import stripAnsi = require('strip-ansi');
import { AuthFields } from '@salesforce/core';
import { AuthFields, OrgAuthorization } from '@salesforce/core';
import { genUniqueString } from './genUniqueString';
import { zipDir } from './zip';

import { TestProject, TestProjectOptions } from './testProject';
import { DevhubAuthStrategy, getAuthStrategy, testkitHubAuth, transferExistingAuthToEnv } from './hubAuth';
import { JsonOutput } from './execCmd';

export type ScratchOrgConfig = {
executable?: 'sfdx' | 'sf';
Expand Down Expand Up @@ -105,6 +106,7 @@ export class TestSession extends AsyncOptionalCreatable<TestSessionOptions> {
public rmRetryConfig: Partial<RetryConfig<void>> = { retries: 12, delay: 5000 };

public orgs: Map<string, AuthFields> = new Map<string, AuthFields>();
public hubOrg!: AuthFields;

private debug: Debugger;
private cwdStub?: SinonStub;
Expand Down Expand Up @@ -178,6 +180,20 @@ export class TestSession extends AsyncOptionalCreatable<TestSessionOptions> {

process.env.SFDX_USE_GENERIC_UNIX_KEYCHAIN = 'true';
testkitHubAuth(this.homeDir, authStrategy);

if (authStrategy !== 'NONE') {
const config = shell.exec('sf config get target-dev-hub --json', this.shelljsExecOptions) as shell.ShellString;
const configResults = JSON.parse(config.stdout) as unknown as JsonOutput<Array<{ name: string; value: string }>>;
const usernameOrAlias = configResults.result.find((org) => org.name === 'target-dev-hub')?.value;
if (usernameOrAlias) {
const displayEnv = shell.exec(
`sf env display -e ${usernameOrAlias} --json`,
this.shelljsExecOptions
) as shell.ShellString;
const displayEnvResults = JSON.parse(displayEnv.stdout) as unknown as JsonOutput<OrgAuthorization>;
this.hubOrg = displayEnvResults.result;
}
}
}

/**
Expand Down

0 comments on commit 104f6ea

Please sign in to comment.