我需要编写Java代码来检索问题列表,其中包含SonarQube的完整描述。我使用Sonar WS Client JAR编写以下代码,但我收到以下错误:
java.lang.IllegalStateException:无法请求127.0.0.1:9001/api/issues/search?rules=Major,Minor,Critical
public class App {
public static void main(String args[]) {
try {
String url = "http://127.0.0.1:9001";
String login = "admin";
String password = "admin";
SonarClient client = SonarClient.builder()
.url(url)
.login(login)
.password(password)
.build();
IssueQuery query = IssueQuery.create();
query.rules("Major","Minor","Critical");
IssueClient issueClient = client.issueClient();
Issues issues = issueClient.find(query);
List<Issue> issueList = issues.list();
for (int i = 0; i < issueList.size(); i++) {
System.out.println(issueList.get(i).projectKey() + " " +
issueList.get(i).componentKey() + " " +
issueList.get(i).line() + " " +
issueList.get(i).ruleKey() + " " +
issueList.get(i).severity() + " " +
issueList.get(i).message());
}
} catch (Exception ex) {
System.out.println(ex);
}
}
}
如何使用Sonar WS Client服务获取问题列表?
答案 0 :(得分:0)
如果您想让主要的,次要的,关键问题在行下方发生变化
query.rules("Major","Minor","Critical");
到
query.severities("MAJOR","MINOR","CRITICAL");
答案 1 :(得分:0)
我已经使用Sonar客户端创建了HTML报告,即使您可以使用POI jar来创建.xls报告。在使用以下代码之前,请在类路径中设置 sonar-ws-client jar。
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/Application Snapshot info
builder.append("<table class='projectInfo' style='width:26%; margin-bottom:10px'><tr><td style='border-color: #fff;width: 86%;' class='title'> 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;
}
}