0

I am trying to retreive SonarQube results using Java API's. While I am able to successfully retreive some results, I am facing few challenges as well. I have following 2 problems

Problem 1 : My code is as follows

IssueQuery issueQuery = IssueQuery.create();
issueQuery.severities("MAJOR", "MINOR");
IssueClient issueClient = sonarClient.issueClient();
Issues issues = issueClient.find(issueQuery);
List<Issue> issueList = issues.list();

issues.list() in above code always gives 100 results only. Don't know how to configure this parameter to fetch all results. Can you please let me know how to retrive all issues?

Problem 2 : While above code returns results for all projects. Is there any way I can retrive issues for single project? I tried the following code but it does not seem to be working.

IssueQuery issueQuery = IssueQuery.create();
issueQuery.issues(projKey1, projKey2);
IssueClient issueClient = sonarClient.issueClient();
Issues issues = issueClient.find(issueQuery);
List<Issue> issueList = issues.list();
System.out.println("No of issues = "+issueList.size());

issueList.size() is returning value 0 Can you please tell me how can I acheive this?

I searched a lot on internet but could not find any help? Could you please help me n this regard? Thanks in advance.

G. Ann - SonarSource Team
  • 22,346
  • 4
  • 40
  • 76
Sachin
  • 247
  • 4
  • 16
  • 26
  • 2
    For problem 1: Obviously, there is some sort of page mechanism within IssueQuery. See Javadoc: http://javadocs.sonarsource.org/4.5.2/apidocs/org/sonar/wsclient/issue/IssueQuery.html Without having tried, perhaps try `issueQuery.severities(...).pageSize(10000)`. – Florian Albrecht Oct 19 '16 at 15:21

1 Answers1

0

Problem 1 Results are paginated. See Issues#paging() for the information about total number of results and IssueQuery#pageIndex(int) for requesting other pages.

Problem 2 The method IssueQuery#componentRoots(String... projectKeys) allows to filter on projects.

Simon Brandhof
  • 5,137
  • 1
  • 21
  • 28
  • Hey Simon, thanks a lot for your response. On issueQuery object I am not able to get componentRoots() method. I am using sonar-ws-client.jar. But I could get issueQuery.components() method instead. So I tried with that but it is not working. Following is my code IssueQuery issueQuery = IssueQuery.create(); issueQuery = issueQuery.components(projKey1, projKey2); IssueClient issueClient = sonarClient.issueClient(); Issues issues = issueClient.find(issueQuery); List issueList = issues.list(); System.out.println("No of issues = "+issueList.size()); – Sachin Oct 20 '16 at 07:35
  • which version of library ? – Simon Brandhof Oct 20 '16 at 12:03
  • I am using sonar-ws-client-5.1.jar – Sachin Oct 20 '16 at 12:38
  • Hey Simon, do you think I am missing something here? – Sachin Oct 20 '16 at 15:03
  • You should check what are the results of web service `api/issues/search?componentKeys=projKey1`. See documentation at /api_documentation#api/issues. – Simon Brandhof Oct 21 '16 at 07:27
  • Hey Simon, thank you again for your help. If I use api/issues/search?componentKeys=projKey1. then I am getting result. But somehow through code it does not seem to be working. Could you please post sample code here in accordance with my library version? – Sachin Oct 21 '16 at 09:58
  • Hi Simpon, I used library sonar-ws-client-5.0 and guess what I could make it work using the method named componentRoots(). Sadly this method is not there in v5.1 I just wondered why this method is removed in v5.1. Is there any other method that I need to use in v5.1? – Sachin Oct 21 '16 at 10:24
  • ws-client is a simple layer to call api/issues/search web service, so I suggest that you have a look at the source code of v5.1. It maps exactly the parameters that you tested: https://github.com/SonarSource/sonarqube/blob/5.1/server/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueQuery.java – Simon Brandhof Oct 21 '16 at 12:28
  • Thanks again for your reply. But sadly there is no API named componentRoots() in version 5.1. You can verify it here https://github.com/SonarSource/sonarqube/blob/5.1/server/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueQuery.java Could you please let me know alternate API in version 5.1? – Sachin Oct 24 '16 at 10:11