搜索 Sonarcloud API returns none 的问题,即使它们存在

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

我正在尝试从私有 Sonarcloud 项目的分支中获取所有声纳报告问题。 我通过以下 REST 调用执行此操作:

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

如果我在网络浏览器中正常输入此呼叫或使用邮递员呼叫,我会收到此响应:

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

所以我得到了 1 个声纳问题的完整报告,就像 Sonarcloud 页面中显示的那样。 现在,当我想在Java中这样做时,报告突然没有问题了。我用 CloseableHttpClient:

进行 REST 调用
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);

打印出来的正文是这样的:

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

如您所见,问题现在是空的。该报告也确实已经存在,因此声纳报告尚未准备好不是问题。有帮助吗?

原来是权限问题。似乎 CloseableHttpClient 无法识别网络调用中给定的 loginHash。 您必须像这样手动添加授权:

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");

如您所见,不再需要 URI 中的 loginHash