如何使用SonarQube Web API?

2022-09-04 07:21:32

之前,我询问了如何从SonarQube数据库导出自定义数据,声纳团队建议我应该使用Web API。

经过一些研究,我仍然在为如何使用Web API而苦恼。(我对Web API的工作原理非常不熟悉)

读完这篇文章后,我意识到我可以使用Java代码来做到这一点。(我刚刚介绍了如何使用Apache Http Client)但是,运行后

HttpGet httpGet = new HttpGet("http://localhost:9000/api/issues?metrics=lines");(抄自该帖子)

我得到了:

HTTP/1.1 404 {"errors":[{"msg":"Unknown url : /api/issues"}]}

在我将此行更改为:

HttpGet httpGet = new HttpGet("http://localhost:9000/project/issues?facetMode=effort&id=project%3Atesting&resolved=false&types=CODE_SMELL");

我得到了:

HTTP/1.1 200 <!DOCTYPE html><html lang="en"><head><meta http-equiv="content-type" content="text/html; charset=UTF-8" charset="UTF-8"/><meta http-equiv="X-UA-Compatible" content="IE=edge"><link rel="apple-touch-icon" href="/apple-touch-icon.png"><link rel="apple-touch-icon" sizes="57x57" href="/apple-touch-icon-57x57.png"><link rel="apple-touch-icon" sizes="60x60" href="/apple-touch-icon-60x60.png"><link rel="apple-touch-icon" sizes="72x72" href="/apple-touch-icon-72x72.png"><link rel="apple-touch-icon" sizes="76x76" href="/apple-touch-icon-76x76.png"><link rel="apple-touch-icon" sizes="114x114" href="/apple-touch-icon-114x114.png"><link rel="apple-touch-icon" sizes="120x120" href="/apple-touch-icon-120x120.png"><link rel="apple-touch-icon" sizes="144x144" href="/apple-touch-icon-144x144.png"><link rel="apple-touch-icon" sizes="152x152" href="/apple-touch-icon-152x152.png"><link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon-180x180.png"><link rel="icon" type="image/x-icon" href="/favicon.ico"><meta name="application-name" content="SonarQube"/><meta name="msapplication-TileColor" content="#FFFFFF"/><meta name="msapplication-TileImage" content="/mstile-512x512.png"/><link href="/css/sonar.bf342fee.css" rel="stylesheet"><title>SonarQube</title></head><body><div id="content"><div class="global-loading"><i class="spinner global-loading-spinner"></i> <span class="global-loading-text">Loading...</span></div></div><script>window.baseUrl=""</script><script src="/js/vendor.0ba4fd94.js"></script><script src="/js/app.bf342fee.js"></script></body></html>

这也不是我所期望的。

我想知道使用Web API的正确方法是什么?例如,如果我想获取项目的代码异味。Java中的代码应该如何?

以下是我目前使用的代码:

import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class Test {
    public static void main(String[] args) throws ClientProtocolException, IOException {

        //HttpGet httpGet = new HttpGet("http://localhost:9000/api/issues?metrics=lines");
        HttpGet httpGet = new HttpGet("http://localhost:9000/project/issues?facetMode=effort&id=project%3Atesting&resolved=false&types=CODE_SMELL");

        try(CloseableHttpClient httpClient = HttpClients.createDefault();
            CloseableHttpResponse response = httpClient.execute(httpGet);) {
            System.out.println(response.getStatusLine());
            HttpEntity entity = response.getEntity();
            System.out.println(EntityUtils.toString(entity));
        }
    }
}

感谢您的任何帮助或指导!


答案 1

根据SonarQube文档,SonarQube Web API与部分和操作(您似乎缺少)一起位于上下文路径下。/api

例如,若要在端口 9000 上运行的本地主机上搜索问题,请发送 to 并解析 JSON 响应。GEThttp://localhost:9000/api/issues/search?pageSize=500&componentKeys=YOUR_COMPONENT

您可能还需要提供授权,授权将作为 BASIC 用户名密码组合或可通过 Web 客户端检索的访问令牌发送。


答案 2

http://localhost:9000/web_api/可能会有所帮助,解释参数,使用Sonarqube版本提供响应和跟踪更改的示例。


推荐