-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathUploadSourceCodeStep.ts
More file actions
77 lines (63 loc) · 4.8 KB
/
UploadSourceCodeStep.ts
File metadata and controls
77 lines (63 loc) · 4.8 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.md in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { getResourceGroupFromId } from '@microsoft/vscode-azext-azureutils';
import { AzExtFsExtra, GenericParentTreeItem, GenericTreeItem, activityFailContext, activityFailIcon, activitySuccessContext, activitySuccessIcon, nonNullValue } from '@microsoft/vscode-azext-utils';
import * as path from 'path';
import { type Progress } from 'vscode';
import { fse } from '../../../../node/fs-extra';
import { tar } from '../../../../node/tar';
import { ExecuteActivityOutputStepBase, type ExecuteActivityOutput } from '../../../../utils/activity/ExecuteActivityOutputStepBase';
import { createActivityChildContext } from '../../../../utils/activity/activityUtils';
import { createContainerRegistryManagementClient } from '../../../../utils/azureClients';
import { localize } from '../../../../utils/localize';
import { type BuildImageInAzureImageSourceContext } from './BuildImageInAzureImageSourceContext';
const vcsIgnoreList = ['.git', '.gitignore', '.bzr', 'bzrignore', '.hg', '.hgignore', '.svn'];
export class UploadSourceCodeStep extends ExecuteActivityOutputStepBase<BuildImageInAzureImageSourceContext> {
public priority: number = 430;
/** Relative path of src folder from rootFolder and what gets deployed */
private _sourceFilePath: string;
protected async executeCore(context: BuildImageInAzureImageSourceContext, progress: Progress<{ message?: string | undefined; increment?: number | undefined }>): Promise<void> {
this._sourceFilePath = context.rootFolder.uri.fsPath === context.srcPath ? '.' : path.relative(context.rootFolder.uri.fsPath, context.srcPath);
context.telemetry.properties.sourceDepth = this._sourceFilePath === '.' ? '0' : String(this._sourceFilePath.split(path.sep).length);
context.registryName = nonNullValue(context.registry?.name);
context.resourceGroupName = getResourceGroupFromId(nonNullValue(context.registry?.id));
context.client = await createContainerRegistryManagementClient(context);
progress.report({ message: localize('uploadingSourceCode', 'Uploading source code...') });
const source: string = path.join(context.rootFolder.uri.fsPath, this._sourceFilePath);
let items = await AzExtFsExtra.readDirectory(source);
items = items.filter(i => !vcsIgnoreList.includes(i.name));
tar.c({ cwd: source }, items.map(i => i.name)).pipe(fse.createWriteStream(context.tarFilePath));
const sourceUploadLocation = await context.client.registries.getBuildSourceUploadUrl(context.resourceGroupName, context.registryName);
const uploadUrl: string = nonNullValue(sourceUploadLocation.uploadUrl);
const relativePath: string = nonNullValue(sourceUploadLocation.relativePath);
const storageBlob = await import('@azure/storage-blob');
const blobClient = new storageBlob.BlockBlobClient(uploadUrl);
await blobClient.uploadFile(context.tarFilePath);
context.uploadedSourceLocation = relativePath;
}
public shouldExecute(context: BuildImageInAzureImageSourceContext): boolean {
return !context.uploadedSourceLocation;
}
protected createSuccessOutput(context: BuildImageInAzureImageSourceContext): ExecuteActivityOutput {
return {
item: new GenericTreeItem(undefined, {
contextValue: createActivityChildContext(['uploadSourceCodeStepSuccessItem', activitySuccessContext]),
label: localize('uploadSourceCodeLabel', 'Upload source code from "{1}" directory to registry "{0}"', context.registry?.name, this._sourceFilePath),
iconPath: activitySuccessIcon
}),
message: localize('uploadedSourceCodeSuccess', 'Uploaded source code from "{1}" directory to registry "{0}" for remote build.', context.registry?.name, this._sourceFilePath)
};
}
protected createFailOutput(context: BuildImageInAzureImageSourceContext): ExecuteActivityOutput {
return {
item: new GenericParentTreeItem(undefined, {
contextValue: createActivityChildContext(['uploadSourceCodeStepFailItem', activityFailContext]),
label: localize('uploadSourceCodeLabel', 'Upload source code from "{1}" directory to registry "{0}"', context.registry?.name, this._sourceFilePath),
iconPath: activityFailIcon
}),
message: localize('uploadedSourceCodeFail', 'Failed to upload source code from "{1}" directory to registry "{0}" for remote build.', context.registry?.name, this._sourceFilePath)
};
}
}