Skip to content

[JENKINS-74074][JENKINS-74751] Extract inline JavaScript & remove eval call #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
<script type="text/javascript" src="${resURL}/plugin/test-results-analyzer/js/testresult.js"></script>
<script type="text/javascript" src="${resURL}/plugin/test-results-analyzer/js/chart-generator.js"></script>
<script type="text/javascript" src="${resURL}/plugin/test-results-analyzer/js/test-result-analyzer-template.js"></script>
<script>

var $j = jQuery.noConflict();
</script>
<style type="text/css">
.failed {
background-color: ${it.failedColor};
Expand Down Expand Up @@ -81,7 +78,7 @@ var $j = jQuery.noConflict();
</div>
</div>
<button id="downloadCSV" >Download Test (CSV)</button>
Search: <input id="filter" class="table-filter" type="text" placeholder="Test/Class/Package" onkeyup="searchTests()"/>
Search: <input id="filter" class="table-filter" type="text" placeholder="Test/Class/Package"/>

<div class="extrabuttons">
<button id="expandall">Expand All</button>
Expand Down Expand Up @@ -124,106 +121,9 @@ Search: <input id="filter" class="table-filter" type="text" placeholder="Test/Cl
<div id="builddetail">

</div>

<script>
var runtimeLowThreshold = "${it.runTimeLowThreshold}";
var runtimeHighThreshold = "${it.runTimeHighThreshold}";
var customStatuses = {
'PASSED':'PASSED',
'SKIPPED':'SKIPPED',
'FAILED':'FAILED',
'N/A':'N/A'
}
function generateCharts() {
var chartType = {
type: jQuery("#chartDataType").val(),
line: jQuery('#linegraph').is(':checked'),
bar: jQuery('#bargraph').is(':checked'),
pie: jQuery('#piegraph').is(':checked')
}
generateChart(chartType);

//fixes Jenkins issue where page content is not correctly placed until the window is resized
window.dispatchEvent(new Event('resize'));
}

jQuery(document).ready(function () {
jQuery("#allnoofbuilds")[0].checked = ${it.showAllBuilds};
jQuery("#show-build-durations")[0].checked = ${it.showBuildTime};
jQuery("#hide-config-methods")[0].checked = ${it.hideConfigurationMethods};
jQuery("#linegraph")[0].checked = ${it.showLineGraph};
jQuery("#bargraph")[0].checked = ${it.showBarGraph};
jQuery("#piegraph")[0].checked = ${it.showPieGraph};
jQuery("#noofbuilds").attr('disabled', ${it.showAllBuilds});

if ("${it.chartDataType}" === "runtime") {
jQuery("#chartDataType").val("runtime");
jQuery("#bargraph").attr('disabled', true);
} else {
jQuery("#chartDataType").val("passfail");
}
setCustomStatuses();
populateTemplate();
});

jQuery("#settingsmenubutton").click(function () {
jQuery("#settingsmenu").slideToggle(400, function () {
//fixes Jenkins issue where page content is not correctly placed until the window is resized
window.dispatchEvent(new Event('resize'));
});

//fixes Jenkins issue where page content is not correctly placed until the window is resized
window.dispatchEvent(new Event('resize'));
});

jQuery("#allnoofbuilds").change(function () {
jQuery("#noofbuilds").attr('disabled', this.checked);
});

jQuery("#chartDataType").change(function (e) {
jQuery("#bargraph").attr('disabled', e.target.value == "runtime");
});

jQuery("#downloadCSV").click(function () {
var noOfBuilds = "-1";

if (!jQuery("#allnoofbuilds").is(":checked")) {
noOfBuilds = jQuery("#noofbuilds").val();
}
remoteAction.getExportCSV(displayValues, noOfBuilds, function(t) {
download("Test Results.csv", t.responseObject());
})
});

jQuery("#getbuildreport").click(function () {
populateTemplate();
});

jQuery("#expandall").click(function () {
expandAll();
});

jQuery("#collapseall").click(function () {
collapseAll();
});

function setCustomStatuses(){
customStatuses['PASSED'] = "${it.passedRepresentation}";
customStatuses['SKIPPED'] = "${it.skippedRepresentation}";
customStatuses['FAILED'] = "${it.failedRepresentation}";
customStatuses['N/A'] = "${it.naRepresentation}";
}

function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
</script>
<span class="tre-chart-data" data-run-time-low-threshold="${it.runTimeLowThreshold}" data-run-time-high-threshold="${it.runTimeHighThreshold}"
data-show-all-builds="${it.showAllBuilds}" data-show-build-time="${it.showBuildTime}" data-hide-configuration-methods="${it.hideConfigurationMethods}"
data-show-line-graph="${it.showLineGraph}" data-show-bar-graph="${it.showBarGraph}" data-show-pie-graph="${it.showPieGraph}"/>
</l:main-panel>
</l:layout>
</j:jelly>
4 changes: 2 additions & 2 deletions src/main/webapp/js/chart-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function generateRuntimeLineChart() {
if (chartResult.hasOwnProperty(key)) {
var buildResult = chartResult[key];
chartCategories.push(key);
chartData["Runtime"].push(Math.round(eval(buildResult["Runtime"]) * 1000) / 1000);
chartData["Runtime"].push(Math.round(buildResult["Runtime"] * 1000) / 1000);
}
}
$j(function () {
Expand All @@ -107,7 +107,7 @@ function generateRuntimePieChart(inputData) {
var lowThreshold = parseFloat(runtimeLowThreshold);
var highThreshold = parseFloat(runtimeHighThreshold);

runtimeArray.each(function (time) {
runtimeArray.forEach(function (time) {
if (time < lowThreshold) {
fast++;
} else if (time >= highThreshold) {
Expand Down
157 changes: 133 additions & 24 deletions src/main/webapp/js/testresult.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
var $j = jQuery.noConflict();

var colTemplate = "{'cellClass':'col1','value':'build20','header':'20','title':'20'}";
var reevaluateChartData = true;
var displayValues = false;
var runtimeLowThreshold = "${it.runTimeLowThreshold}";
var runtimeHighThreshold = "${it.runTimeHighThreshold}";
var customStatuses = {
'PASSED':'PASSED',
'SKIPPED':'SKIPPED',
'FAILED':'FAILED',
'N/A':'N/A'
}

function clearedFilter(rows) {
var levelsToShow = [0]; // stack to keep track of hierarchy
Expand Down Expand Up @@ -298,36 +308,135 @@ function resetAdvancedOptions(){
}

function getWorstTests(itemsResponse, range = 10) {
worstTests = {}
findChildren(itemsResponse);
worstTests = $j.map(worstTests, function(v,k) { return {k, v}});
worstTests.sort(function (a,b) { return compareInteger(b.v.length, a.v.length)});
return worstTests.slice(0, range);
worstTests = {}
findChildren(itemsResponse);
worstTests = $j.map(worstTests, function(v,k) { return {k, v}});
worstTests.sort(function (a,b) { return compareInteger(b.v.length, a.v.length)});
return worstTests.slice(0, range);
}

function compareInteger(integer1, integer2) {
if (parseInt(integer1) > parseInt(integer2)) return 1;
else if (parseInt(integer1) < parseInt(integer2)) return -1;
else return 0;
if (parseInt(integer1) > parseInt(integer2)) return 1;
else if (parseInt(integer1) < parseInt(integer2)) return -1;
else return 0;
}

function findChildren(hash, path = '') {
if ( hash.text != undefined ) { path += (path == '') ? hash.text : '.' + hash.text }
$j.each(hash, function( index, value ) {
if ( index == 'children' && value.length > 0 ) {
findChildren(value, path);
} else if ( index == 'buildResults' ) {
$j.each(value, function(index1, buildResult) {
// if totalTests is equal to 1 then it should be at the lowest level of the itemsResponse hash
if ((buildResult.status == 'FAILED') && (buildResult.totalTests == '1')) {
if ( worstTests[path] === undefined ) { worstTests[path] = [] }
worstTests[path].push({buildNumber: buildResult.buildNumber, buildUrl: buildResult.url});
if ( hash.text != undefined ) { path += (path == '') ? hash.text : '.' + hash.text }
$j.each(hash, function( index, value ) {
if ( index == 'children' && value.length > 0 ) {
findChildren(value, path);
} else if ( index == 'buildResults' ) {
$j.each(value, function(index1, buildResult) {
// if totalTests is equal to 1 then it should be at the lowest level of the itemsResponse hash
if ((buildResult.status == 'FAILED') && (buildResult.totalTests == '1')) {
if ( worstTests[path] === undefined ) { worstTests[path] = [] }
worstTests[path].push({buildNumber: buildResult.buildNumber, buildUrl: buildResult.url});
}
});
} else if ( $j.type(value) == 'object' ) {
findChildren(value, path);
} else if ( $j.isArray(value) ) {
findChildren(value, path);
}
});
} else if ( $j.type(value) == 'object' ) {
findChildren(value, path);
} else if ( $j.isArray(value) ) {
findChildren(value, path);
});
}

function generateCharts() {
var chartType = {
type: jQuery("#chartDataType").val(),
line: jQuery('#linegraph').is(':checked'),
bar: jQuery('#bargraph').is(':checked'),
pie: jQuery('#piegraph').is(':checked')
}
});
generateChart(chartType);

//fixes Jenkins issue where page content is not correctly placed until the window is resized
window.dispatchEvent(new Event('resize'));
}

function setCustomStatuses(){
customStatuses['PASSED'] = "${it.passedRepresentation}";
customStatuses['SKIPPED'] = "${it.skippedRepresentation}";
customStatuses['FAILED'] = "${it.failedRepresentation}";
customStatuses['N/A'] = "${it.naRepresentation}";
}

function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}

document.addEventListener("DOMContentLoaded", () => {
document.querySelector("#filter").addEventListener("keyup", searchTests);
const chartData = document.querySelector(".tre-chart-data").dataset;
const showAllBuilds = chartData.showAllBuilds === "true";
const showBuildTime = chartData.showBuildTime === "true";
const hideConfigurationMethods = chartData.hideConfigurationMethods === "true";
const showLineGraph = chartData.showLineGraph === "true";
const showBarGraph = chartData.showBarGraph === "true";
const showPieGraph = chartData.showPieGraph === "true";

jQuery("#allnoofbuilds")[0].checked = showAllBuilds;
jQuery("#show-build-durations")[0].checked = showBuildTime;
jQuery("#hide-config-methods")[0].checked = hideConfigurationMethods;
jQuery("#linegraph")[0].checked = showLineGraph;
jQuery("#bargraph")[0].checked = showBarGraph;
jQuery("#piegraph")[0].checked = showPieGraph;
jQuery("#noofbuilds").attr('disabled', showAllBuilds);

if ("${it.chartDataType}" === "runtime") {
jQuery("#chartDataType").val("runtime");
jQuery("#bargraph").attr('disabled', true);
} else {
jQuery("#chartDataType").val("passfail");
}
setCustomStatuses();
populateTemplate();

jQuery("#settingsmenubutton").click(function () {
jQuery("#settingsmenu").slideToggle(400, function () {
//fixes Jenkins issue where page content is not correctly placed until the window is resized
window.dispatchEvent(new Event('resize'));
});

//fixes Jenkins issue where page content is not correctly placed until the window is resized
window.dispatchEvent(new Event('resize'));
});

jQuery("#allnoofbuilds").change(function () {
jQuery("#noofbuilds").attr('disabled', this.checked);
});

jQuery("#chartDataType").change(function (e) {
jQuery("#bargraph").attr('disabled', e.target.value == "runtime");
});

jQuery("#downloadCSV").click(function () {
var noOfBuilds = "-1";

if (!jQuery("#allnoofbuilds").is(":checked")) {
noOfBuilds = jQuery("#noofbuilds").val();
}
remoteAction.getExportCSV(displayValues, noOfBuilds, function(t) {
download("Test Results.csv", t.responseObject());
})
});

jQuery("#getbuildreport").click(function () {
populateTemplate();
});

jQuery("#expandall").click(function () {
expandAll();
});

jQuery("#collapseall").click(function () {
collapseAll();
});
});