Create your own Code Coverage report with Sonar API

       We already have a CI job to publish unit test results to Sonar, but Sonar's interface, you know. We want to focus only on CodeCoverage data for relevant projects in our department, as well as modules for specific substandard projects.

       Check out Sonar's API documentation, http://nemo.sonarqube.org/api_documentation. api/resources are available.

example:

请求:http://sonar.vip.qa.ebay.com/api/resources?resource=Scala&depth=0&metrics=coverage,test_success_density,it_coverage,total-quality&format=json&callback=jQuery19108647494609467685_1419918769346&_=1419918769347

回复:jQuery19108647494609467685_1419918769346([{"id":312828,"key":"Scala","name":"Scala","scope":"PRJ","qualifier":"SVW","date":"2014-12-29T20:31:18-0700","creationDate":"2014-07-25T12:06:48-0700","lname":"Scala","lang":"none","msr":[{"key":"coverage","val":76.0,"frmt_val":"76.0%"},{"key":"total-quality","val":86.6,"frmt_val":"86.6%"}]}]);

 

    Through this request, we get the corresponding coverage and other data.

 

     We only need to use javascript to use jQuery's Ajax call, analyze the returned data, and then display it in the form of a table.

     

 

        The specific code is very simple:

 

<!DOCTYPE html>

<html>
<head>
	<script src="../javascripts/jquery.js" type="text/javascript"></script>

	<link href="http://ir.qa.ebaystatic.com/z/mk/q0km4qa2bizklor5ltdsqgthq.css?dataUri=true" type="text/css" rel="stylesheet"></head>
<body bgcolor="transparent">
</head>


<body>
<style>
  .table th {text-align:center}
</style>
<div class="span14">
	<div id="divother"></div>
</div>

