|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { ContainerAppsAPIClient, KnownActiveRevisionsMode, Revision } from "@azure/arm-appcontainers"; |
| 7 | +import { uiUtils } from "@microsoft/vscode-azext-azureutils"; |
| 8 | +import { IActionContext, IAzureQuickPickItem, createSubscriptionContext, nonNullProp, nonNullValueAndProp } from "@microsoft/vscode-azext-utils"; |
| 9 | +import * as dayjs from "dayjs"; |
| 10 | +// eslint-disable-next-line import/no-internal-modules |
| 11 | +import * as relativeTime from 'dayjs/plugin/relativeTime'; |
| 12 | +import { ext } from "../../extensionVariables"; |
| 13 | +import type { RevisionItem } from "../../tree/revisionManagement/RevisionItem"; |
| 14 | +import { RevisionsItem } from "../../tree/revisionManagement/RevisionsItem"; |
| 15 | +import { createContainerAppsAPIClient } from "../../utils/azureClients"; |
| 16 | +import { delay } from "../../utils/delay"; |
| 17 | +import { localize } from "../../utils/localize"; |
| 18 | +import { pickContainerApp } from "../../utils/pickContainerApp"; |
| 19 | +import { pickRevisionItem } from "../../utils/pickRevisionItem"; |
| 20 | +import type { IContainerAppContext } from "../IContainerAppContext"; |
| 21 | + |
| 22 | +dayjs.extend(relativeTime); |
| 23 | + |
| 24 | +export async function createRevisionDraft(context: IActionContext, node?: RevisionsItem): Promise<void> { |
| 25 | + const containerAppsItem = node ?? await pickContainerApp(context); |
| 26 | + |
| 27 | + if (containerAppsItem.containerApp.revisionsMode !== KnownActiveRevisionsMode.Multiple) { |
| 28 | + throw new Error(localize('revisionsModeError', 'You must be in multiple revisions mode to run this command.')); |
| 29 | + } else if (ext.revisionDraftFileSystem.doesContainerAppsItemHaveRevisionDraft(containerAppsItem)) { |
| 30 | + throw new Error(localize('revisionDraftExists', 'A revision draft already exists for container app "{0}".', containerAppsItem.containerApp.name)); |
| 31 | + } |
| 32 | + |
| 33 | + const { subscription, containerApp } = containerAppsItem; |
| 34 | + const containerAppContext: IContainerAppContext = { |
| 35 | + ...context, |
| 36 | + ...createSubscriptionContext(subscription), |
| 37 | + containerApp, |
| 38 | + subscription |
| 39 | + }; |
| 40 | + |
| 41 | + /** |
| 42 | + * Overwrite the typical 'pickRevisionItem' behavior with custom behavior. |
| 43 | + * Leverage the `selectRevisionName` option to obtain the RevisionItem without re-prompting the user |
| 44 | + */ |
| 45 | + const revisionName: string | undefined = await promptForRevisionName(containerAppContext); |
| 46 | + const revisionItem: RevisionItem = await pickRevisionItem(context, containerAppsItem, { |
| 47 | + selectByRevisionName: revisionName |
| 48 | + }); |
| 49 | + |
| 50 | + await ext.state.showCreatingChild( |
| 51 | + `${revisionItem.containerApp.id}/${RevisionsItem.idSuffix}`, |
| 52 | + localize('creatingDraft', 'Creating draft...'), |
| 53 | + async () => { |
| 54 | + // Add a short delay to display the creating message |
| 55 | + await delay(5); |
| 56 | + ext.revisionDraftFileSystem.createRevisionDraft(revisionItem); |
| 57 | + } |
| 58 | + ); |
| 59 | +} |
| 60 | + |
| 61 | +async function promptForRevisionName(context: IContainerAppContext): Promise<string | undefined> { |
| 62 | + const revisionPicks: IAzureQuickPickItem<Revision | undefined>[] = await getRevisionNamePicks(context); |
| 63 | + if (revisionPicks.length === 1) { |
| 64 | + return revisionPicks[0].data?.name; |
| 65 | + } |
| 66 | + |
| 67 | + return (await context.ui.showQuickPick(revisionPicks, { |
| 68 | + placeHolder: localize('selectBaseRevision', 'Select a base revision'), |
| 69 | + suppressPersistence: true |
| 70 | + })).data?.name; |
| 71 | +} |
| 72 | + |
| 73 | +async function getRevisionNamePicks(context: IContainerAppContext): Promise<IAzureQuickPickItem<Revision | undefined>[]> { |
| 74 | + const rgName: string = nonNullValueAndProp(context.containerApp, 'resourceGroup'); |
| 75 | + const caName: string = nonNullValueAndProp(context.containerApp, 'name'); |
| 76 | + |
| 77 | + const client: ContainerAppsAPIClient = await createContainerAppsAPIClient(context); |
| 78 | + |
| 79 | + const revisionsIterator = client.containerAppsRevisions.listRevisions(rgName, caName); |
| 80 | + const revisions: Revision[] = await uiUtils.listAllIterator(revisionsIterator); |
| 81 | + |
| 82 | + if (context.containerApp?.revisionsMode === KnownActiveRevisionsMode.Single) { |
| 83 | + return [ |
| 84 | + { |
| 85 | + label: 'Latest', |
| 86 | + data: revisions.find((revision: Revision) => revision.name === context.containerApp?.latestRevisionName) |
| 87 | + } |
| 88 | + ]; |
| 89 | + } |
| 90 | + |
| 91 | + return revisions |
| 92 | + .map((revision: Revision) => { |
| 93 | + const revisionName = nonNullProp(revision, 'name'); |
| 94 | + const day = revision.createdTime?.toLocaleDateString(undefined, { |
| 95 | + day: '2-digit', |
| 96 | + month: '2-digit', |
| 97 | + year: '2-digit' |
| 98 | + }); |
| 99 | + const time = revision.createdTime?.toLocaleTimeString(undefined, { |
| 100 | + hour: '2-digit', |
| 101 | + minute: '2-digit', |
| 102 | + second: '2-digit' |
| 103 | + }); |
| 104 | + const timeAgo = dayjs(revision.createdTime).fromNow(); |
| 105 | + |
| 106 | + return { |
| 107 | + label: revisionName === context.containerApp?.latestRevisionName ? `${revisionName}` : revisionName, |
| 108 | + description: (day && time && timeAgo) ? |
| 109 | + `${day} ${time} (${revisionName === context.containerApp?.latestRevisionName ? 'Latest' : timeAgo})` : |
| 110 | + '', |
| 111 | + data: revision, |
| 112 | + }; |
| 113 | + }) |
| 114 | + .sort((a: IAzureQuickPickItem<Revision>, b: IAzureQuickPickItem<Revision>) => { |
| 115 | + const aCreatedTime = nonNullValueAndProp(a.data, 'createdTime'); |
| 116 | + const bCreatedTime = nonNullValueAndProp(b.data, 'createdTime'); |
| 117 | + |
| 118 | + if (aCreatedTime > bCreatedTime) { |
| 119 | + return -1; |
| 120 | + } else if (aCreatedTime < bCreatedTime) { |
| 121 | + return 1; |
| 122 | + } else { |
| 123 | + return 0; |
| 124 | + } |
| 125 | + }); |
| 126 | +} |
0 commit comments