-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathdeployRevisionDraft.ts
More file actions
91 lines (75 loc) · 4.55 KB
/
deployRevisionDraft.ts
File metadata and controls
91 lines (75 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { ContainerAppsAPIClient, KnownActiveRevisionsMode, Revision } from "@azure/arm-appcontainers";
import { uiUtils } from "@microsoft/vscode-azext-azureutils";
import { AzureWizard, AzureWizardExecuteStep, AzureWizardPromptStep, IActionContext, createSubscriptionContext, nonNullProp, nonNullValue, nonNullValueAndProp } from "@microsoft/vscode-azext-utils";
import * as deepEqual from "deep-eql";
import { ext } from "../../../extensionVariables";
import type { ContainerAppItem, ContainerAppModel } from "../../../tree/ContainerAppItem";
import { RevisionDraftItem } from "../../../tree/revisionManagement/RevisionDraftItem";
import { createActivityContext } from "../../../utils/activityUtils";
import { createContainerAppsAPIClient } from "../../../utils/azureClients";
import { delay } from "../../../utils/delay";
import { localize } from "../../../utils/localize";
import { pickContainerApp } from "../../../utils/pickContainerApp";
import { DeployRevisionDraftConfirmStep } from "./DeployRevisionDraftConfirmStep";
import { DeployRevisionDraftStep } from "./DeployRevisionDraftStep";
import type { IDeployRevisionDraftContext } from "./IDeployRevisionDraftContext";
export async function deployRevisionDraft(context: IActionContext, node?: ContainerAppItem | RevisionDraftItem): Promise<void> {
const item = node ?? await pickContainerApp(context);
if (!ext.revisionDraftFileSystem.doesContainerAppsItemHaveRevisionDraft(item)) {
throw new Error(localize('noDraftExists', 'No draft changes exist for container app "{0}".', item.containerApp.name));
}
const { subscription, containerApp } = item;
const wizardContext: IDeployRevisionDraftContext = {
...context,
...createSubscriptionContext(subscription),
...(await createActivityContext()),
subscription,
containerApp,
template: nonNullValue(ext.revisionDraftFileSystem.parseRevisionDraft(item)),
};
if (!await hasUnsavedChanges(wizardContext, item)) {
throw new Error(localize('noUnsavedChanges', 'No unsaved changes detected to deploy to container app "{0}".', containerApp.name));
}
const promptSteps: AzureWizardPromptStep<IDeployRevisionDraftContext>[] = [
new DeployRevisionDraftConfirmStep()
];
const executeSteps: AzureWizardExecuteStep<IDeployRevisionDraftContext>[] = [
new DeployRevisionDraftStep()
];
const wizard: AzureWizard<IDeployRevisionDraftContext> = new AzureWizard(wizardContext, {
title: localize('deploy', 'Deploy changes to container app "{0}"', containerApp.name),
promptSteps,
executeSteps,
});
await wizard.prompt();
await wizard.execute();
if (item.containerApp.revisionsMode === KnownActiveRevisionsMode.Single) {
ext.revisionDraftFileSystem.discardRevisionDraft(item);
} else {
await ext.state.showDeleting(
`${item.containerApp.id}/${RevisionDraftItem.idSuffix}`,
async () => {
// Add a short delay to display the deleting message
await delay(5);
ext.revisionDraftFileSystem.discardRevisionDraft(item);
}
);
}
ext.state.notifyChildrenChanged(item.containerApp.managedEnvironmentId);
}
async function hasUnsavedChanges(context: IDeployRevisionDraftContext, item: ContainerAppItem | RevisionDraftItem): Promise<boolean> {
const containerApp: ContainerAppModel = nonNullProp(context, 'containerApp');
if (context.containerApp?.revisionsMode === KnownActiveRevisionsMode.Single) {
return !!containerApp.template && !deepEqual(containerApp.template, context.template);
} else {
const client: ContainerAppsAPIClient = await createContainerAppsAPIClient(context);
const revisions: Revision[] = await uiUtils.listAllIterator(client.containerAppsRevisions.listRevisions(containerApp.resourceGroup, containerApp.name));
const baseRevisionName: string = nonNullValueAndProp(ext.revisionDraftFileSystem.getRevisionDraftFile(item), 'baseRevisionName');
const baseRevision: Revision | undefined = revisions.find(revision => revision.name === baseRevisionName);
return !!baseRevision?.template && !deepEqual(baseRevision.template, context.template);
}
}