SonarQube 6.0 pdf报告

时间:2017-07-28 12:06:46

标签: sonarqube

我使用SonarQube 6.0并尝试将Analysis报告导出为pdf。 不幸我得到了#34;报告不可用。安装插件后至少需要进行一次分析。"

2 个答案:

答案 0 :(得分:0)

您无法使用此插件,因为该版本仅与SonarQube 4.5.7兼容。该插件被Governance插件(商业产品,不单独提供)取代,但该版本仅与SonarQube 5.6兼容。你今天唯一的选择是自己构建插件。但请注意:可用的API可以在不同的SonarQube版本之间进行更改。

答案 1 :(得分:0)

如果您愿意,我可以使用 sonar-ws-client 创建我自己的报告,甚至可以像我创建的那样创建自己的 .pdf 报告。

注意:在使用以下代码之前,请将 sonar-ws-client 导入您的类路径中。

import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.sonar.wsclient.SonarClient;
import org.sonar.wsclient.issue.Issue;
import org.sonar.wsclient.issue.IssueClient;
import org.sonar.wsclient.issue.IssueQuery;
import org.sonar.wsclient.issue.Issues;

public class SonarReport {

    public static void main(String args[]) {

        String login = "admin";
        String password = "admin";

        SonarClient client = SonarClient.create("http://10.20.120.47:9000");
        client.builder().login(login);
        client.builder().password(password);

        List<Issue> issuesList = new ArrayList<Issue>();
        IssueClient issueClient = client.issueClient();
        Issues issues = null;

        IssueQuery query = IssueQuery.create();
        //query.severities("BLOCKER","CRITICAL", "MAJOR");
        query.resolved(false);
        query.urlParams().put("types", "VULNERABILITY");


        int issueCount = 0;

        for (int i = 1; i < 7; i++) {

            query.pageIndex(i);
            query.pageSize(500);

            issues = issueClient.find(query);
            issuesList.addAll(issues.list());

            System.out.println("In progress ...");
        }

        createHTMLReport(issuesList);
    }

