Skip to content

Commit edb66a1

Browse files
fix: Command arg fix (#4048)
* fix: vscode command unexpected arg Problem: If a command is execute by a VS Code UI component like an ellipsis or tree node, it is possible for the args of that component to be incorrect since vscode will not use the proper arguments. Solution: Add a new type called VsCodeCommandArgument that is a placeholder for that unexpected argument. When the command is used within the code we should set the value to `undefined` and when it is executed by a VS Code UI component it will be defined which indicates the other args will not exist. Signed-off-by: nkomonen <[email protected]> * refactor: showManageConnections command on its own This gets rid of the auth Command class that contains showManageConnections and instead makes it its own standalone command. We are doing this to have more control over the registration of the command since there are certain options we need to set. Signed-off-by: nkomonen <[email protected]> * fix: handle invalid arg for command auth See the big comment in the function, but we are essentially filling in a missing value because the function can be called incorrectly and we need to handle this case without throwing an error. Signed-off-by: nkomonen <[email protected]> * tests: fix failing tests due to previous changes Signed-off-by: nkomonen <[email protected]> * fix log message Signed-off-by: nkomonen <[email protected]> * fix log messages Signed-off-by: nkomonen <[email protected]> --------- Signed-off-by: nkomonen <[email protected]>
1 parent 31ebc11 commit edb66a1

File tree

15 files changed

+232
-199
lines changed

15 files changed

+232
-199
lines changed

src/auth/activation.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import { AwsContext } from '../shared/awsContext'
88
import { Auth } from './auth'
99
import { LoginManager } from './deprecated/loginManager'
1010
import { fromString } from './providers/credentials'
11-
import { registerCommandsWithVSCode } from '../shared/vscode/commands2'
12-
import { AuthCommandBackend, AuthCommandDeclarations } from './commands'
11+
import { placeholder } from '../shared/vscode/commands2'
1312
import { getLogger } from '../shared/logger'
1413
import { ExtensionUse } from './utils'
1514
import { isCloud9 } from '../shared/extensionUtilities'
1615
import { isInDevEnv } from '../codecatalyst/utils'
16+
import { showManageConnections } from './ui/vue/show'
1717

1818
export async function initialize(
1919
extensionContext: vscode.ExtensionContext,
@@ -29,11 +29,7 @@ export async function initialize(
2929
}
3030
})
3131

32-
registerCommandsWithVSCode(
33-
extensionContext,
34-
AuthCommandDeclarations.instance,
35-
new AuthCommandBackend(extensionContext)
36-
)
32+
extensionContext.subscriptions.push(showManageConnections.register(extensionContext))
3733

3834
showManageConnectionsOnStartup()
3935
}
@@ -57,5 +53,5 @@ async function showManageConnectionsOnStartup() {
5753
}
5854

5955
// Show connection management to user
60-
AuthCommandDeclarations.instance.declared.showManageConnections.execute('firstStartup')
56+
showManageConnections.execute(placeholder, 'firstStartup')
6157
}

src/auth/commands.ts

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/auth/ui/vue/show.ts

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,28 @@ import {
3232
isIamConnection,
3333
isSsoConnection,
3434
} from '../../connection'
35-
import { tryAddCredentials, signout, showRegionPrompter, promptAndUseConnection, ExtensionUse } from '../../utils'
35+
import {
36+
tryAddCredentials,
37+
signout,
38+
showRegionPrompter,
39+
promptAndUseConnection,
40+
ExtensionUse,
41+
showConnectionsPageCommand,
42+
addConnection,
43+
} from '../../utils'
3644
import { Region } from '../../../shared/regions/endpoints'
3745
import { CancellationError } from '../../../shared/utilities/timeoutUtils'
3846
import { validateSsoUrl, validateSsoUrlFormat } from '../../sso/validation'
3947
import { debounce } from '../../../shared/utilities/functionUtils'
40-
import { AuthError, ServiceItemId, userCancelled } from './types'
48+
import { AuthError, ServiceItemId, isServiceItemId, userCancelled } from './types'
4149
import { awsIdSignIn } from '../../../codewhisperer/util/showSsoPrompt'
4250
import { connectToEnterpriseSso } from '../../../codewhisperer/util/getStartUrl'
4351
import { trustedDomainCancellation } from '../../sso/model'
4452
import { FeatureId, CredentialSourceId, Result, telemetry } from '../../../shared/telemetry/telemetry'
4553
import { AuthFormId, isBuilderIdAuth } from './authForms/types'
4654
import { handleWebviewError } from '../../../webviews/server'
4755
import { cwQuickPickSource, cwTreeNodeSource } from '../../../codewhisperer/commands/types'
56+
import { Commands, VsCodeCommandArg, placeholder, vscodeComponent } from '../../../shared/vscode/commands2'
4857

