-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
main.ts
106 lines (89 loc) · 4.67 KB
/
main.ts
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
var path = require('path');
var url = require('url');
var fs = require('fs');
import * as tl from 'azure-pipelines-task-lib/task';
import * as models from 'artifact-engine/Models';
import * as engine from 'artifact-engine/Engine';
import * as providers from 'artifact-engine/Providers';
import * as webHandlers from 'artifact-engine/Providers/typed-rest-client/Handlers';
var taskJson = require('./task.json');
tl.setResourcePath(path.join(__dirname, 'task.json'));
const area: string = 'DownloadFileShareArtifacts';
function getDefaultProps() {
const hostType = (tl.getVariable('SYSTEM.HOSTTYPE') || "").toLowerCase();
return {
hostType: hostType,
definitionName: '[NonEmail:' + (hostType === 'release' ? tl.getVariable('RELEASE.DEFINITIONNAME') : tl.getVariable('BUILD.DEFINITIONNAME')) + ']',
processId: hostType === 'release' ? tl.getVariable('RELEASE.RELEASEID') : tl.getVariable('BUILD.BUILDID'),
processUrl: hostType === 'release' ? tl.getVariable('RELEASE.RELEASEWEBURL') : (tl.getVariable('SYSTEM.TEAMFOUNDATIONSERVERURI') + tl.getVariable('SYSTEM.TEAMPROJECT') + '/_build?buildId=' + tl.getVariable('BUILD.BUILDID')),
taskDisplayName: tl.getVariable('TASK.DISPLAYNAME'),
jobid: tl.getVariable('SYSTEM.JOBID'),
agentVersion: tl.getVariable('AGENT.VERSION'),
agentOS: tl.getVariable('AGENT.OS'),
agentName: tl.getVariable('AGENT.NAME'),
version: taskJson.version
};
}
function publishEvent(feature, properties: any): void {
try {
var splitVersion = (process.env.AGENT_VERSION || '').split('.');
var major = parseInt(splitVersion[0] || '0');
var minor = parseInt(splitVersion[1] || '0');
let telemetry = '';
if (major > 2 || (major == 2 && minor >= 120)) {
telemetry = `##vso[telemetry.publish area=${area};feature=${feature}]${JSON.stringify(Object.assign(getDefaultProps(), properties))}`;
}
else {
if (feature === 'reliability') {
let reliabilityData = properties;
telemetry = "##vso[task.logissue type=error;code=" + reliabilityData.issueType + ";agentVersion=" + tl.getVariable('Agent.Version') + ";taskId=" + area + "-" + JSON.stringify(taskJson.version) + ";]" + reliabilityData.errorMessage
}
}
console.log(telemetry);;
}
catch (err) {
tl.warning("Failed to log telemetry, error: " + err);
}
}
async function main(): Promise<void> {
const promise = new Promise<void>(async (resolve, reject) => {
const downloadPath: string = tl.getInput("downloadPath", true);
const debugMode: string = tl.getVariable('System.Debug');
const isVerbose: boolean = debugMode ? debugMode.toLowerCase() != 'false' : false;
const parallelLimit: number = +tl.getInput("parallelizationLimit", false);
const retryLimit = parseInt(tl.getVariable("VSTS_HTTP_RETRY")) ? parseInt(tl.getVariable("VSTS_HTTP_RETRY")) : 4;
const itemPattern: string = tl.getInput("itemPattern", false) || '**';
const downloader = new engine.ArtifactEngine();
const downloadUrl = tl.getInput("filesharePath", true);
let artifactName = tl.getInput("artifactName", true);
artifactName = artifactName.replace('/', '\\');
let artifactLocation = path.join(downloadUrl, artifactName);
console.log(tl.loc("DownloadArtifacts", artifactName, artifactLocation));
if (!fs.existsSync(artifactLocation)) {
console.log(tl.loc("ArtifactNameDirectoryNotFound", artifactLocation, downloadUrl));
artifactLocation = downloadUrl;
}
let downloaderOptions = new engine.ArtifactEngineOptions();
downloaderOptions.itemPattern = itemPattern;
downloaderOptions.verbose = isVerbose;
if (parallelLimit) {
downloaderOptions.parallelProcessingLimit = parallelLimit;
}
let fileShareProvider = new providers.FilesystemProvider(artifactLocation, artifactName);
let fileSystemProvider = new providers.FilesystemProvider(downloadPath);
let downloadPromise = downloader.processItems(fileShareProvider, fileSystemProvider, downloaderOptions);
downloadPromise.then(() => {
console.log(tl.loc('ArtifactsSuccessfullyDownloaded', downloadPath));
resolve();
}).catch((error) => {
reject(error);
});
});
return promise;
}
main()
.then((result) => tl.setResult(tl.TaskResult.Succeeded, ""))
.catch((err) => {
publishEvent('reliability', { issueType: 'error', errorMessage: JSON.stringify(err, Object.getOwnPropertyNames(err)) });
tl.setResult(tl.TaskResult.Failed, err);
});