2018-12-13 17:37:10 +03:00
|
|
|
import * as cm from "./common";
|
|
|
|
import * as vm from "azure-devops-node-api";
|
2017-03-09 16:11:52 +03:00
|
|
|
|
2018-12-13 17:37:10 +03:00
|
|
|
import * as ba from "azure-devops-node-api/BuildApi";
|
|
|
|
import * as bi from "azure-devops-node-api/interfaces/BuildInterfaces";
|
2017-03-09 16:11:52 +03:00
|
|
|
|
|
|
|
export async function run() {
|
|
|
|
try
|
|
|
|
{
|
2017-04-06 05:19:47 +03:00
|
|
|
let vsts: vm.WebApi = await cm.getWebApi();
|
2018-02-12 21:13:37 +03:00
|
|
|
let vstsBuild: ba.IBuildApi = await vsts.getBuildApi();
|
2017-04-06 05:19:47 +03:00
|
|
|
|
2018-12-13 17:37:10 +03:00
|
|
|
cm.banner("Build Samples");
|
2017-03-09 16:11:52 +03:00
|
|
|
let project = cm.getProject();
|
2018-12-13 17:37:10 +03:00
|
|
|
console.log("project", project);
|
2017-03-09 16:11:52 +03:00
|
|
|
|
|
|
|
// list definitions
|
2018-12-13 17:37:10 +03:00
|
|
|
cm.heading(`Build Definitions for ${project}`);
|
2017-03-09 16:11:52 +03:00
|
|
|
let defs: bi.DefinitionReference[] = await vstsBuild.getDefinitions(project);
|
|
|
|
|
2018-12-13 17:37:10 +03:00
|
|
|
console.log(`You have ${defs.length} build definition(s)`);
|
2017-03-09 16:11:52 +03:00
|
|
|
|
|
|
|
// 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];
|
|
|
|
|
2019-05-02 15:00:01 +03:00
|
|
|
let def: bi.BuildDefinition = await vstsBuild.getDefinition(project, defRef.id);
|
2017-03-09 16:11:52 +03:00
|
|
|
lastDef = def;
|
|
|
|
let rep: bi.BuildRepository = def.repository;
|
|
|
|
|
2018-12-13 17:37:10 +03:00
|
|
|
console.log(`${defRef.name} (${defRef.id}) repo ${rep.type}`);
|
2017-03-09 16:11:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// get top 10 successfully completed builds since 2016
|
2018-12-13 17:37:10 +03:00
|
|
|
cm.heading(`top 10 successfully completed builds for ${project}project`);
|
2017-03-09 16:11:52 +03:00
|
|
|
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
|
|
|
|
);
|
|
|
|
|
2018-12-13 17:37:10 +03:00
|
|
|
console.log(`${builds.length} builds returned`);
|
2017-03-09 16:11:52 +03:00
|
|
|
builds.forEach((build: bi.Build) => {
|
2018-12-13 17:37:10 +03:00
|
|
|
console.log(build.buildNumber, bi.BuildResult[build.result], "on", build.finishTime.toDateString());
|
2017-03-09 16:11:52 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// new definition
|
2018-01-11 18:04:33 +03:00
|
|
|
if (lastDef && lastDef.process && lastDef.process.type === 1 /* Designer */) {
|
|
|
|
let process = lastDef.process as bi.DesignerProcess;
|
|
|
|
if (process.phases && process.phases.length > 0) {
|
|
|
|
let phase = process.phases[0];
|
2018-12-13 17:37:10 +03:00
|
|
|
cm.heading("creating a new definition");
|
2018-01-11 18:04:33 +03:00
|
|
|
let newDef: bi.BuildDefinition = <bi.BuildDefinition>{};
|
|
|
|
|
2018-12-13 17:37:10 +03:00
|
|
|
let newName = `api copy of ${lastDef.name}`;
|
2018-01-11 18:04:33 +03:00
|
|
|
console.log("name", newName);
|
|
|
|
newDef.name = newName;
|
|
|
|
|
|
|
|
console.log("repo", lastDef.repository.name);
|
|
|
|
newDef.repository = lastDef.repository;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2018-03-26 18:10:39 +03:00
|
|
|
console.log("process", lastDef.process);
|
|
|
|
newDef.process = lastDef.process;
|
|
|
|
|
|
|
|
console.log("creating");
|
2018-01-11 18:04:33 +03:00
|
|
|
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}`);
|
2017-04-20 20:19:48 +03:00
|
|
|
}
|
|
|
|
|
2018-01-11 18:04:33 +03:00
|
|
|
// delete def
|
|
|
|
console.log("deleting", createdDef.name);
|
2019-05-02 15:00:01 +03:00
|
|
|
await vstsBuild.deleteDefinition(project, createdDef.id);
|
2018-01-11 18:04:33 +03:00
|
|
|
console.log("deleted");
|
2017-04-20 20:19:48 +03:00
|
|
|
}
|
2017-03-09 16:11:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (err) {
|
2018-12-13 17:37:10 +03:00
|
|
|
console.error(`Error: ${err.stack}`);
|
2017-03-09 16:11:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|