2017-09-20 93 views
1

以前,我問過how to export custom data from SonarQube Database,Sonar團隊建議我應該使用Web API。如何使用SonarQube網絡API?

經過一番研究,我仍然爲如何使用Web API而苦苦掙扎。 ( 我對Web API的工作原理非常陌生)

看完這個post後,我意識到我可以使用Java代碼來實現這一點。但是(我已經通過了如何使用Apache HTTP客戶端剛剛過去),運行後

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

欣賞任何幫助或指導!

回答

5

根據SonarQube文檔,SonarQube web API位於/api上下文路徑下,以及部分和操作(您似乎缺少)。

例如,要搜索在端口9000上運行的本地主機上的問題,請發送GEThttp://localhost:9000/api/issues/search?pageSize=500&componentKeys=YOUR_COMPONENT並解析JSON響應。

您可能還需要提供授權,該授權既可以作爲BASIC用戶名密碼組合發送,也可以通過Web客戶端檢索到訪問令牌。

+0

非常感謝你,這就是我需要的! – MaXon

+0

你能否解釋我應該在'?'之後放置什麼?標記? 在你的情況,你把pageSize = 500和componentKeys = YOUR_COMPONENT。我發現componentKeys是'issues/search'下的參數之一。那麼pageSize呢?我在哪裏可以找到這種參數? – MaXon

+1

'pageSize'只是控制你回來的結果數量,500是最大值。已棄用的文檔列出它們,但是如果它們被刪除,我會感到驚訝:https://docs.sonarqube.org/pages/viewpage.action?pageId=2392181 –