Skip to content

Commit

Permalink
Added Back pushImage() (#4034)
Browse files Browse the repository at this point in the history
* make pushImage work

* added getAllRegistries command

* added getLoginInformation()

* account for cases where registry in undefined

* added filtering to make get all registries more optima

* added logic to filter github repositories
  • Loading branch information
alexyaang authored Aug 17, 2023
1 parent c967595 commit 6df7928
Show file tree
Hide file tree
Showing 10 changed files with 224 additions and 138 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@
},
{
"command": "vscode-docker.registries.copyRemoteFullTag",
"when": "view == dockerRegistries && viewItem =~ /(dockerHubTag|registryV2Tag)/i",
"when": "view == dockerRegistries && viewItem =~ /(dockerHubTag|registryV2Tag|azureContainerTag)/i",
"group": "regs_tag_1_general@2"
},
{
Expand Down
170 changes: 92 additions & 78 deletions src/commands/images/pushImage.ts
Original file line number Diff line number Diff line change
@@ -1,78 +1,92 @@
// /*---------------------------------------------------------------------------------------------
// * Copyright (c) Microsoft Corporation. All rights reserved.
// * Licensed under the MIT License. See LICENSE.md in the project root for license information.
// *--------------------------------------------------------------------------------------------*/

// import { IActionContext, NoResourceFoundError, contextValueExperience } from '@microsoft/vscode-azext-utils';
// import * as vscode from 'vscode';
// import { ext } from '../../extensionVariables';
// import { TaskCommandRunnerFactory } from '../../runtimes/runners/TaskCommandRunnerFactory';
// import { ImageTreeItem } from '../../tree/images/ImageTreeItem';
// import { UnifiedRegistryItem } from '../../tree/registries/UnifiedRegistryTreeDataProvider';
// import { addImageTaggingTelemetry, tagImage } from './tagImage';

// export async function pushImage(context: IActionContext, node: ImageTreeItem | undefined): Promise<void> {
// if (!node) {
// await ext.imagesTree.refresh(context);
// node = await ext.imagesTree.showTreeItemPicker<ImageTreeItem>(ImageTreeItem.contextValue, {
// ...context,
// noItemFoundErrorMessage: vscode.l10n.t('No images are available to push'),
// });
// }

// let connectedRegistry: UnifiedRegistryItem<unknown> | undefined;

// if (!node.fullTag.includes('/')) {
// // The registry to push to is indeterminate--could be Docker Hub, or could need tagging.
// const prompt: boolean = vscode.workspace.getConfiguration('docker').get('promptForRegistryWhenPushingImages', true);

// // If the prompt setting is true, we'll ask; if not we'll assume Docker Hub.
// if (prompt) {
// try {
// connectedRegistry = await contextValueExperience(context, ext.registriesRoot, { include: 'commonregistry' });
// } catch (error) {
// if (error instanceof NoResourceFoundError) {
// // Do nothing, move on without a selected registry
// } else {
// // Rethrow
// throw error;
// }
// }
// } else {
// // Try to find a connected Docker Hub registry (primarily for login credentials)
// connectedRegistry = await contextValueExperience(context, ext.registriesRoot, { include: 'dockerHubRegistry' });
// }
// } else {
// // The registry to push to is determinate. If there's a connected registry in the tree view, we'll try to find it, to perform login ahead of time.
// // Registry path is everything up to the last slash.
// const baseImagePath = node.fullTag.substring(0, node.fullTag.lastIndexOf('/'));

// const progressOptions: vscode.ProgressOptions = {
// location: vscode.ProgressLocation.Notification,
// title: vscode.l10n.t('Fetching login credentials...'),
// };

// // connectedRegistry = await vscode.window.withProgress(progressOptions, async () => await tryGetConnectedRegistryForPath(context, baseImagePath)); TODO: review this later
// }

// // Give the user a chance to modify the tag however they want
// const finalTag = await tagImage(context, node, connectedRegistry);

// // if (connectedRegistry && finalTag.startsWith(connectedRegistry.baseImagePath)) {
// // // If a registry was found/chosen and is still the same as the final tag's registry, try logging in
// // await vscode.commands.executeCommand('vscode-docker.registries.logInToDockerCli', connectedRegistry);
// // } TODO: review this later

// addImageTaggingTelemetry(context, finalTag, '');

// const client = await ext.runtimeManager.getClient();
// const taskCRF = new TaskCommandRunnerFactory(
// {
// taskName: finalTag
// }
// );

// await taskCRF.getCommandRunner()(
// client.pushImage({ imageRef: finalTag })
// );
// }
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE.md in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { IActionContext, NoResourceFoundError, contextValueExperience } from '@microsoft/vscode-azext-utils';
import { CommonRegistry } from '@microsoft/vscode-docker-registries';
import * as vscode from 'vscode';
import { ext } from '../../extensionVariables';
import { TaskCommandRunnerFactory } from '../../runtimes/runners/TaskCommandRunnerFactory';
import { ImageTreeItem } from '../../tree/images/ImageTreeItem';
import { UnifiedRegistryItem } from '../../tree/registries/UnifiedRegistryTreeDataProvider';
import { getBaseImagePathFromRegistryItem } from '../../tree/registries/registryTreeUtils';
import { addImageTaggingTelemetry, tagImage } from './tagImage';

export async function pushImage(context: IActionContext, node: ImageTreeItem | undefined): Promise<void> {
if (!node) {
await ext.imagesTree.refresh(context);
node = await ext.imagesTree.showTreeItemPicker<ImageTreeItem>(ImageTreeItem.contextValue, {
...context,
noItemFoundErrorMessage: vscode.l10n.t('No images are available to push'),
});
}

let connectedRegistry: UnifiedRegistryItem<CommonRegistry> | undefined;

if (!node.fullTag.includes('/')) {
// The registry to push to is indeterminate--could be Docker Hub, or could need tagging.
const prompt: boolean = vscode.workspace.getConfiguration('docker').get('promptForRegistryWhenPushingImages', true);

// If the prompt setting is true, we'll ask; if not we'll assume Docker Hub.
if (prompt) {
try {
connectedRegistry = await contextValueExperience(context, ext.registriesTree, { include: ['commonregistry'] });
} catch (error) {
if (error instanceof NoResourceFoundError) {
// Do nothing, move on without a selected registry
} else {
// Rethrow
throw error;
}
}
} else {
// Try to find a connected Docker Hub registry (primarily for login credentials)
connectedRegistry = await contextValueExperience(context, ext.registriesTree, { include: ['dockerHubRegistry'] });
}
} else {
// The registry to push to is determinate. If there's a connected registry in the tree view, we'll try to find it, to perform login ahead of time.
// Registry path is everything up to the last slash.
const baseImagePath = node.fullTag.substring(0, node.fullTag.lastIndexOf('/'));

const progressOptions: vscode.ProgressOptions = {
location: vscode.ProgressLocation.Notification,
title: vscode.l10n.t('Fetching login credentials...'),
};

connectedRegistry = await vscode.window.withProgress(progressOptions, async () => await tryGetConnectedRegistryForPath(context, baseImagePath));
}

// Give the user a chance to modify the tag however they want
const finalTag = await tagImage(context, node, connectedRegistry);

if (connectedRegistry && finalTag.startsWith(getBaseImagePathFromRegistryItem(connectedRegistry.wrappedItem))) {
// If a registry was found/chosen and is still the same as the final tag's registry, try logging in
await vscode.commands.executeCommand('vscode-docker.registries.logInToDockerCli', connectedRegistry);
}

addImageTaggingTelemetry(context, finalTag, '');

const client = await ext.runtimeManager.getClient();
const taskCRF = new TaskCommandRunnerFactory(
{
taskName: finalTag
}
);

await taskCRF.getCommandRunner()(
client.pushImage({ imageRef: finalTag })
);
}

async function tryGetConnectedRegistryForPath(context: IActionContext, baseImagePath: string): Promise<UnifiedRegistryItem<CommonRegistry> | undefined> {
const allRegistries = await ext.registriesTree.getConnectedRegistries(vscode.Uri.parse(baseImagePath));

let matchedRegistry = allRegistries.find((registry) => getBaseImagePathFromRegistryItem(registry.wrappedItem) === baseImagePath);

if (!matchedRegistry) {
matchedRegistry = await contextValueExperience(context, ext.registriesTree, { include: ['commonregistry'] });
}

return matchedRegistry;
}
5 changes: 4 additions & 1 deletion src/commands/images/tagImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
*--------------------------------------------------------------------------------------------*/

import { IActionContext, TelemetryProperties } from '@microsoft/vscode-azext-utils';
import { CommonRegistry, isRegistry } from '@microsoft/vscode-docker-registries';
import * as vscode from 'vscode';
import { ext } from '../../extensionVariables';
import { ImageTreeItem } from '../../tree/images/ImageTreeItem';
import { UnifiedRegistryItem } from '../../tree/registries/UnifiedRegistryTreeDataProvider';
import { getBaseImagePathFromRegistryItem } from '../../tree/registries/registryTreeUtils';

export async function tagImage(context: IActionContext, node?: ImageTreeItem, registry?: UnifiedRegistryItem<unknown>): Promise<string> {
if (!node) {
Expand All @@ -19,7 +21,8 @@ export async function tagImage(context: IActionContext, node?: ImageTreeItem, re
}

addImageTaggingTelemetry(context, node.fullTag, '.before');
const newTaggedName: string = await getTagFromUserInput(context, node.fullTag, ''); // TODO: review this later
const baseImagePath = isRegistry(registry) ? getBaseImagePathFromRegistryItem(registry.wrappedItem as CommonRegistry) : undefined;
const newTaggedName: string = await getTagFromUserInput(context, node.fullTag, baseImagePath);
addImageTaggingTelemetry(context, newTaggedName, '.after');

await ext.runWithDefaults(client =>
Expand Down
31 changes: 9 additions & 22 deletions src/commands/registerCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,19 @@ import { inspectDockerContext } from "./context/inspectDockerContext";
import { removeDockerContext } from "./context/removeDockerContext";
import { useDockerContext } from "./context/useDockerContext";
import { help } from "./help";
// import { buildImage } from "./images/buildImage";
import { buildImage } from "./images/buildImage";
import { configureImagesExplorer } from "./images/configureImagesExplorer";
import { copyFullTag } from "./images/copyFullTag";
import { inspectImage } from "./images/inspectImage";
import { pruneImages } from "./images/pruneImages";
import { pullImage } from "./images/pullImage";
import { pushImage } from "./images/pushImage";
import { removeImage } from "./images/removeImage";
import { removeImageGroup } from "./images/removeImageGroup";
import { runAzureCliImage } from "./images/runAzureCliImage";
import { runImage, runImageInteractive } from "./images/runImage";
import { hideDanglingImages, setInitialDanglingContextValue, showDanglingImages } from "./images/showDanglingImages";
import { tagImage } from "./images/tagImage";
import { configureNetworksExplorer } from "./networks/configureNetworksExplorer";
import { createNetwork } from "./networks/createNetwork";
import { inspectNetwork } from "./networks/inspectNetwork";
Expand All @@ -50,29 +52,14 @@ import { removeNetwork } from "./networks/removeNetwork";
import { pruneSystem } from "./pruneSystem";
import { registerWorkspaceCommand } from "./registerWorkspaceCommand";
import { createAzureRegistry } from "./registries/azure/createAzureRegistry";
// import { deleteAzureRegistry } from "./registries/azure/deleteAzureRegistry";
// import { deleteAzureRepository } from "./registries/azure/deleteAzureRepository";
// import { deployImageToAzure } from "./registries/azure/deployImageToAzure";
import { openInAzurePortal } from "./registries/azure/openInAzurePortal";
// import { buildImageInAzure } from "./registries/azure/tasks/buildImageInAzure";
import { runAzureTask } from "./registries/azure/tasks/runAzureTask";
// import { runFileAsAzureTask } from "./registries/azure/tasks/runFileAsAzureTask";
// import { viewAzureTaskLogs } from "./registries/azure/tasks/viewAzureTaskLogs";
// import { untagAzureImage } from "./registries/azure/untagAzureImage";
// import { viewAzureProperties } from "./registries/azure/viewAzureProperties";
import { connectRegistry } from "./registries/connectRegistry";
// import { copyRemoteFullTag } from './registries/copyRemoteFullTag';
// import { copyRemoteImageDigest } from "./registries/copyRemoteImageDigest";
// import { deleteRemoteImage } from "./registries/deleteRemoteImage";
// import { logInToDockerCli } from "./registries/logInToDockerCli";
// import { logOutOfDockerCli } from "./registries/logOutOfDockerCli";
// import { pullImageFromRepository, pullRepository } from "./registries/pullImages";
// import { reconnectRegistry } from "./registries/reconnectRegistry";
import { deleteAzureRegistry } from "./registries/azure/deleteAzureRegistry";
import { deleteAzureRepository } from "./registries/azure/deleteAzureRepository";
import { openInAzurePortal } from "./registries/azure/openInAzurePortal";
import { buildImageInAzure } from "./registries/azure/tasks/buildImageInAzure";
import { runAzureTask } from "./registries/azure/tasks/runAzureTask";
import { untagAzureImage } from "./registries/azure/untagAzureImage";
import { viewAzureProperties } from "./registries/azure/viewAzureProperties";
import { connectRegistry } from "./registries/connectRegistry";
import { copyRemoteFullTag } from "./registries/copyRemoteFullTag";
import { copyRemoteImageDigest } from "./registries/copyRemoteImageDigest";
import { disconnectRegistry } from "./registries/disconnectRegistry";
Expand Down Expand Up @@ -150,21 +137,21 @@ export function registerCommands(): void {
registerWorkspaceCommand('vscode-docker.containers.composeGroup.restart', composeGroupRestart);
registerWorkspaceCommand('vscode-docker.containers.composeGroup.down', composeGroupDown);

// registerWorkspaceCommand('vscode-docker.images.build', buildImage);
registerWorkspaceCommand('vscode-docker.images.build', buildImage);
registerCommand('vscode-docker.images.configureExplorer', configureImagesExplorer);
registerCommand('vscode-docker.images.inspect', inspectImage);
registerCommand('vscode-docker.images.prune', pruneImages);
registerCommand('vscode-docker.images.showDangling', showDanglingImages);
registerCommand('vscode-docker.images.hideDangling', hideDanglingImages);
setInitialDanglingContextValue();
registerWorkspaceCommand('vscode-docker.images.pull', pullImage);
// registerWorkspaceCommand('vscode-docker.images.push', pushImage);
registerWorkspaceCommand('vscode-docker.images.push', pushImage);
registerCommand('vscode-docker.images.remove', removeImage);
registerCommand('vscode-docker.images.group.remove', removeImageGroup);
registerWorkspaceCommand('vscode-docker.images.run', runImage);
registerWorkspaceCommand('vscode-docker.images.runAzureCli', runAzureCliImage);
registerWorkspaceCommand('vscode-docker.images.runInteractive', runImageInteractive);
// registerCommand('vscode-docker.images.tag', tagImage);
registerCommand('vscode-docker.images.tag', tagImage);
registerCommand('vscode-docker.images.copyFullTag', copyFullTag);

registerCommand('vscode-docker.networks.configureExplorer', configureNetworksExplorer);
Expand Down
4 changes: 2 additions & 2 deletions src/commands/registries/azure/untagAzureImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import { contextValueExperience, IActionContext } from "@microsoft/vscode-azext-
import { l10n, ProgressLocation, window } from "vscode";
import { ext } from "../../../extensionVariables";
import { AzureRegistryDataProvider, AzureTag } from "../../../tree/registries/Azure/AzureRegistryDataProvider";
import { getFullImageNameFromRegistryItem } from "../../../tree/registries/registryTreeUtils";
import { getFullImageNameFromRegistryTagItem } from "../../../tree/registries/registryTreeUtils";
import { UnifiedRegistryItem } from "../../../tree/registries/UnifiedRegistryTreeDataProvider";

export async function untagAzureImage(context: IActionContext, node?: UnifiedRegistryItem<AzureTag>): Promise<void> {
if (!node) {
node = await contextValueExperience(context, ext.registriesTree, { include: 'azureContainerTag' });
}

const fullTag = getFullImageNameFromRegistryItem(node);
const fullTag = getFullImageNameFromRegistryTagItem(node.wrappedItem);
const confirmUntag: string = l10n.t('Are you sure you want to untag image "{0}"? This does not delete the manifest referenced by the tag.', fullTag);
// no need to check result - cancel will throw a UserCancelledError
await context.ui.showWarningMessage(confirmUntag, { modal: true }, { title: "Untag" });
Expand Down
4 changes: 2 additions & 2 deletions src/commands/registries/copyRemoteFullTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import { CommonTag } from '@microsoft/vscode-docker-registries';
import * as vscode from 'vscode';
import { ext } from '../../extensionVariables';
import { UnifiedRegistryItem } from '../../tree/registries/UnifiedRegistryTreeDataProvider';
import { getFullImageNameFromRegistryItem } from '../../tree/registries/registryTreeUtils';
import { getFullImageNameFromRegistryTagItem } from '../../tree/registries/registryTreeUtils';

export async function copyRemoteFullTag(context: IActionContext, node?: UnifiedRegistryItem<CommonTag>): Promise<string> {
if (!node) {
node = await contextValueExperience(context, ext.registriesTree, { include: ['registryV2Tag', 'dockerHubTag'] });
}
const fullTag = getFullImageNameFromRegistryItem(node);
const fullTag = getFullImageNameFromRegistryTagItem(node.wrappedItem);
void vscode.env.clipboard.writeText(fullTag);
return fullTag;
}
4 changes: 2 additions & 2 deletions src/commands/registries/pullImages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { CommonRegistry, CommonRepository, CommonTag } from '@microsoft/vscode-d
import { ext } from '../../extensionVariables';
import { TaskCommandRunnerFactory } from '../../runtimes/runners/TaskCommandRunnerFactory';
import { UnifiedRegistryItem } from '../../tree/registries/UnifiedRegistryTreeDataProvider';
import { getImageNameFromRegistryItem } from '../../tree/registries/registryTreeUtils';
import { getImageNameFromRegistryTagItem } from '../../tree/registries/registryTreeUtils';
import { logInToDockerCli } from './logInToDockerCli';

export async function pullRepository(context: IActionContext, node?: UnifiedRegistryItem<CommonRepository>): Promise<void> {
Expand All @@ -24,7 +24,7 @@ export async function pullImageFromRepository(context: IActionContext, node?: Un
node = await contextValueExperience(context, ext.registriesTree, { include: 'commontag' });
}

await pullImages(context, node.parent.parent, getImageNameFromRegistryItem(node), false);
await pullImages(context, node.parent.parent, getImageNameFromRegistryTagItem(node.wrappedItem), false);
}

async function pullImages(context: IActionContext, node: UnifiedRegistryItem<unknown>, imageRequest: string, allTags: boolean): Promise<void> {
Expand Down
Loading

0 comments on commit 6df7928

Please sign in to comment.