    private static void createHTMLReport(List<Issue> issueList) {

        try {
            String filename = "SonarIssues.html";

            PrintWriter pw = new PrintWriter(new FileWriter(filename));
            StringBuilder builder = new StringBuilder();

            // Construct report data
            Map<String,String> tableData = cunstructTableData(issueList);

            if(tableData!= null)
            {
                // HTML page construction
                builder.append("<!DOCTYPE>").append("\n");
                builder.append("<html>").append("\n");
                builder.append("<head>").append("\n");
                builder.append("<style>").append("\n");
                builder.append("table, th, td {").append("\n");
                builder.append(" border: 1px solid black;").append("\n");
                builder.append(" border-collapse: collapse;font-size: 13px !important;").append("\n");
                builder.append("}").append("\n");
                builder.append("th, td {  ").append("\n");
                builder.append(" padding: 10px;  ").append("\n");
                builder.append("}").append("\n");
                builder.append("table#alter tr:nth-child(even) { ").append("\n");
                builder.append("background-color: #eee;").append("\n");
                builder.append("}").append("\n");
                builder.append("table#alter tr:nth-child(odd) {  ").append("\n");
                builder.append("    background-color: #fff;  ").append("\n");
                builder.append("}  ").append("\n");
                builder.append("table#alter th {  ").append("\n");
                builder.append("    color: white; ").append("\n");
                builder.append("    background-color: gray;  ").append("\n");
                builder.append("}").append("\n");
                builder.append(" table {margin-bottom:50px;}").append("\n");
                builder.append(" table tr .title {background:#5f9be3;color:#fff;font-weight:bold;padding:5px; width:100px;   }").append("\n");
                builder.append(" table tr .text {padding-left:10px;}").append("\n");
                builder.append("</style>").append("\n");
                builder.append("</head>").append("\n");
                builder.append("<body style='font-family: Verdana, Geneva, sans-serif;'> ").append("\n");

                // Application version info
                builder.append("<table class='projectInfo' style='width:26%; margin-bottom:10px'><tr><td style='border-color: #fff;width: 86%;' class='title'> Project Version: v4.4.0</td>").append("\n");
                builder.append("</tr></table>").append("\n");

                // Table header info
                builder.append("<table class='statisticsInfo'><tr><td style='border-color: #fff;width: 86%;' class='title'>Total No. of issues:</td>").append("\n");
                builder.append("<td style='background-color: yellow;border-color: #fff;' class='text'>"+tableData.get("TotalNumberOfIssues")+"</td></tr>").append("\n");

                builder.append("<tr><td style='border-color: #fff;width: 86%;' class='title'>Blocker Issues:</td>").append("\n");
                builder.append("<td style='background-color: yellow;border-color: #fff;' class='text'>"+tableData.get("BlockerIssues")+"</td></tr>").append("\n");

                builder.append("<tr><td style='border-color: #fff;width: 86%;' class='title'>Critical Issues: </td>").append("\n");
                builder.append("<td style='background-color: yellow;border-color: #fff;' class='text'>"+tableData.get("CriticalIssues")+"</td></tr>").append("\n");

                builder.append("<tr><td style='border-color: #fff;width: 86%;' class='title'>Major Issues: </td>").append("\n");
                builder.append("<td style='background-color: yellow;border-color: #fff;' class='text'>"+tableData.get("MajorIssues")+"</td></tr>").append("\n");

                builder.append("<tr><td style='border-color: #fff;width: 86%;' class='title'>Minor Issues: </td>").append("\n");
                builder.append("<td style='background-color: yellow;border-color: #fff;' class='text'>"+tableData.get("MinorIssues")+"</td></tr></table>").append("\n");

                builder.append("<table id='alter'>").append("\n");
                // Header part
                builder.append("<tr><th>File name</th>");
                builder.append("<th>Severity</th>");
                builder.append("<th>Status</th>");
                builder.append("<th>Message</th></tr> ");

                builder.append(tableData.get("TableData")).append("\n");

                builder.append("</table>").append("\n");
                builder.append("</body>").append("\n");
                builder.append("</html>").append("\n");

                pw.println(builder.toString());
                pw.close();
            }

            System.out.println("HTML Report has been generated!");

        } catch (Exception ex) {
            System.out.println(ex);

        }
    }

    private static Map<String, String> cunstructTableData(List<Issue> issueList) {

         Map<String,String> result = new HashMap<String, String>();
         StringBuilder tableData = new StringBuilder();

         int blockerIssues = 0;
         int criticalIssues = 0;
         int majorIssues = 0;
         int minorIssues = 0;

         result.put("TotalNumberOfIssues", String.valueOf(issueList.size()));

         for (int i = 0; i < issueList.size(); i++) {

            String[] className = issueList.get(i).componentKey().split("/");

            tableData.append("<tr><td>"+className[className.length -1]+"</td>");
            tableData.append("<td>"+issueList.get(i).severity()+"</td>");
            tableData.append("<td>"+issueList.get(i).status()+"</td>");
            tableData.append("<td>"+issueList.get(i).message()+"</td></tr> ").append("\n");

            switch(issueList.get(i).severity()){

                case "BLOCKER":
                    ++blockerIssues;
                    break;
                case "CRITICAL":
                    ++criticalIssues;
                    break;
                case "MAJOR":
                    ++majorIssues;
                    break;
                case "MINOR":
                    ++minorIssues;
                    break;
            }
         }

         result.put("MajorIssues",String.valueOf(majorIssues));
         result.put("CriticalIssues",String.valueOf(criticalIssues));
         result.put("BlockerIssues",String.valueOf(blockerIssues));
         result.put("MinorIssues",String.valueOf(minorIssues));

         result.put("TableData", tableData.toString());

        return result;
    }

}