此类型的报告,我写的有几个了,但一直都不太满意,这次因为看见了网上的一篇样式,就又重新写了一版。
不算抄袭,因为语言都不同
效果图:
用到的查询:
package com.pconlineqa.sonarapi.dao;
import com.pconlineqa.sonarapi.entity.Issues;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface ReportDao extends JpaRepository<Issues,String> {
/**
* 查询坏味道一共有多少个
* @param project_uuid
* @return
*/
@Query(name="findbadtastescount",nativeQuery = true,value = "select count(*) as bad_tastes_count from issues where project_uuid=?1 and issue_type=1" +
"and severity in ('BLOCKER','CRITICAL','MAJOR','MINOR','INFO') ")
long findbadtastescount(@Param("project_uuid") String project_uuid);
/**
* 查询bugs一共有多少个
* @param project_uuid
* @return
*/
@Query(name="findbugscount",nativeQuery = true,value = "select count(*) as bugs_count from issues where project_uuid=?1 and issue_type=2" +
"and severity in ('BLOCKER','CRITICAL','MAJOR','MINOR','INFO') ")
long findbugscount(@Param("project_uuid") String project_uuid);
/**
* 查询漏洞一共有多少个
* @param project_uuid
* @return
*/
@Query(name="findVulnerabilitiescount",nativeQuery = true,value = "select count(*) as vulnerabilities_count from issues where project_uuid=?1 and issue_type=3" +
"and severity in ('BLOCKER','CRITICAL','MAJOR','MINOR','INFO') ")
long findVulnerabilitiescount(@Param("project_uuid") String project_uuid);
/**
* 通过kee查uuid
*/
@Query(name="findprojectuuid",nativeQuery = true,value = "select uuid from projects where kee=?1")
String findprojectuuid(@Param("kee") String kee);
/**
* 查询bugs类型一共有多少个
* @param project_uuid
* @return
* select severity,count(*) from issues where project_uuid='AX3rNg5MIsXtnTLt4mM5' and issue_type=1 group by severity
*
* select severity,count(*) from issues where project_uuid='AX3rNg5MIsXtnTLt4mM5' and issue_type=2 group by severity
*
* select severity,count(*) from issues where project_uuid='AX3rNg5MIsXtnTLt4mM5' and issue_type=3 group by severity
*/
@Query(name="findbugsInfo",nativeQuery = true,value = "select severity,count(*) as num from issues where project_uuid=?1 and issue_type=2 group by severity ")
List<Object[]> findbugsInfo(@Param("project_uuid") String project_uuid);
/*查询坏味道类型一共有多少个BUG
*/
@Query(name="findbadtastesInfo",nativeQuery = true,value = "select severity,count(*) as num from issues where project_uuid=?1 and issue_type=1 group by severity ")
List<Object[]> findbadtastesInfo(@Param("project_uuid") String project_uuid);
/*查询漏洞类型一共有多少个BUG
*/
@Query(name="findVulnerabilitiesInfo",nativeQuery = true,value = "select severity,count(*) as num from issues where project_uuid=?1 and issue_type=3 group by severity ")
List<Object[]> findVulnerabilitiesInfo(@Param("project_uuid") String project_uuid);
}
请求处理
package com.pconlineqa.sonarapi.controller;
import com.alibaba.fastjson.JSONObject;
import com.pconlineqa.sonarapi.service.impl.ReportServicesImpl;
import com.pconlineqa.sonarapi.util.SendEmal;
import com.pconlineqa.sonarapi.util.SendRuquest;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.mail.Message;
import java.util.*;
@RestController
public class ReportController {
@Resource
ReportServicesImpl services;
@GetMapping("/report/{projectKey}/{email}")
public String report(@PathVariable("projectKey") String projectKey,@PathVariable("email") String email ){
StringBuffer content=new StringBuffer();
SendRuquest sendRuquest=new SendRuquest();
content.append("<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
" <meta charset=\"GBK\">\n" +
"<body>\n" +
"<p style=\"font-weight:bold;\">一、扫描状态:");
/**
* 获取项目状态
* 获取项目状态
* http://sonar.pc.com.cn/api/qualitygates/project_status?projectKey=pcdlc
*/
String projectresult1= sendRuquest.gitSendGet("http://sonar.pc.com.cn/api/qualitygates/project_status?projectKey="+projectKey);
JSONObject projectjsont=JSONObject.parseObject(projectresult1);
String status= (String) projectjsont.getJSONObject("projectStatus").get("status");
System.out.println("扫描状态:"+status);
if(status.equals("ERROR")){
content.append("<span style=\"color: brown\">"+"扫描不通过"+"</span></p>");
}
if(status.equals("OK")){
content.append("<span style=\"color: green\">"+"扫描通过"+"</span></p>");
}
/*
获取代码行数、覆盖度等
http://sonar.pc.com.cn/api/components/app?component=pcgroup:sonar_mblog
*/
String linesresult= sendRuquest.gitSendGet("http://sonar.pc.com.cn/api/components/app?component="+projectKey);
JSONObject linesjson=JSONObject.parseObject(linesresult);
System.out.println("linesjson.getJSONObject(\"measures\").get(\"lines\")"+linesjson.getJSONObject("measures").getString("lines"));
long lines = (long)Float.parseFloat(linesjson.getJSONObject("measures").getString("lines"));
System.out.println("共扫描代码"+lines+"行");
/**
* 得出bugs\漏洞数、异味数
*/
String project_uuid= services.findprojectuuid(projectKey);
System.out.println("project_uuid=="+project_uuid);
long bugscount= services.findbugscount(project_uuid);//得出bug数量
System.out.println("bugscount"+bugscount);
long badtastescount= services.findbadtastescount(project_uuid);//得出坏吃法道数量
System.out.println("badtastescount"+badtastescount);
long vulnerabilitiescount= services.findVulnerabilitiescount(project_uuid);//得出漏洞数量
System.out.println("vulnerabilitiescount"+vulnerabilitiescount);
String report_url="https://sonar.pc.com.cn/dashboard?id="+projectKey;
content.append("<p style=\"font-weight:bold;\">二、总体情况:</p>\n" +
"<ul>\n" +
" <li style=\"font-weight:bold;\">整体运行情况:扫描代码行数:<span style=\"color:blue\">"+lines+"</span>, bugs:<span style=\"color:red\">"+bugscount+"</span>," +
" 漏洞:<span style=\"color:red\">"+vulnerabilitiescount+"</span>, 坏味道:<span style=\"color:red\">"+badtastescount+"</span></li>\n" +
" <li style=\"font-weight:bold;\">URL地址:<a style=\"font-weight:bold;\" href=\""+report_url+"\">"+report_url+"</a></li>\n" +
"</ul>");
//第三段
content.append("<p style=\"font-weight:bold;\">三、错误信息详情:</p>\n" +
"<table border=\"1\" cellpadding=\"10\" width=\"540\" height=\"120\">\n" +
" <tr ><th></th><th>阻断</th><th>严重</th><th>主要</th><th>次要</th><th>提示</th><th>总数</th></tr>");
//bugs内容处理
List<Object[]> bugslist=services.findbugsInfo(project_uuid);
long bugs_MAJOR=0;//主要的
long bugs_MINOR=0;//次要的
long bugs_CRITICAL=0;//严重的
long bugs_INFO=0;//提示
long bugs_BLOCKER=0;//阻断
if(bugslist.size()>0){
for (Object[] objs : bugslist) {
String severity = objs[0].toString();
if(severity.equals("BLOCKER")){
bugs_BLOCKER=Long.parseLong(objs[1].toString());;
}
if(severity.equals("CRITICAL")){
bugs_CRITICAL=Long.parseLong(objs[1].toString());;
}
if(severity.equals("MAJOR")){
bugs_MAJOR=Long.parseLong(objs[1].toString());;
}
if(severity.equals("MINOR")){
bugs_MINOR=Long.parseLong(objs[1].toString());;
}
if(severity.equals("INFO")){
bugs_INFO=Long.parseLong(objs[1].toString());;
}
}
}
long bugs_SUM=bugs_BLOCKER+bugs_CRITICAL+bugs_MAJOR+bugs_MINOR+bugs_INFO;
content.append(" <tr bgcolor=#ECFFFF><td>bugs</td><td align=\"center\">"+bugs_BLOCKER+"</td>" +
"<td align=\"center\">"+bugs_CRITICAL+"</td>" +
"<td align=\"center\">"+bugs_MAJOR+"</td>" +
"<td align=\"center\">"+bugs_MINOR+"</td>" +
"<td align=\"center\">"+bugs_INFO+"</td>" +
"<td align=\"center\" style=\"color:red\">"+bugs_SUM+"</td></tr>\n");
//bad_states坏味道内容处理
List<Object[]> badtasteslist=services.findbadtastesInfo(project_uuid);
System.out.println();
long badtastes_MAJOR=0;//主要的
long badtastes_MINOR=0;//次要的
long badtastes_CRITICAL=0;//严重的
long badtastes_INFO=0;//提示
long badtastes_BLOCKER=0;//阻断
if(badtasteslist.size()>0){
for (Object[] objs : badtasteslist) {
String severity = objs[0].toString();
if(severity.equals("BLOCKER")){
badtastes_BLOCKER=Long.parseLong(objs[1].toString());;
}
if(severity.equals("CRITICAL")){
badtastes_CRITICAL=Long.parseLong(objs[1].toString());;
}
if(severity.equals("MAJOR")){
badtastes_MAJOR=Long.parseLong(objs[1].toString());;
}
if(severity.equals("MINOR")){
badtastes_MINOR=Long.parseLong(objs[1].toString());;
}
if(severity.equals("INFO")){
badtastes_INFO=Long.parseLong(objs[1].toString());;
}
}
}
long badtastes_SUM=badtastes_BLOCKER+badtastes_CRITICAL+badtastes_MAJOR+badtastes_MINOR+badtastes_INFO;
content.append(" <tr bgcolor=#D2E9FF><td>坏味道</td><td align=\"center\">"+badtastes_BLOCKER+"</td>" +
"<td align=\"center\">"+badtastes_CRITICAL+"</td>" +
"<td align=\"center\">"+badtastes_MAJOR+"</td>" +
"<td align=\"center\">"+badtastes_MINOR+"</td>" +
"<td align=\"center\">"+badtastes_INFO+"</td>" +
"<td align=\"center\" style=\"color:red\">"+badtastes_SUM+"</td></tr>\n");
//漏洞内容处理
List<Object[]> vbslist=services.findVulnerabilitiesInfo(project_uuid);
long vbs_MAJOR=0;//主要的
long vbs_MINOR=0;//次要的
long vbs_CRITICAL=0;//严重的
long vbs_INFO=0;//提示
long vbs_BLOCKER=0;//阻断
if(vbslist.size()>0){
for (Object[] objs : vbslist) {
String severity = objs[0].toString();
if(severity.equals("BLOCKER")){
vbs_BLOCKER=Long.parseLong(objs[1].toString());;
}
if(severity.equals("CRITICAL")){
vbs_CRITICAL=Long.parseLong(objs[1].toString());;
}
if(severity.equals("MAJOR")){
vbs_MAJOR=Long.parseLong(objs[1].toString());;
}
if(severity.equals("MINOR")){
vbs_MINOR=Long.parseLong(objs[1].toString());;
}
if(severity.equals("INFO")){
vbs_INFO=Long.parseLong(objs[1].toString());;
}
}
}
long vbs_SUMm=vbs_BLOCKER+vbs_CRITICAL+vbs_MAJOR+vbs_MINOR+vbs_INFO;
content.append(" <tr bgcolor=#ECFFFF><td>漏洞</td><td align=\"center\">"+vbs_BLOCKER+"</td>" +
"<td align=\"center\">"+vbs_CRITICAL+"</td>" +
"<td align=\"center\">"+vbs_MAJOR+"</td>" +
"<td align=\"center\">"+vbs_MINOR+"</td>" +
"<td align=\"center\">"+vbs_INFO+"</td>" +
"<td align=\"center\" style=\"color:red\">"+vbs_SUMm+"</td></tr>\n");
//html末尾
content.append("</table>\n" +
"<br><span style=\"font-weight:bold;\"><b style=\"color:red\">代码扫描度量通过准则:</b></span>\n" +
"<br><span style=\"font-size:14px\">新覆盖率<80%;\n" +
"<br><span style=\"font-size:14px\">新代码中的重复行密度 (%)>30%;\n" +
"<br><span style=\"font-size:14px\">新代码可维护率劣于A;\n" +
"<br><span style=\"font-size:14px\">新代码可靠率劣于A;\n" +
"<br><span style=\"font-size:14px\">新代码安全率劣于A;\n" +
"<br></br>\n" +
"</body>\n" +
"</html>");
//发邮件
Map<String, String> emailAddrMap=new HashMap<String, String>();
if(email!=null){
emailAddrMap.put(Message.RecipientType.TO.toString(),email);
}
/**
* 接收者类型可以是Message.RecipientType.TO,Message.RecipientType.CC和Message.RecipientType.BCC,
*
* TO表示主要接收人,CC表示抄送人,BCC表示秘密抄送人。
*/
SendEmal sendEmal=new SendEmal();
sendEmal.sendEmail(sendEmal.MOFANGSENDER,emailAddrMap,projectKey+"扫描报告",content.toString());
return "OK";
}
}
jenkins部署
发邮件接口
http://localhost:8002/report/{projectKey}/a@aa.com.cn,qa@aa.com.cn,rdcenter@aa.com.cn
这里要注意email参数分隔符是逗号
标题题外
懂的人,认真看一下,就会懂。