-
Notifications
You must be signed in to change notification settings - Fork 407
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
Open
PierreVanobbergen
wants to merge
2
commits into
forcedotcom:develop
Choose a base branch
from
PierreVanobbergen:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+246
−11
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
packages/salesforcedx-vscode-core/src/settings/colorWarningWhenProdOrg.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
); | ||
|
||
/** | ||
* 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(); | ||
} else { | ||
await vscode.workspace | ||
.getConfiguration() | ||
.update( | ||
'workbench.colorCustomizations', | ||
{}, | ||
vscode.ConfigurationTarget.Global | ||
); | ||
} | ||
}); | ||
return await colorWhenProductionOrgHandler(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
packages/salesforcedx-vscode-core/test/jest/settings/colorWarningWhenProdOrg.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 comment
The 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 comment
The 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 comment
The 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.