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

feat: save namespacePrefix in auth file #908

Merged
merged 3 commits into from
Aug 3, 2023
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
36 changes: 36 additions & 0 deletions src/org/authInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { randomBytes } from 'crypto';
import { resolve as pathResolve } from 'path';
import * as os from 'os';
import * as fs from 'fs';
import { Record as RecordType } from 'jsforce';
import { AsyncOptionalCreatable, cloneJson, env, isEmpty, parseJson, parseJsonMap } from '@salesforce/kit';
import {
AnyJson,
Expand Down Expand Up @@ -826,6 +827,15 @@ export class AuthInfo extends AsyncOptionalCreatable<AuthInfo.Options> {
ensureString(authConfig.accessToken)
);

const namespacePrefix = await this.getNamespacePrefix(
ensureString(authConfig.instanceUrl),
ensureString(authConfig.accessToken)
);

if (namespacePrefix) {
authConfig.namespacePrefix = namespacePrefix;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getNamespacePrefix will return undefined if an org doesn't have a namespace prefix we just set it if there's one.

}

if (authConfig.username) await this.stateAggregator.orgs.read(authConfig.username, false, false);

// Update the auth fields WITH encryption
Expand Down Expand Up @@ -1114,6 +1124,32 @@ export class AuthInfo extends AsyncOptionalCreatable<AuthInfo.Options> {
throw new SfError(errorMsg);
}

private async getNamespacePrefix(instanceUrl: string, accessToken: string): Promise<string | undefined> {
// Make a REST call for the Organization obj directly. Normally this is done via a connection
// but we don't want to create circular dependencies or lots of snowflakes
// within this file to support it.
const apiVersion = 'v51.0'; // hardcoding to v51.0 just for this call is okay.
const instance = ensure(instanceUrl);
const baseUrl = new SfdcUrl(instance);
const namespacePrefixOrgUrl = `${baseUrl.toString()}/services/data/${apiVersion}/query?q=Select%20Namespaceprefix%20FROM%20Organization`;
const headers = Object.assign({ Authorization: `Bearer ${accessToken}` }, SFDX_HTTP_HEADERS);

try {
const res = await new Transport().httpRequest({ url: namespacePrefixOrgUrl, method: 'GET', headers });
if (res.statusCode >= 400) {
return;
}

const namespacePrefix = JSON.parse(res.body) as {
records: RecordType[];
};

return ensureString(namespacePrefix.records[0]?.NamespacePrefix);
} catch (err) {
/* Doesn't have a namespace */
return;
}
}
/**
* Returns `true` if the org is a Dev Hub.
*
Expand Down
1 change: 1 addition & 0 deletions test/unit/org/authInfoTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,7 @@ describe('AuthInfo', () => {
accessToken: '',
});
stubMethod($$.SANDBOX, AuthInfo.prototype, 'determineIfDevHub').resolves(false);
stubMethod($$.SANDBOX, AuthInfo.prototype, 'getNamespacePrefix').resolves();
shetzel marked this conversation as resolved.
Show resolved Hide resolved

await AuthInfo.create({
username: testOrg.username,
Expand Down
1 change: 1 addition & 0 deletions test/unit/org/userTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ describe('User Tests', () => {
'auto-approve-user': '789101',
},
});
stubMethod($$.SANDBOX, AuthInfo.prototype, 'getNamespacePrefix').resolves();

const org = await Org.create({
connection: await Connection.create({
Expand Down
Loading