以前、 SonarQubeデータベースからカスタムデータをエクスポートする方法 について質問しましたが、SonarチームはWeb APIを使用することを提案しました。
いくつかの研究の後、私はまだWeb APIの使用方法に苦労しています。 (私はWeb APIがどのように機能するかについて非常に不慣れです)
これを読んだ後 post 、私はJavaコードを使用してそれを行うことができることに気付きます。走る
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));
}
}
}
_
ヘルプやガイダンスに感謝します!
SonarQube Web APIは、SonarQubeのドキュメントに従って、/api
コンテキストパスの下に、セクションと操作(欠落しているように見える)とともに存在します。
例として、ポート9000で実行されているlocalhostで問題を検索するには、GET
をhttp://localhost:9000/api/issues/search?pageSize=500&componentKeys=YOUR_COMPONENT
に送信し、JSON応答を解析します。
認可も必要になる場合があります。これは、BASICユーザー名パスワードコンボ、またはWebクライアント経由で取得できるアクセストークンとして送信されます。
http://localhost:9000/web_api/
は、Sonarqubeバージョンでの有用性、パラメーターの説明、応答の例、トレースの変更を提供します。