forked from colindembovsky/cols-agent-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coverageGate.ts
81 lines (73 loc) · 3.4 KB
/
coverageGate.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
import * as tl from 'vsts-task-lib/task';
import * as vstsInterfaces from 'azure-devops-node-api/interfaces/common/VsoBaseInterfaces';
import * as webApi from 'azure-devops-node-api/WebApi';
async function run() {
try {
tl.debug("Starting Coverage Gate task");
var tpcUri = tl.getVariable("System.TeamFoundationCollectionUri");
var teamProject = tl.getVariable("System.TeamProject");
var buildId = parseInt(tl.getVariable("Build.BuildId"));
var minDelta = parseInt(tl.getInput("minDelta", true));
var operator = tl.getInput("operator", true);
var username = tl.getInput("username", false);
var password = tl.getInput("password", false);
tl.debug(`minDelta: ${minDelta}`);
tl.debug(`operator: ${operator}`);
tl.debug(`username: ${username}`);
// handle creds
var credOk = true;
var credHandler: vstsInterfaces.IRequestHandler;
var accessToken = tl.getVariable("System.AccessToken");
if (!accessToken || accessToken.length === 0) {
if (username && username.length > 0) {
tl.debug("Detected username: creating basic cred handler");
credHandler = webApi.getBasicHandler(username, password);
} else {
tl.setResult(tl.TaskResult.Failed, "Could not find token for autheniticating. Please enable OAuth token in Build/Release Options or supply username/password.");
credOk = false;
}
} else {
tl.debug("Detected token: creating bearer cred handler");
credHandler = webApi.getBearerHandler(accessToken);
}
if (credOk) {
var vsts = new webApi.WebApi(tpcUri, credHandler);
// get previous successful builds
tl.debug("Connecting to test API");
var testApi = await vsts.getTestApi();
tl.debug("Getting coverage data");
// 1 = modules, 2 = function, 4 = block
var covData = await testApi.getCodeCoverageSummary(teamProject, buildId);
tl.debug("Analyzing coverage data");
if (covData) {
var data = covData.coverageData[0];
var trends = data.coverageStats.filter(c => c.isDeltaAvailable)
.map(u => {
return {
Label: u.label,
Delta: u.delta
};
});
if (trends.length === 0){
tl.setResult(tl.TaskResult.Failed, `There are no coverage deltas. Make sure you have at least 2 builds.`);
}
var fail = trends.some(t => operator === 'lt' ? t.Delta < minDelta : t.Delta <= minDelta);
if (fail) {
tl.setResult(tl.TaskResult.Failed, `Coverage delta is below the threshold of ${minDelta}`);
}
} else {
tl.setResult(tl.TaskResult.Failed, "No coverage data for build. Cannot determine trend.");
}
tl.setResult(tl.TaskResult.Succeeded, `Code coverage delta is above the threshold of ${minDelta}`);
}
} catch (err) {
tl.debug("Caught an error - logging");
let msg = err;
if (err.message) {
msg = err.message;
}
tl.setResult(tl.TaskResult.Failed, msg);
}
tl.debug("Leaving Coverage Gate task");
}
run();