forked from rezn/CodeCoverageDashboardWidgets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode-coverage2.html
More file actions
211 lines (192 loc) · 12.1 KB
/
Copy pathcode-coverage2.html
File metadata and controls
211 lines (192 loc) · 12.1 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
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
207
208
209
210
211
<!DOCTYPE html>
<html>
<head>
<script src="lib/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("CodeCoverageWidget2", 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();
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('projectId = ' + projectId);
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 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]) {
// 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;
$.each(coverageData, function(key, value) {
totalMeasurement += value.total;
coveredMeasurement += value.covered;
});
// 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));
}
}
// Fetch build name
var buildName = query3.build.name;
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");
}
$("#init-label").hide();
$("#build-name").text(buildName);
$("#build-link").prop("href", buildWeb);
$("#build-link").prop("target", "_top");
}
else {
// Set display values for no coverage data
$("#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
$("#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 to VSTS
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');
return getBuildBuildInfo(widgetSettings);
},
reload: function (widgetSettings) {
// Set title
$("h2.title").text(widgetSettings.name);
// Output diagnostics info to console
console.log('event: reload');
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>
<!-- Load c3.css -->
<link href="c3.css" rel="stylesheet" type="text/css">
<!-- Load d3.js and c3.js -->
<script src="d3.v3.min.js" charset="utf-8"></script>
<script src="c3.min.js"></script>
<script>
var generate = function () { return c3.generate({
data: {
columns: [
['Code Coverage', 0, .05, .10, .25, .50, .75, .80, .85, .90, .95]
],
type: 'bar'
},
bar: {
space: 0.1
},
axis: {
x: {
show: false
},
y: {
label: {
text: 'Percentage',
position: 'outer-middle'
},
tick: {
format: d3.format("%")
}
}
},
legend: {
show: false
}
}); }, chart = generate();
</script>
<a id="build-link" target="_blank">
<div id="build-name" class="subtitle"></div>
</a>
</div>
</body>
</html>