Skip to content

Commit

Permalink
Throw Better Error when running registries commands when No Registrie…
Browse files Browse the repository at this point in the history
…s are Connected (#4101)

* fix genericV2 root not showing up when adding registry

* throw good error when no registries are connected

* use hasTrackedRegistries function

* use npm dot com
  • Loading branch information
alexyaang authored Sep 27, 2023
1 parent f53de2c commit 007bc79
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 21 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3007,7 +3007,7 @@
"@microsoft/vscode-azext-azureutils": "^2.0.0",
"@microsoft/vscode-azext-utils": "^2.1.1",
"@microsoft/vscode-container-client": "^0.1.0",
"@microsoft/vscode-docker-registries": "^0.1.1",
"@microsoft/vscode-docker-registries": "^0.1.2",
"dayjs": "^1.11.7",
"dockerfile-language-server-nodejs": "^0.11.0",
"fs-extra": "^11.1.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import { ext } from "../../../extensionVariables";
import { UnifiedRegistryItem } from "../../../tree/registries/UnifiedRegistryTreeDataProvider";

export async function addTrackedGenericV2Registry(context: IActionContext, node?: UnifiedRegistryItem<unknown>): Promise<void> {
await ext.genericRegistryV2DataProvider.addTrackedRegistry();
// if there are already registries, add a new registry to the existing root node
if (ext.genericRegistryV2DataProvider.hasTrackedRegistries()) {
await ext.genericRegistryV2DataProvider.addTrackedRegistry();
} else {
// if there are no registries, connect as usual
await ext.registriesTree.connectRegistryProvider(ext.genericRegistryV2DataProvider);
}

// don't wait
void ext.registriesTree.refresh();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function removeTrackedGenericV2Registry(context: IActionContext, no
await ext.genericRegistryV2DataProvider.removeTrackedRegistry(node.wrappedItem);

// remove the provider if it's the last one
if ((await ext.genericRegistryV2DataProvider.getRegistries(node.parent.wrappedItem)).length === 0) {
if (!ext.genericRegistryV2DataProvider.hasTrackedRegistries()) {
await ext.registriesTree.disconnectRegistryProvider(node.parent);
}

Expand Down
33 changes: 33 additions & 0 deletions src/tree/registries/ConnectRegistryTreeItem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE.md in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { CommonRegistryItem, isCommonRegistryItem } from '@microsoft/vscode-docker-registries';
import * as vscode from 'vscode';
import { UnifiedRegistryItem } from "./UnifiedRegistryTreeDataProvider";

export const ConnectRegistryContextValue: string = 'connectregistry';

export function isConnectRegistryTreeItem(item: unknown): item is UnifiedRegistryItem<CommonRegistryItem> {
return isCommonRegistryItem(item) && item.type === ConnectRegistryContextValue;
}

/**
* Creates a tree item that can be used to connect a new registry
*/
export function getConnectRegistryTreeItem(): UnifiedRegistryItem<CommonRegistryItem> {
return {
provider: undefined,
parent: undefined,
wrappedItem: {
label: vscode.l10n.t('Connect Registry...'),
type: ConnectRegistryContextValue,
iconPath: new vscode.ThemeIcon('plug'),
command: {
command: 'vscode-docker.registries.connectRegistry'
},
parent: undefined
}
};
}
15 changes: 3 additions & 12 deletions src/tree/registries/UnifiedRegistryTreeDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { CommonRegistry, CommonRegistryRoot, RegistryDataProvider, isCommonRegis
import * as vscode from 'vscode';
import { ext } from '../../extensionVariables';
import { isAzureSubscriptionRegistryItem } from './Azure/AzureRegistryDataProvider';
import { getConnectRegistryTreeItem } from './ConnectRegistryTreeItem';

interface WrappedElement {
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand Down Expand Up @@ -85,18 +86,8 @@ export class UnifiedRegistryTreeDataProvider implements vscode.TreeDataProvider<

// if there are no connected providers, show a command to connect one
if (unifiedRegistryItems.length === 0) {
unifiedRegistryItems.push({
provider: undefined,
wrappedItem: {
label: vscode.l10n.t('Connect Registry...'),
type: 'connectregistry',
iconPath: new vscode.ThemeIcon('plug'),
command: {
command: 'vscode-docker.registries.connectRegistry'
}
},
parent: undefined,
});
const connectRegistryItem = getConnectRegistryTreeItem();
unifiedRegistryItems.push(connectRegistryItem);
}
}

Expand Down
21 changes: 19 additions & 2 deletions src/utils/registryExperience.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { AzureWizardPromptStep, ContextValueFilterQuickPickOptions, GenericQuickPickStep, IActionContext, PickFilter, QuickPickWizardContext, RecursiveQuickPickStep, runQuickPickWizard } from '@microsoft/vscode-azext-utils';
import { AzureWizardPromptStep, ContextValueFilterQuickPickOptions, GenericQuickPickStep, IActionContext, PickFilter, QuickPickWizardContext, RecursiveQuickPickStep, UserCancelledError, runQuickPickWizard } from '@microsoft/vscode-azext-utils';
import { CommonRegistryItem } from '@microsoft/vscode-docker-registries';
import { TreeItem, l10n } from 'vscode';
import { MessageItem, TreeItem, commands, l10n, window } from 'vscode';
import { ext } from '../extensionVariables';
import { isConnectRegistryTreeItem } from '../tree/registries/ConnectRegistryTreeItem';
import { UnifiedRegistryItem, UnifiedRegistryTreeDataProvider } from '../tree/registries/UnifiedRegistryTreeDataProvider';

export interface RegistryFilter {
Expand All @@ -27,6 +28,22 @@ export interface RegistryExperienceOptions extends Partial<ContextValueFilterQui
}

export async function registryExperience<TNode extends CommonRegistryItem>(context: IActionContext, options?: RegistryExperienceOptions): Promise<UnifiedRegistryItem<TNode>> {
const registryRoots = await ext.registriesTree.getChildren();
// if there are no registry providers, throw an error with option to connect a registry provider
if (registryRoots.length === 0 || (registryRoots.length === 1 && isConnectRegistryTreeItem(registryRoots[0].wrappedItem))) {
const add: MessageItem = { title: l10n.t('Connect Registry...') };
void window.showErrorMessage(
l10n.t('No registry providers are connected. Please connect a registry provider to continue.'),
...[add])
.then((result) => {
if (result === add) {
void commands.executeCommand('vscode-docker.registries.connectRegistry');
}
});

throw new UserCancelledError();
}

// get the registry provider unified item
const promptSteps: AzureWizardPromptStep<QuickPickWizardContext>[] = [
new RegistryQuickPickStep(ext.registriesTree, options)
Expand Down

0 comments on commit 007bc79

Please sign in to comment.