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

Throw Better Error when running registries commands when No Registries are Connected #4101

Merged
merged 4 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
9 changes: 5 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": "file:../vscode-docker-extensibility/packages/vscode-docker-registries/microsoft-vscode-docker-registries-0.1.2.tgz",
"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,17 @@ 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();
const root = ext.genericRegistryV2DataProvider.getRoot();
const registries = await ext.genericRegistryV2DataProvider.getRegistries(root);

if (registries?.length > 0) {
// if there are already registries, add a new registry to the existing root node
await ext.genericRegistryV2DataProvider.addTrackedRegistry();
} else {
// if there are no registries, connect as usual
await ext.registriesTree.connectRegistryProvider(ext.genericRegistryV2DataProvider);
alexyaang marked this conversation as resolved.
Show resolved Hide resolved
}

// don't wait
void ext.registriesTree.refresh();
}
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