forked from microsoft/azure-devops-node-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.ts
124 lines (104 loc) · 4.06 KB
/
run.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// do first just to ensure variables set
// export API_URL=https://buildcanary.visualstudio.com/DefaultCollection
// export export API_TOKEN=<yourAllScopesApiToken>
// export API_PROJECT=test
import * as cm from "./common";
import * as nodeApi from "azure-devops-node-api";
import * as cApi from "azure-devops-node-api/CoreApi";
import * as coreInterfaces from "azure-devops-node-api/interfaces/CoreInterfaces"
let samples: string[] = require("./samples.json");
let coreApi: cApi.ICoreApi;
const maxLoops: number = 500;
let selection: string = process.argv[2];
if (selection) {
if (samples.indexOf(selection) == -1) {
console.error("Not a valid sample. See list of samples");
process.exit(1);
}
samples = [selection];
}
async function createProject(projectId: string): Promise<boolean> {
console.log("Cleaning up from last run");
if (!(await deleteProject(projectId))) {
console.log("Failed to delete previous project");
return false;
}
const projectToCreate: coreInterfaces.TeamProject = {name: projectId,
description: "sample project created automatically by azure-devops-node-api.",
visibility: coreInterfaces.ProjectVisibility.Private,
capabilities: {versioncontrol: {sourceControlType: "Git"},
processTemplate: {templateTypeId: "6b724908-ef14-45cf-84f8-768b5384da45"}},
_links: null,
defaultTeam: null,
abbreviation: null,
id: null,
revision: null,
state: null,
url: null};
await coreApi.queueCreateProject(projectToCreate);
//Poll until project exists
let project: coreInterfaces.TeamProject = null;
console.log("Waiting for project to spin up");
let numLoops = 0;
while (numLoops < maxLoops) {
project = await coreApi.getProject(projectId);
numLoops += 1;
if (project) {
return true;
}
}
return false;
}
async function deleteProject(projectId: string): Promise<boolean> {
let project: coreInterfaces.TeamProject = await coreApi.getProject(projectId);
if (!project) {
//If no project to clean up, just return
console.log("Nothing to clean up");
return true;
}
await coreApi.queueDeleteProject(project.id);
//Poll until project no longer exists
console.log("Waiting for project to be deleted");
let numLoops = 0;
while (project && numLoops < maxLoops) {
project = await coreApi.getProject(projectId);
numLoops += 1;
if (!project) {
return true;
}
}
return false;
}
async function runSamples(selected?: string) {
const webApi: nodeApi.WebApi = await cm.getWebApi();
coreApi = await webApi.getCoreApi();
const projectId: string = "azureDevopsNodeSampleProject";
cm.heading("Creating example project");
if (await createProject(projectId)) {
console.log("Project created");
}
else {
console.log("Failed to create project, exiting");
return;
}
for (let i: number = 0; i < samples.length; i++) {
let sample: string = samples[i];
if (selected && sample !== selected) {
continue;
}
cm.banner(`Sample ${sample}`);
var sm = require(`./${sample}.js`);
await sm.run(projectId);
}
cm.heading("Cleaning up project");
if (await deleteProject(projectId)) {
console.log("Done");
}
else {
console.log("Failed to delete project");
}
}
function run() {
runSamples();
}
runSamples(process.argv[2]);