Reputation: 2137
I am trying to get the total number of unresolved bugs and Vulnerabilities in the particular project using sonar-ws-5.6.jar
.
I tried to pass the type as BUG to the search query. But still I am getting all the unresolved stuff. It is not taking the parameter type.
How to get the exact number of bugs and Vulnerabilities using Webservice?
Here is my code to connect to sonar and get the data.
import java.util.ArrayList;
import java.util.List;
import org.sonarqube.ws.Issues.SearchWsResponse;
import org.sonarqube.ws.client.HttpConnector;
import org.sonarqube.ws.client.WsClient;
import org.sonarqube.ws.client.WsClientFactories;
import org.sonarqube.ws.client.issue.SearchWsRequest;
public class SonarTest {
static String resourceKey = "com.company.projectname:parent";
public static void main(String[] args) {
try {
// Get Issue
HttpConnector httpConnector = HttpConnector.newBuilder().url("http://localhost:9000").credentials("admin", "admin").build();
SearchWsRequest issueSearchRequest = new SearchWsRequest();
issueSearchRequest.setPageSize(1000);
issueSearchRequest.setResolved(false);
List<String> bugTypesList = new ArrayList<String>();
bugTypesList.add("BUG");
issueSearchRequest.setTypes(bugTypesList);
WsClient wsClient = WsClientFactories.getDefault().newClient(httpConnector);
SearchWsResponse issuesResponse = wsClient.issues().search(issueSearchRequest);
System.out.println(issuesResponse.getIssuesList());
System.out.println("DONE");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Note: I am using sonarqube 5.6 with Java 1.8
As of now I am iterating the response and getting the count
List<Issue> issueList = issuesResponse.getIssuesList();
int bugCount = 0;
for(Issue issue : issueList){
if(issue.getType() == RuleType.BUG){
bugCount ++;
}
}
Upvotes: 4
Views: 1246
Reputation: 2137
I am using ComponentWsRequest to get the total number of bugs
We can pass the metric key to get the required value.
Here is the code which gives me the total number of bugs.
List<String> VALUE_METRIC_KEYS = Arrays.asList("bugs");
ComponentWsRequest componentWsRequest = new ComponentWsRequest();
componentWsRequest.setComponentKey(resourceKey);
componentWsRequest.setMetricKeys(VALUE_METRIC_KEYS);
ComponentWsResponse componentWsResponse = wsClient.measures().component(componentWsRequest);
List<Measure>measureList = componentWsResponse.getComponent().getMeasuresList();
for(Measure measure : measureList){
System.out.println(measure);
}
We can use any of the metric keys to get the respective values:
"quality_gate_details","reliability_rating","reliability_remediation_effort","vulnerabilities","security_rating","security_remediation_effort","code_smells","sqale_rating","sqale_debt_ratio","effort_to_reach_maintainability_rating_a","sqale_index","ncloc","lines","statements","functions","classes","files","directories","duplicated_lines_density","duplicated_blocks","duplicated_lines","duplicated_files","complexity","function_complexity","file_complexity","class_complexity","comment_lines_density","comment_lines","public_api","public_documented_api_density","public_undocumented_api","violations","open_issues","reopened_issues","confirmed_issues","false_positive_issues","wont_fix_issues"
Upvotes: 2
Reputation: 7321
Well, you caught a bug ! I used your code and found out that the types
parameter was not properly passed from the WSClient to the actual HTTP query.
So thanks for sharing your issue, SONAR-7871 is opened to have it addressed.
Upvotes: 1