forked from microsoft/azure-devops-node-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.ts
66 lines (55 loc) · 2.39 KB
/
task.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
import * as fs from "fs";
import * as path from "path";
import * as stream from "stream";
import * as sh from "shelljs";
import * as cm from "./common";
import * as vm from "azure-devops-node-api";
import * as ta from "azure-devops-node-api/TaskAgentApi";
import * as ti from "azure-devops-node-api/interfaces/TaskAgentInterfaces";
let sampleFilePath: string = path.join(process.cwd(), "taskdefinition.zip");
export async function run() {
try
{
let vsts: vm.WebApi = await cm.getWebApi();
let vstsTask: ta.ITaskAgentApi = await vsts.getTaskAgentApi();
cm.banner("Task Samples");
let project = cm.getProject();
console.log("project", project);
// list tasks
cm.heading("Task Definitions");
let tasks: ti.TaskDefinition[] = await vstsTask.getTaskDefinitions();
console.log(`You have ${tasks.length} task definition(s)`);
// download a task
if (tasks.length > 0) {
let taskDefinition = tasks[0];
let file: NodeJS.WritableStream = fs.createWriteStream(sampleFilePath);
let stream = (await vstsTask.getTaskContentZip(taskDefinition.id, `${taskDefinition.version.major}.${taskDefinition.version.minor}.${taskDefinition.version.patch}`)).pipe(file);
let promise = new Promise((resolve, reject) => {
stream.on("finish", () => {
resolve();
});
});
await promise;
console.log(`Downloaded task ${taskDefinition.name}`);
}
// upload a secure file
let s = new stream.Readable();
let testString: string = 'test file contents';
s._read = function noop() {};
s.push(testString);
s.push(null);
let name = `vstsnodeapitest${new Date().getTime()}`;
console.log("uploading file");
const headers = { 'Content-Length': testString.length };
let secureFile = await vstsTask.uploadSecureFile(headers, s, project, name);
console.log(`uploaded secure file ${secureFile.name}`);
// delete it
await vstsTask.deleteSecureFile(project, secureFile.id);
console.log(`deleted secure file ${secureFile.name}`);
// delete generated file
sh.rm(sampleFilePath);
}
catch (err) {
console.error(`Error: ${err.stack}`);
}
}