forked from microsoft/azure-devops-node-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.ts
More file actions
128 lines (104 loc) · 5.13 KB
/
Copy pathbuild.ts
File metadata and controls
128 lines (104 loc) · 5.13 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
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
122
123
124
125
126
127
128
import * as cm from './common';
import * as vm from 'vso-node-api';
import * as ba from 'vso-node-api/BuildApi';
import * as bi from 'vso-node-api/interfaces/BuildInterfaces';
export async function run() {
try
{
let vsts: vm.WebApi = await cm.getWebApi();
let vstsBuild: ba.IBuildApi = vsts.getBuildApi();
cm.banner('Build Samples');
let project = cm.getProject();
console.log('project', project);
// list definitions
cm.heading('Build Definitions for ' + project);
let defs: bi.DefinitionReference[] = await vstsBuild.getDefinitions(project);
console.log('You have ' + defs.length + ' build definition(s)');
// save off last def to create a new definition below
let lastDef: bi.BuildDefinition;
for (let i: number = 0; i < defs.length; i++) {
let defRef: bi.DefinitionReference = defs[i];
let def: bi.BuildDefinition = await vstsBuild.getDefinition(defRef.id, project);
lastDef = def;
let rep: bi.BuildRepository = def.repository;
console.log(defRef.name + ' (' + defRef.id + ') ' + 'repo ' + rep.type);
}
// get top 10 successfully completed builds since 2016
cm.heading('top 10 successfully completed builds for ' + project + 'project');
let builds: bi.Build[] = await vstsBuild.getBuilds(
project,
null, // definitions: number[]
null, // queues: number[]
null, // buildNumber
null, //new Date(2016, 1, 1), // minFinishTime
null, // maxFinishTime
null, // requestedFor: string
bi.BuildReason.All, // reason
bi.BuildStatus.Completed,
bi.BuildResult.Succeeded,
null, // tagFilters: string[]
null, // properties: string[]
//bi.DefinitionType.Build,
10 // top: number
);
console.log(builds.length + ' builds returned');
builds.forEach((build: bi.Build) => {
console.log(build.buildNumber, bi.BuildResult[build.result], 'on', build.finishTime.toDateString());
});
// new definition
if (lastDef && lastDef.build) {
cm.heading('creating a new definition');
let newDef: bi.BuildDefinition = <bi.BuildDefinition>{};
let newName: string = "api copy of " + lastDef.name;
console.log("name", newName);
newDef.name = newName;
console.log("repo", lastDef.repository.name);
newDef.repository = lastDef.repository;
let steps: bi.BuildDefinitionStep[] = [];
lastDef.build.forEach((step: bi.BuildDefinitionStep) => {
console.log("adding step", step.displayName);
steps.push(step);
});
newDef.build = steps;
newDef.comment = "copy of definition from sample";
newDef.buildNumberFormat = lastDef.buildNumberFormat;
console.log("project", project);
newDef.project = lastDef.project;
console.log("queue", lastDef.queue.name);
newDef.queue = lastDef.queue;
console.log("type", lastDef.type);
newDef.type = lastDef.type;
console.log("creating");
let createdDef: bi.BuildDefinition = await vstsBuild.createDefinition(newDef, project);
console.log("created", createdDef.name);
console.log("reading history");
let history = await vstsBuild.getDefinitionRevisions(project, createdDef.id);
console.log(`last updated ${history[0].changedDate}`);
let document = [
{
op: "replace",
path: "/key1",
value: "/value1"
},
{
op: "replace",
path: "/key2",
value: "/value2"
}
];
console.log("setting properties");
let updatedProperties = await vstsBuild.updateDefinitionProperties(null, document, project, createdDef.id);
console.log(`properties for definition ${createdDef.name}:`);
for (let key in updatedProperties.value) {
console.log(`${key} = ${updatedProperties.value[key].$value}`);
}
// delete def
console.log("deleting", createdDef.name);
await vstsBuild.deleteDefinition(createdDef.id, project);
console.log("deleted");
}
}
catch (err) {
console.error('Error: ' + err.stack);
}
}