如何使用SonarQube Web API?

问题描述 投票:4回答:1

以前,我询问过how to export custom data from SonarQube Database,而声纳团队建议我应该使用Web API。

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

读完这个post之后,我意识到我可以使用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));
        }
    }
}

感谢任何帮助或指导!

java sonarqube apache-httpclient-4.x
1个回答
10
投票

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

例如,要搜索在端口9000上运行的localhost上的问题,请将GET发送到http://localhost:9000/api/issues/search?pageSize=500&componentKeys=YOUR_COMPONENT并解析JSON响应。

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

© www.soinside.com 2019 - 2024. All rights reserved.