sonarqube8.4报告系列-bugs统计报告

sonar-bugs数据库是有统计的,但是查询的话,也不如api来的方便。
sonar-api入口,我就不再说了,里面有很多接口,我是翻了一遍,其中post多用于与其它第三方应用做webhooks的时候,可能会比较方便做自动化,get多用于查询数据

报告结果
在这里插入图片描述
请憋住指责,我们这个这么多问题,是因为质量真的很差,举个例子:一个周期3周的项目,手工进行功能测试至少是150个BUG起步。有多差,靠想象
sonar-api
Authorization接口调用要进行身份验证,使用postman生成Authorization的值即可.

public class SonarWebApi {
    /*
     *
     * sonar自带的邮件报告api,无法使用html
     * @throws IOException*/

    /*public static  void sendmail() throws IOException {
        SendRuquest sendRuquest=new SendRuquest();
        WebApi sendMailInfo =new WebApi();
        String message= sendMailInfo.getemailContent();
        String subject="项目质量汇总报告";
        String url="http://sonar.com.cn/api/emails/send";
//        ?message=hello&subject=hi&to=liujuan@pconline.com.cn
        String to="liujuan@pconline.com.cn";
        Map<String,String> headerMap=new HashMap<String,String>();
        headerMap.put("Authorization","Basic YWRtaW46YWRtaW4=");
        String params="message="+message+"&subject="+subject+"&to="+to;
        String flag=sendRuquest.sendPost(url,params,headerMap);
        if(flag!=null){
            System.out.println("发送成功");
        }else {
            System.out.println("发送失败");
        }

    }*/
    /**
     * 获取单个项目的各个质量结果 ,得到JSONArray
     * @return
     * @throws IOException
     */
    public  JSONObject getmeasures(String projectKee){
        SendRuquest sendRuquest=new SendRuquest();
        String url="http://sonar.com.cn/api/measures/component";
        String params="component="+projectKee+
                "&metricKeys=new_bugs,bugs,new_coverage,coverage,new_vulnerabilities," +
                "vulnerabilities,new_duplicated_lines_density,duplicated_lines_density";
        Map<String,String> headerMap=new HashMap<String,String>();
        headerMap.put("Authorization","Basic YWRtaW46YWRtaW4=");
        JSONObject result =null;
        try {
            result = JSONObject.parseObject(sendRuquest.sendGet(url,params,headerMap));
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;

    }
    /**
     *获取项目质量结果,
     */
    public  String getProjectStatus(String projectKee){
        SendRuquest sendRuquest=new SendRuquest();
        String projectStatus="";
        Map<String,String> headerMap=new HashMap<String,String>();
        headerMap.put("Authorization","Basic YWRtaW46YWRtaW4=");
        try {
            String url="http://sonar.com.cn/api/qualitygates/project_status";
            String params="projectKey="+projectKee;
            String reponse = sendRuquest.sendGet(url,params,headerMap);
            JSONObject json = (JSONObject) JSONObject.parse(reponse);
            JSONObject object2=json.getJSONObject("projectStatus");
            projectStatus=object2.getString("status");
        }catch (Exception e){
            e.printStackTrace();
        }
        return projectStatus;
    }

  /*  public static void main(String[] args) {
        SpringApplication.run(SonarapiApplication.class, args);
        ApplicationContext context = SpringContextUtil.getApplicationContext();
        ProjectsServices services = context.getBean(ProjectsServices.class);// 注意是Service,不是ServiceImpl
        List<String> keelist = services.getAllProjectKee();
        for (String kee:keelist) {
            JSONObject result = getmeasures(kee);
            System.out.println("result===="+result);
        }


    }*/
    /**
     * 获取项目分析结果,整合版
     */
    public static JSONObject getProjectMeasures(String projectKee){
        SendRuquest sendRuquest=new SendRuquest();
        JSONObject json =null;
        Map<String,String> headerMap=new HashMap<String,String>();
        headerMap.put("Authorization","Basic YWRtaW46YWRtaW4=");
        try {
            String url="http://sonar.com.cn/api/components/app";
            String params="component="+projectKee;
            String reponse = sendRuquest.sendGet(url,params,headerMap);
            json= (JSONObject) JSONObject.parse(reponse);
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 获取项目分析结果,整合版
     */
    public JSONArray getProjectMeasuresToArray(List<String> keelist){
        JSONArray array=new JSONArray();
        for (String kee:keelist){
            array.add(getProjectMeasures(kee));
        }
        return array;
    }

}

获取html内容

 public static String getEmailContent() {
        StringBuffer content=new StringBuffer();
        String title="<table border=\"1\" style=\"border-collapse: collapse;\" style=\"margin: auto;\" " +
                "        <tr >" +
                "            <th bgcolor=\"#a9a9a9\">项目</th>" +
                "            <th bgcolor=\"#a9a9a9\">总bug数据</th>" +
                "            <th bgcolor=\"#a9a9a9\">涉及代码行数</th>" +
                "            <th bgcolor=\"#a9a9a9\">单元测试数</th>" +
                "            <th bgcolor=\"#a9a9a9\">单元测试覆盖率(%)</th>" +
                "            <th bgcolor=\"#a9a9a9\">代码重复率(%)</th>" +
                "            <th bgcolor=\"#a9a9a9\">详细结果</th>" +
                "        </tr>";
        content.append(title);
        ParseJson parseJson=new ParseJson();
        Common mapSortDemo=new Common();
        SonarWebApi sonarWebApi=new SonarWebApi();
        ApplicationContext context = SpringContextUtil.getApplicationContext();
        ProjectsServices services = context.getBean(ProjectsServices.class);// 注意是Service,不是ServiceImpl
        List<String> keelist=services.getAllProjectKee();
        JSONArray array = sonarWebApi.getProjectMeasuresToArray(keelist);
        Map<Integer,List<String>> personmap =parseJson.jsonParseToTableStringSort1(array);
        Map<Integer, List<String>> resultMap = mapSortDemo.sortMapByKey(personmap);  //Key进行排序
        int i=1;
        for (Map.Entry<Integer, List<String>> entry : resultMap.entrySet()) {
            System.out.println(entry.getKey() + " " + entry.getValue());
            List<String> lists=entry.getValue();
            System.out.println("==="+lists.get(0));
            String tr="<tr style=\"text-align: center\">";
            int bugs=entry.getKey();
            String bugsall="";
            int lines=Integer.parseInt(lists.get(1));
            String linesall="";
            DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
            if(bugs>=1000){
                bugsall=bugs / 1000+"K";
            }
            if(lines>=1000){
                linesall=decimalFormat.format(lines / 1000)+"K";
            }
            if(i<=20) {
                /*content.append(tr + lists.get(0) + "<td>" + entry.getKey() + "</td>" + lists.get(1) + lists.get(2) + lists.get(3) + lists.get(4) + lists.get(5) + "</tr>");*/
                content.append(tr + lists.get(0) + "<td>" + bugsall + "</td>" + "<td>"+ linesall + "</td>"
                        + "<td>" + lists.get(2)+ "</td>" + "<td>"+ lists.get(3)+ "</td>"
                        + "<td>"+ lists.get(4)+ "</td>"+ lists.get(5)+ "</tr>");
                i++;
            }
        }

        content.append("</table>");
        content.append("共扫描"+keelist.size()+"个项目,正文列出bug总数排行前20的项目情况,其它项目详情请查看:"+"<a href=\"http://sonar.pc.com.cn\">https://sonar.pc.com.cn</a>");
        System.out.println(content.toString());
        return content.toString();
    }

说明:我这里埋了个隐患,我为了按bug数量排序,使用了Map<Integer, List<String>> resultMap = mapSortDemo.sortMapByKey(personmap) ,后面一旦有key相同的数据,就会会替换掉。在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值