Skip to content
Advertisement

Searching for issues with Sonarcloud API returns none even though they exist

I am trying to get all the sonar report issues from a branch of a private Sonarcloud project. I do this with the following REST call:

https://<loginHash>@sonarcloud.io/api/issues/search?projects=<projectKey>&branch=<branchName>

If I enter this call normally in the webbrowser or with a postman call, I get this response:

{
   "total": 1,
   "p": 1,
   "ps": 100,
   "paging": {
       "pageIndex": 1,
       "pageSize": 100,
       "total": 1
   },
   "effortTotal": 5,
   "debtTotal": 5,
   "issues": [
       {
          ...
       }
   ],
   ...
}

So I get the full report with the 1 sonar issue, like it’s shown in the Sonarcloud page. Now, when I want to do this in Java, the report suddenly has no issues. I do the REST call with a CloseableHttpClient:

CloseableHttpClient client = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("https://<loginHash>@sonarcloud.io/api/issues/search?projects=<projectKey>&branch=<branchName>");

CloseableHttpResponse response = client.execute(httpGet);
String body = EntityUtils.toString(response.getEntity(), "UTF-8");

System.out.println(body);

And the printed body is this:

{
   "total":0,
   "p":1,
   "ps":100,
   "paging":{
      "pageIndex":1,
      "pageSize":100,
      "total":0
   },
   "effortTotal":0,
   "debtTotal":0,
   "issues":[],
   "components":[],
   "organizations":[],
   "facets":[]
}

As you can see the issues are now empty. The report does also already exist, so it can’t be an issue with that the sonar report isn’t ready yet. Any help?

Advertisement

Answer

It turned out to be an authorization problem. Seems like the CloseableHttpClient doesn’t recognize the given loginHash inside of the web call. You have to add the authorization manually like this:

CloseableHttpClient client = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("https://sonarcloud.io/api/issues/search?projects=<projectKey>&branch=<branchName>"); 
httpGet.setHeader(HttpHeaders.AUTHORIZATION, "<Login:Password>");

CloseableHttpResponse response = client.execute(httpGet);
String body = EntityUtils.toString(response.getEntity(), "UTF-8");

As you can see, the loginHash inside of the URI isn’t needed anymore.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement