-
Notifications
You must be signed in to change notification settings - Fork 405
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: change status bar color when in production org setting #5519
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright (c) 2024, 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 vscode from 'vscode'; | ||
import { WorkspaceContext } from '../context'; | ||
import { getDefaultUsernameOrAlias } from '../context/workspaceOrgType'; | ||
import { OrgAuthInfo } from '../util'; | ||
import { SfdxCoreSettings } from './sfdxCoreSettings'; | ||
|
||
/** | ||
* Change the color of the status bar when the default org is a production org | ||
* @returns {Promise<boolean>} - returns true if the color was changed | ||
*/ | ||
export const colorWhenProductionOrg = async () => { | ||
const baseColorStatusBar = new vscode.ThemeColor('statusBar.background'); | ||
|
||
const colorWHenProductionOrgHandler = async () => { | ||
const usernameOrAlias = await getDefaultUsernameOrAlias(); | ||
const settings = SfdxCoreSettings.getInstance(); | ||
|
||
const activated = settings.getColorWarningWhenProductionOrg(); | ||
const colorForProdOrg = settings.getColorWarningWhenProductionOrgColor(); | ||
|
||
if (!usernameOrAlias || !activated) { | ||
return false; | ||
} | ||
const isProdOrg = await OrgAuthInfo.isAProductionOrg( | ||
await OrgAuthInfo.getUsername(usernameOrAlias) | ||
); | ||
const colorCustomizations = { | ||
'statusBar.background': isProdOrg ? colorForProdOrg : baseColorStatusBar | ||
}; | ||
|
||
// Save the configuration to the global settings file | ||
await vscode.workspace | ||
.getConfiguration() | ||
.update( | ||
'workbench.colorCustomizations', | ||
colorCustomizations, | ||
vscode.ConfigurationTarget.Global | ||
); | ||
return true; | ||
}; | ||
|
||
WorkspaceContext.getInstance().onOrgChange(() => | ||
colorWHenProductionOrgHandler() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fix typo |
||
); | ||
|
||
/** | ||
* Change the color of the status bar when the window state changes, avoiding the status bar color on others vscode windows | ||
*/ | ||
vscode.window.onDidChangeWindowState(async e => { | ||
if (e.focused) { | ||
await colorWHenProductionOrgHandler(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And here as well |
||
} else { | ||
await vscode.workspace | ||
.getConfiguration() | ||
.update( | ||
'workbench.colorCustomizations', | ||
{}, | ||
vscode.ConfigurationTarget.Global | ||
); | ||
} | ||
}); | ||
return await colorWHenProductionOrgHandler(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And one more here |
||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,9 +18,8 @@ import { telemetryService } from '../telemetry'; | |
|
||
export class OrgAuthInfo { | ||
public static async getDevHubUsername() { | ||
const defaultDevHubUsernameOrAlias = await OrgAuthInfo.getDefaultDevHubUsernameOrAlias( | ||
false | ||
); | ||
const defaultDevHubUsernameOrAlias = | ||
await OrgAuthInfo.getDefaultDevHubUsernameOrAlias(false); | ||
let defaultDevHubUsername: string | undefined; | ||
if (defaultDevHubUsernameOrAlias) { | ||
defaultDevHubUsername = await OrgAuthInfo.getUsername( | ||
|
@@ -34,7 +33,8 @@ export class OrgAuthInfo { | |
enableWarning: boolean | ||
): Promise<string | undefined> { | ||
try { | ||
const defaultUsernameOrAlias = await ConfigUtil.getDefaultUsernameOrAlias(); | ||
const defaultUsernameOrAlias = | ||
await ConfigUtil.getDefaultUsernameOrAlias(); | ||
if (!defaultUsernameOrAlias) { | ||
displayMessage( | ||
nls.localize('error_no_default_username'), | ||
|
@@ -114,6 +114,15 @@ export class OrgAuthInfo { | |
); | ||
} | ||
|
||
public static async isAProductionOrg(username: string): Promise<boolean> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please review https://forcedotcom.github.io/sfdx-core/classes/org_org.Org-1.html to determine this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @peternhale is there an affirmative case for "this is a production org"? I'm not seeing that in these docs, so it's looking like Production would be the fallback case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @randi274 Yes. production should be the fallback. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it! @interfaceconjurer and I were chatting about this detail today, it might be worth y'all discussing the possibility of "Production" being overly inclusive. |
||
const authInfo = await AuthInfo.create({ username }); | ||
const authInfoFields = authInfo.getFields(); | ||
return Promise.resolve( | ||
!authInfoFields.isSandbox && | ||
!authInfoFields.instanceUrl?.includes('sandbox.my.salesforce.com') | ||
); | ||
} | ||
|
||
public static async getConnection( | ||
usernameOrAlias?: string | ||
): Promise<Connection> { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright (c) 2024, 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 vscode from 'vscode'; | ||
import { WorkspaceContext } from '../../../src/context'; | ||
import { getDefaultUsernameOrAlias } from '../../../src/context/workspaceOrgType'; | ||
import { colorWhenProductionOrg } from '../../../src/settings/colorWarningWhenProdOrg'; | ||
import { SfdxCoreSettings } from '../../../src/settings/sfdxCoreSettings'; | ||
import { OrgAuthInfo } from '../../../src/util'; | ||
|
||
// Mocking getDefaultUsernameOrAlias function | ||
jest.mock('../../../src/context/workspaceOrgType', () => ({ | ||
getDefaultUsernameOrAlias: jest.fn() | ||
})); | ||
|
||
// Mocking OrgAuthInfo class | ||
jest.mock('../../../src/util', () => ({ | ||
OrgAuthInfo: { | ||
getUsername: jest.fn(), | ||
isAProductionOrg: jest.fn() | ||
} | ||
})); | ||
|
||
// Mocking SfdxCoreSettings class | ||
jest.mock('../../../src/settings/sfdxCoreSettings', () => ({ | ||
SfdxCoreSettings: { | ||
getInstance: jest.fn() | ||
} | ||
})); | ||
|
||
const mockWorkspaceContextInstance = { | ||
onOrgChange: jest.fn() | ||
}; | ||
|
||
// Mock the WorkspaceContext class | ||
jest.mock('../../../src/context', () => ({ | ||
WorkspaceContext: jest.fn().mockImplementation(() => ({ | ||
getInstance: jest.fn().mockReturnValue(mockWorkspaceContextInstance) | ||
})) | ||
})); | ||
|
||
describe('colorWhenProductionOrg', () => { | ||
let mockConfiguration: any; | ||
let mockOnOrgChange: any; | ||
|
||
beforeEach(() => { | ||
mockConfiguration = { | ||
update: jest.fn() | ||
}; | ||
|
||
mockOnOrgChange = jest.fn(); | ||
(SfdxCoreSettings.getInstance as jest.Mock).mockReturnValue({ | ||
getColorWarningWhenProductionOrg: jest.fn(() => true), | ||
getColorWarningWhenProductionOrgColor: jest.fn() | ||
}); | ||
|
||
WorkspaceContext.getInstance = jest.fn().mockReturnValue({ | ||
onOrgChange: mockOnOrgChange | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should update status bar color when production org is detected', async () => { | ||
(getDefaultUsernameOrAlias as jest.Mock).mockResolvedValue('testUsername'); | ||
(OrgAuthInfo.getUsername as jest.Mock).mockResolvedValue('testUsername'); | ||
(OrgAuthInfo.isAProductionOrg as jest.Mock).mockResolvedValue(true); | ||
|
||
const updated = await colorWhenProductionOrg(); | ||
|
||
expect(updated).toBe(true); | ||
}); | ||
|
||
it('should not update status bar color when no username or alias is found', async () => { | ||
(getDefaultUsernameOrAlias as jest.Mock).mockResolvedValue(null); | ||
|
||
const updated = await colorWhenProductionOrg(); | ||
|
||
expect(updated).toBe(false); | ||
}); | ||
|
||
it('should not update status bar color when color warning for production org is not activated', async () => { | ||
(getDefaultUsernameOrAlias as jest.Mock).mockResolvedValue('testUsername'); | ||
(OrgAuthInfo.getUsername as jest.Mock).mockResolvedValue('testUsername'); | ||
(SfdxCoreSettings.getInstance as jest.Mock).mockReturnValue({ | ||
getColorWarningWhenProductionOrg: jest.fn(() => false), | ||
getColorWarningWhenProductionOrgColor: jest.fn(() => undefined) | ||
}); | ||
|
||
const updated = await colorWhenProductionOrg(); | ||
expect(updated).toBe(false); | ||
}); | ||
|
||
it('should not update status bar color when org is not a production org', async () => { | ||
(getDefaultUsernameOrAlias as jest.Mock).mockResolvedValue('testUsername'); | ||
(OrgAuthInfo.getUsername as jest.Mock).mockResolvedValue('testUsername'); | ||
(OrgAuthInfo.isAProductionOrg as jest.Mock).mockResolvedValue(false); | ||
|
||
const updated = await colorWhenProductionOrg(); | ||
|
||
expect(updated).toBe(true); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo colorWHenProductionOrgHandler -> colorWhenProductionOrgHandler