<script>
	
	
	$(document).ready(function(){
		//alert("hi");
		
		populateQualityConfidenceTable();
	});
	
	function sortByProperty(property) {
		//'use strict';
		return function (a, b) {
			var sortStatus = 0;
			if (a[property] < b[property]) {
				sortStatus = -1;
			} else if (a[property] > b[property]) {
				sortStatus = 1;
			}

			return sortStatus;
		};
	}	
	
	function printTableHeader(val){
		var tableheader = "";
		
		if(val.msr[0]!=undefined && val.msr[1]!=undefined){
			
														if((val.lname.replace(/ /g,"") == "Scala") ) {
																tableheader = '<tr class="table" style="text-align:center"><th colspan="1"><font color="blue"><a id="test" href="#" onclick="openPage(\'http://sonar.vip.qa.ebay.com/dashboard/index/' + val.key + '\');return false;">' + val.lname.replace(/ /g,"") + '</a></font></th><th></th><th colspan="1">' + val.msr[0].frmt_val + '</th><th colspan="1">0.00</th><th colspan="1">0.00</th><th colspan="1">' + val.msr[1].frmt_val + '</th></tr>';
																tableheader = tableheader + '<tr><td colspan="7" align="center"><b>' + val.lname.replace(/ /g,"") + ' Sub Components</b></td></tr>';
																tableheader = tableheader + '<tr><th>Product</th><th colspan="1">Module</th><th>UT Coverage</th><th>Pass</th><th>FT Coverage</th><th><a id="qc" href="#" onclick="openPage(\'http://docs.codehaus.org/display/SONAR/Total+Quality+Plugin\');return false;">Quality Confidence</a></th></tr>';
															}else{
																if(val.msr[1].frmt_val.replace(/%/,"")>=85){
																	tableheader = tableheader + '<tr bgcolor="#dedede"><td><a id="test" href="#" onclick="openPage(\'http://sonar.vip.qa.ebay.com/dashboard/index/' + val.key + '\');return false;">' + val.lname.replace(/ /g,"") + '</a></td><td></td><td>' + val.msr[1].frmt_val + '</td><td>0.00</td><td>0.00</td><td >' + val.msr[0].frmt_val + '</td></tr>';
																}else{
																	tableheader = tableheader + '<tr bgcolor="#dedede"><td><a id="test" href="#" onclick="openPage(\'http://sonar.vip.qa.ebay.com/dashboard/index/' + val.key + '\');return false;">' + val.lname.replace(/ /g,"") + '</a></td><td></td><td bgcolor="red">' + val.msr[1].frmt_val + '</td><td>0.00</td><td>0.00</td><td >' + val.msr[0].frmt_val + '</td></tr>';
																}
															}
														}
		return tableheader;
	}
		
	function populateQualityConfidenceTable(){
		sonar_baseurl_head = "http://sonar.vip.qa.ebay.com/api/resources?resource=";
		var sonar_baseurl_tail_withzerodepth = "&depth=0&metrics=coverage,test_success_density,it_coverage,total-quality&format=json";
		var sonar_baseurl_tail_withonedepth = "&depth=1&metrics=scoverage,test_success_density,it_coverage,total-quality&format=json";

		var metric_scala_coverage = sonar_baseurl_head + "Scala" + sonar_baseurl_tail_withzerodepth;
		var metric_sub_polyglot_coverage = sonar_baseurl_head + "Scala" + sonar_baseurl_tail_withonedepth;
		var requestUrl = "";
		var modules = 0;
		//alert(metric_scala_coverage);
		var table = '<table style="width:100%">';
			$.ajax({
					url : metric_scala_coverage + "&callback=?",
					async : false,
					dataType: 'json',
					success:
						function(polyjson)
						{								
							$.ajax({
								url : metric_sub_polyglot_coverage + "&callback=?",
								async : false,
								dataType: 'json',
								success:
									function(subpolyjson)
									{
										polyData = polyjson.concat(subpolyjson.sort(sortByProperty('lname')));
										var dataArray = [polyData];
										table = table + '<tr><th colspan="1">Product</th><th colspan="1">Module</th><th colspan="1">UT Coverage</th><th colspan="1">Pass</th><th colspan="1">FT Coverage</th><th colspan="1"><a id="qc" href="#" onclick="openPage(\'http://docs.codehaus.org/display/SONAR/Total+Quality+Plugin\');return false;">Quality Confidence</a></th></tr>';
										
										for (var j = 0; j < dataArray.length; j++) {
											$.each(dataArray[j], function(i, val) {
												if(val.lname.replace(/ /g,"") == "Scala"){
													table = table + printTableHeader(val);
												}
												else{
													if(val.lname.replace(/ /g,"") == "higgins"){
														requestUrl = sonar_baseurl_head + "com.ebay.higgins" + sonar_baseurl_tail_withonedepth;
														}else if(val.lname.replace(/ /g,"") == "squbs"){
															requestUrl = sonar_baseurl_head + "com.ebay.squbs" + sonar_baseurl_tail_withonedepth;
														}else if(val.lname.replace(/ /g,"") == "rocksqubs"){
															requestUrl = sonar_baseurl_head + "com.ebay.rocksqubs" + sonar_baseurl_tail_withonedepth;
														}else{
															requestUrl = sonar_baseurl_head + "com.ebay.squbs.chnlsvc" + sonar_baseurl_tail_withonedepth;
														}
																	
													$.ajax({
														url : requestUrl + "&callback=?",
														async : false,
														dataType: 'json',
														success:
															function(json)
															{
																
																scalaData = json//.sort(sortByProperty('lname'));
																var dataArray = [scaleData];
																var subtable = ""
																for (var k = 0; k < dataArray.length; k++) {
																	$.each(dataArray[k], function(l, val2) {
																	if(parseFloat(val2.msr[1].frmt_val.replace(/%/,""))>=85){
																		subtable = subtable + '<tr id="'+val2.lname.replace(/ /g,"")+'"><td></td><td><a id="test" href="#" onclick="openPage(\'http://sonar.vip.qa.ebay.com/dashboard/index/' + val2.key + '\');return false;">' + val2.lname.replace(/ /g,"") + '</a></td><td>' + val2.msr[1].frmt_val + '</td><td>0.00</td><td>0.00</td><td >' + val2.msr[0].frmt_val + '</td></tr>';
																	}else{
																		subtable = subtable + '<tr bgcolor="yellow" id="'+val2.lname.replace(/ /g,"")+'"><td></td><td><a id="test" href="#" onclick="openPage(\'http://sonar.vip.qa.ebay.com/dashboard/index/' + val2.key + '\');return false;">' + val2.lname.replace(/ /g,"") + '</a></td><td >' + val2.msr[1].frmt_val + '</td><td>0.00</td><td>0.00</td><td >' + val2.msr[0].frmt_val + '</td></tr>';
																	
																	}
																	});
																	
																}
													var tableheader = printTableHeader (option);
															
															table = table + tableheader + subtable;
																
																
																modules = modules + 1;
																//alert(modules);
																if(modules==4){
																	//alert("final");	
																	table = table + '</table>';
																	var qc = $('<div  class="coverageTable"><div id="coverageTable" style="border-color: #4271A5" align="center">'
																			+'<div align="center" id="tabletarget" style="width:99%;overflow: auto;">'
																			+ table
																			+ '</div></div></div>');
																	$('#divother').append(qc);
																}
															}
													});
												}
											});
									}
								
								
								}
							});
						}
					});
					
					
	}
	
	

</script>
</body>

</html>

    It should be noted here that the patched part of the Table must be placed in the innermost ajax callback function, otherwise the order cannot be guaranteed, which will cause some confusion in the table.

 

     Since our project is a Scala project, Scoverage is used for unit test, so in the request, coverage is replaced with coverage

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326441720&siteId=291194637