forked from sdavis3/code-coverage-widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
code-coverage1.html
206 lines (185 loc) · 13.1 KB
/
code-coverage1.html
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<!DOCTYPE html>
<html>
<head>
<script src="sdk/scripts/VSS.SDK.min.js"></script>
<script type="text/javascript">
// Initialize
VSS.init({
explicitNotifyLoaded: true,
usePlatformStyles: true,
usePlatformScripts: true
});
VSS.require(["TFS/Dashboards/WidgetHelpers", "TFS/Build/RestClient", "TFS/TestManagement/RestClient"],
function (WidgetHelpers, TFS_Build_WebApi, TFS_TestMgmt_WebApi) {
WidgetHelpers.IncludeWidgetStyles();
VSS.register("CodeCoverageWidget1", function () {
var projectId = VSS.getWebContext().project.id;
var getBuildBuildInfo = function (widgetSettings) {
var settings = JSON.parse(widgetSettings.customSettings.data);
if (!settings || !settings.buildDefinition) {
// Initialize display values
$("#init-label").show();
$("div.big-count").hide();
$("#percent-label").hide();
return WidgetHelpers.WidgetStatusHelper.Success()
}
if (settings && settings.checkOptionBuildName == true) {
// Show build name
$("#build-name").show();
}
else {
// Hide build name
$("#build-name").hide();
}
// Output diagnostics info to console
console.log('settings.buildDefinition: ' + settings.buildDefinition);
if (parseInt(settings.buildDefinition) > 0) {
// Get most recent successful build details
// .getBuilds(project, definitions, queues, buildNumber, minFinishTime, maxFinishTime, requestedFor, reasonFilter, statusFilter, resultFilter, tagFilters, properties, type, top, continuationToken, maxBuildsPerDefinition, deletedFilter, queryOrder, branchName)
TFS_Build_WebApi.getClient().getBuilds(projectId, [parseInt(settings.buildDefinition)], null, null, null, null, null, null, "Completed", "Failed,PartiallySucceeded,Succeeded", null, null, null, 1, null, null, null, null, null)
.then(function (query2) {
// Evaluate build details of first result
if (query2[0]) {
var buildId = query2[0].id;
var buildName = query2[0].buildNumber;
var buildWeb = query2[0]._links.web.href
// Output diagnostics info to console
console.log('buildId: ' + buildId);
// Get code coverage summary for most recent build
TFS_TestMgmt_WebApi.getClient().getCodeCoverageSummary(projectId, buildId)
.then(function (query3) {
// Evaluate for where there is no coverage data
if (query3.coverageData[0]) {
// Output diagnostics info to console
console.log('event: coverageData found');
// Filter for selected coverage measurement
var coverageMeasurement = "";
if (!settings || !settings.coverageMeasurement) {
// Default to Lines if not previously chosen
coverageMeasurement = "Lines";
}
else {
coverageMeasurement = settings.coverageMeasurement;
}
var coverageData = query3.coverageData[0].coverageStats.filter(function (el) {
return el.label == coverageMeasurement;
});
// Iterate and count code coverage items
var totalMeasurement = 0;
var coveredMeasurement = 0;
var coveragePct = 0;
var deltaMeasurement = 0;
$.each(coverageData, function(key, value) {
totalMeasurement += value.total;
coveredMeasurement += value.covered;
deltaMeasurement += value.delta;
// Output diagnostics info to console
console.log('event: delta found');
});
// Output diagnostics info to console
console.log('deltaMeasurement: ' + deltaMeasurement);
// Calculate code coverage percentage
// This check avoids divide by zero error
if (totalMeasurement > 0) {
coveragePct = ((coveredMeasurement / totalMeasurement) * 100);
// Determine and set the requested number of decimal places
if (!settings || !settings.decimalPlaces) {
// Default to zero if not previously chosen
coveragePct = coveragePct.toFixed(0);
}
else {
coveragePct = coveragePct.toFixed(parseInt(settings.decimalPlaces));
}
}
// Update UI with code coverage percentage
$("div.big-count").hide();
$("#percent-label").hide();
if (settings && settings.checkOptionMeasurementName == true) {
// Add the measurement name to the percent label
$("#percent-label").text("Percent (" + coverageMeasurement + ")");
}
else {
// Use the default percent label
$("#percent-label").text("Percent");
}
var percentLabelString = $("#percent-label").text();
// Evaluate for delta display
if (settings && settings.checkOptionDelta == true) {
// Add the delta to the percent label
$("#percent-label").text(percentLabelString + " (" + deltaMeasurement + "%)");
}
$("div.big-count").text(coveragePct);
$("div.big-count").show();
$("#percent-label").show();
$("#init-label").hide();
$("#build-name").text(buildName);
$("#build-link").prop("href", buildWeb);
$("#build-link").prop("target", "_top");
}
else {
// Output diagnostics info to console
console.log('event: no coverageData found');
// Set display values for no coverage data
$("div.big-count").hide();
$("#percent-label").hide();
$("#init-label").text("No code coverage details found for this build definition");
$("#init-label").show();
$("#build-name").text("");
$("#build-link").removeProp("href");
}
});
}
else {
// Set display values for build details
$("div.big-count").hide();
$("#percent-label").hide();
$("#init-label").text("No code coverage details found for this build definition");
$("#init-label").show();
$("#build-name").text("");
$("#build-link").removeProp("href");
};
});
}
// Get a client to make REST calls
return TFS_Build_WebApi.getClient().getDefinition(settings.buildDefinition, projectId)
.then(function (query) {
// Use the widget helper and return success as Widget Status
return WidgetHelpers.WidgetStatusHelper.Success();
}, function (error) {
// Use the widget helper and return failure as Widget Status
return WidgetHelpers.WidgetStatusHelper.Failure(error.message);
});
}
return {
load: function (widgetSettings) {
// Set title
$("h2.title").text(widgetSettings.name);
// Output diagnostics info to console
console.log('event: load called');
return getBuildBuildInfo(widgetSettings);
},
reload: function (widgetSettings) {
// Set title
$("h2.title").text(widgetSettings.name);
// Output diagnostics info to console
console.log('event: reload called');
return getBuildBuildInfo(widgetSettings);
}
}
});
VSS.notifyLoadSucceeded();
});
</script>
</head>
<body>
<div class="widget">
<h2 class="title"></h2>
<label id="init-label" hidden>No build definition selected</label>
<div class="big-count" hidden></div>
<label id="percent-label" hidden>Percent</label>
<a id="build-link" target="_blank">
<div id="build-name" class="subtitle"></div>
</a>
</div>
</body>
</html>