4958
export class AuthWebview extends VueWebview {
5059
public override id: string = 'authWebview'
@@ -712,20 +721,42 @@ const Panel = VueWebview.compilePanel(AuthWebview)
712721
let activePanel: InstanceType<typeof Panel> | undefined
713722
let subscriptions: vscode.Disposable[] | undefined
714723

724+
/**
725+
* Different places the Add Connection command could be executed from.
726+
*
727+
* Useful for telemetry.
728+
*/
715729
export const AuthSources = {
716730
addConnectionQuickPick: 'addConnectionQuickPick',
717731
firstStartup: 'firstStartup',
718732
codecatalystDeveloperTools: 'codecatalystDeveloperTools',
719-
unknown: 'unknown',
733+
vscodeComponent: vscodeComponent,
720734
cwQuickPick: cwQuickPickSource,
721-
cwTreeNode: cwTreeNodeSource
735+
cwTreeNode: cwTreeNodeSource,
736+
authNode: 'authNode'
722737
} as const
723738

724-
export type AuthSource = typeof AuthSources[keyof typeof AuthSources]
739+
export type AuthSource = (typeof AuthSources)[keyof typeof AuthSources]
740+
741+
export const showManageConnections = Commands.declare(
742+
{ id: showConnectionsPageCommand, compositeKey: { 1: 'source' } },
743+
(context: vscode.ExtensionContext) => (_: VsCodeCommandArg, source: AuthSource, serviceToShow?: ServiceItemId) => {
744+
// The auth webview page does not make sense to use in C9,
745+
// so show the auth quick pick instead.
746+
if (isCloud9('any')) {
747+
return addConnection.execute()
748+
}
725749

726-
export function isAuthSource(obj: any): obj is AuthSource {
727-
return Object.values(AuthSources).includes(obj)
728-
}
750+
if (_ !== placeholder) {
751+
source = 'vscodeComponent'
752+
}
753+
754+
if (!isServiceItemId(serviceToShow)) {
755+
serviceToShow = undefined
756+
}
757+
return showAuthWebview(context, source, serviceToShow)
758+
}
759+
)
729760

730761
export async function showAuthWebview(
731762
ctx: vscode.ExtensionContext,

src/auth/utils.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import {
4242
isIamConnection,
4343
isValidCodeCatalystConnection,
4444
} from './connection'
45-
import { Commands } from '../shared/vscode/commands2'
45+
import { Commands, placeholder, vscodeComponent } from '../shared/vscode/commands2'
4646
import { Auth } from './auth'
4747
import { validateIsNewSsoUrl, validateSsoUrlFormat } from './sso/validation'
4848
import { openUrl } from '../shared/utilities/vsCodeUtils'
@@ -62,7 +62,7 @@ export async function promptForConnection(auth: Auth, type?: 'iam' | 'sso'): Pro
6262
if (resp === 'addNewConnection') {
6363
// TODO: Cannot call function directly due to circular dependency. Refactor to fix this.
6464
const source: AuthSource = 'addConnectionQuickPick' // enforcing type sanity check
65-
vscode.commands.executeCommand(showConnectionsPageCommand, source)
65+
vscode.commands.executeCommand(showConnectionsPageCommand, placeholder, source)
6666
return undefined
6767
}
6868

@@ -490,7 +490,8 @@ export const login = Commands.register('aws.login', async () => {
490490
const auth = Auth.instance
491491
const connections = await auth.listConnections()
492492
if (connections.length === 0) {
493-
return vscode.commands.executeCommand(showConnectionsPageCommand)
493+
const source: AuthSource = vscodeComponent
494+
return vscode.commands.executeCommand(showConnectionsPageCommand, placeholder, source)
494495
} else {
495496
return switchConnections.execute(auth)
496497
}
@@ -524,7 +525,12 @@ export class AuthNode implements TreeNode<Auth> {
524525

525526
if (!this.resource.hasConnections) {
526527
const item = new vscode.TreeItem(`Connect to ${getIdeProperties().company} to Get Started...`)
527-
item.command = { title: 'Add Connection', command: showConnectionsPageCommand }
528+
const source: AuthSource = 'authNode'
529+
item.command = {
530+
title: 'Add Connection',
531+
command: showConnectionsPageCommand,
532+
arguments: [placeholder, source],
533+
}
528534

529535
return item
530536
}

src/codecatalyst/activation.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ import { watchRestartingDevEnvs } from './reconnect'
1717
import { PromptSettings } from '../shared/settings'
1818
import { dontShow } from '../shared/localizedText'
1919
import { getIdeProperties, isCloud9 } from '../shared/extensionUtilities'
20-
import { Commands } from '../shared/vscode/commands2'
20+
import { Commands, placeholder } from '../shared/vscode/commands2'
2121
import { getCodeCatalystConfig } from '../shared/clients/codecatalystClient'
2222
import { isDevenvVscode } from './utils'
2323
import { getThisDevEnv } from './model'
2424
import { getLogger } from '../shared/logger/logger'
2525
import { InactivityMessage, shouldTrackUserActivity } from './devEnv'
26-
import { AuthCommandDeclarations } from '../auth/commands'
26+
import { showManageConnections } from '../auth/ui/vue/show'
2727

2828
const localize = nls.loadMessageBundle()
2929

@@ -39,10 +39,7 @@ export async function activate(ctx: ExtContext): Promise<void> {
3939
uriHandlers.register(ctx.uriHandler, CodeCatalystCommands.declared),
4040
...Object.values(CodeCatalystCommands.declared).map(c => c.register(commands)),
4141
Commands.register('aws.codecatalyst.manageConnections', () => {
42-
AuthCommandDeclarations.instance.declared.showManageConnections.execute(
43-
'codecatalystDeveloperTools',
44-
'codecatalyst'
45-
)
42+
showManageConnections.execute(placeholder, 'codecatalystDeveloperTools', 'codecatalyst')
4643
}),
4744
Commands.register('aws.codecatalyst.signout', () => {
4845
return authProvider.secondaryAuth.deleteConnection()

src/codecatalyst/explorer.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ import { DevEnvironment } from '../shared/clients/codecatalystClient'
99
import { isCloud9 } from '../shared/extensionUtilities'
1010
import { addColor, getIcon } from '../shared/icons'
1111
import { TreeNode } from '../shared/treeview/resourceTreeDataProvider'
12-
import { Commands } from '../shared/vscode/commands2'
12+
import { Commands, placeholder } from '../shared/vscode/commands2'
1313
import { CodeCatalystAuthenticationProvider } from './auth'
1414
import { CodeCatalystCommands } from './commands'
1515
import { ConnectedDevEnv, getDevfileLocation, getThisDevEnv } from './model'
1616
import * as codecatalyst from './model'
1717
import { getLogger } from '../shared/logger'
1818
import { Connection, isBuilderIdConnection } from '../auth/connection'
1919
import { openUrl } from '../shared/utilities/vsCodeUtils'
20-
import { AuthCommandDeclarations } from '../auth/commands'
20+
import { showManageConnections } from '../auth/ui/vue/show'
2121

2222
const learnMoreCommand = Commands.register('aws.learnMore', async (docsUrl: vscode.Uri) => {
2323
return openUrl(docsUrl)
@@ -35,12 +35,10 @@ async function getLocalCommands(auth: CodeCatalystAuthenticationProvider) {
3535
const docsUrl = isCloud9() ? codecatalyst.docs.cloud9.overview : codecatalyst.docs.vscode.overview
3636
if (!isBuilderIdConnection(auth.activeConnection) || !(await auth.isConnectionOnboarded(auth.activeConnection))) {
3737
return [
38-
AuthCommandDeclarations.instance.declared.showManageConnections
39-
.build('codecatalystDeveloperTools', 'codecatalyst')
40-
.asTreeNode({
41-
label: 'Start',
42-
iconPath: getIcon('vscode-debug-start'),
43-
}),
38+
showManageConnections.build(placeholder, 'codecatalystDeveloperTools', 'codecatalyst').asTreeNode({
39+
label: 'Start',
40+
iconPath: getIcon('vscode-debug-start'),
41+
}),
4442
learnMoreCommand.build(docsUrl).asTreeNode({
4543
label: 'Learn More about CodeCatalyst',
4644
iconPath: getIcon('vscode-question'),

src/codewhisperer/activation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {
3636
notifyNewCustomizationsCmd,
3737
connectWithCustomization,
3838
signoutCodeWhisperer,
39-
showManageConnections,
39+
showManageCwConnections,
4040
} from './commands/basicCommands'
4141
import { sleep } from '../shared/utilities/timeoutUtils'
4242
import { ReferenceLogViewProvider } from './service/referenceLogViewProvider'
@@ -94,7 +94,7 @@ export async function activate(context: ExtContext): Promise<void> {
9494

9595
context.extensionContext.subscriptions.push(
9696
signoutCodeWhisperer.register(auth),
97-
showManageConnections.register(),
97+
showManageCwConnections.register(),
9898
/**
9999
* Configuration change
100100
*/

0 commit comments

Comments
 (0)