2

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?

NicoB
  • 75
  • 9
  • We're taking your word that those filled in "<..">" blocks have exactly the correct values. I imagine the "loginHash" must have the correct value, or you'd get something else. – David M. Karr Feb 13 '21 at 01:11
  • @DavidM.Karr The values inside of the "<...>" are correct, since I get the wanted report back from the postman call. And I use the same URI for every call. The loginHash has to be correct, as you said. But I still wonder why I get a report back with 0 issues and not a "401 Unauthorized". – NicoB Feb 15 '21 at 07:51

1 Answers1

0

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.

NicoB
  • 75
  • 9