SonarQube には、
API で データを 抽出したい 動機
SonarQube version 6.2 以降 widget が
[1] しっかり
[2] 削除理由はUser-needs oriented spaces
に
参考
Sonar Web APIを
使ってみました - Qiita
Java からHttpRequest で Web API を 叩く 際の 参考に なります。 Using the Web Service Java client - SonarQube-4.5 - Doc SonarQube
過去 Version のドキュメント 現状の API とは IF が 違います。
Java client に ついて
ライブラリ名称
version 5.1 まで
version 5.1 まで
Maven Repository: org.codehaus.sonar » sonar-ws-clientversion 5.2 以降
Maven Repository: org.sonarsource.sonarqube » sonar-ws
クライアントの 実装方式
クライアントの
また、
- エラー内容
Error:(28, 40) java: com.google.protobuf.GeneratedMessageにアクセスできません com.google.protobuf.GeneratedMessageのクラス・ファイルが見つかりません Error:(33, 49) java: com.google.protobuf.MessageOrBuilderにアクセスできません com.google.protobuf.MessageOrBuilderのクラス・ファイルが見つかりません
[3] ドキュメントが
[4] 挙動的には
動作確認した 環境
以下の
OS
% sw_vers ProductName: Mac OS X ProductVersion: 10.12.6 BuildVersion: 16G29
java の
version % java -version java version "1.8.0_45" Java(TM) SE Runtime Environment (build 1.8.0_45-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)
gradle の
version % gradle -v ------------------------------------------------------------ Gradle 4.1 ------------------------------------------------------------ Build time: 2017-08-07 14:38:48 UTC Revision: 941559e020f6c357ebb08d5c67acdb858a3defc2 Groovy: 2.4.11 Ant: Apache Ant(TM) version 1.9.6 compiled on June 29 2015 JVM: 1.8.0_45 (Oracle Corporation 25.45-b02) OS: Mac OS X 10.12.6 x86_64
SonarQube の
Version
SonarQube 6.5
作成した サンプル
作成した
また、localhost:9000
で
他 host で
build.gradle
/* * This build file was generated by the Gradle 'init' task. * * This generated file contains a sample Java Library project to get you started. * For more details take a look at the Java Libraries chapter in the Gradle * user guide available at https://docs.gradle.org/4.1/userguide/java_library_plugin.html */ // Apply the java-library plugin to add support for Java Library apply plugin: 'java-library' // In this section you declare where to find the dependencies of your project repositories { // Use jcenter for resolving your dependencies. // You can declare any Maven/Ivy/file repository here. jcenter() } dependencies { // This dependency is exported to consumers, that is to say found on their compile classpath. api 'org.apache.commons:commons-math3:3.6.1' // This dependency is used internally, and not exposed to consumers on their own compile classpath. implementation 'com.google.guava:guava:22.0' // https://mvnrepository.com/artifact/org.sonarsource.sonarqube/sonar-ws implementation 'org.sonarsource.sonarqube:sonar-ws:6.5' // https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 implementation 'org.apache.commons:commons-lang3:3.6' testImplementation 'junit:junit:4.12' } test { testLogging { events "passed", "skipped", "failed", "standardOut", "standardError" } }
説明 sonar-ws に
ついて が、// https://mvnrepository.com/artifact/org.sonarsource.sonarqube/sonar-ws implementation 'org.sonarsource.sonarqube:sonar-ws:6.5'
sonar-ws java client の ライブラリに なります。 説明 戻り
値の 出力 に ついて に// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 implementation 'org.apache.commons:commons-lang3:3.6'
含まれる ToStringBuilder
を、標準出力に 戻り値を 出力する ために 使用しています。 SearchIssues.java
import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import org.sonarqube.ws.Common; import org.sonarqube.ws.Issues; import org.sonarqube.ws.client.HttpConnector; import org.sonarqube.ws.client.WsClient; import org.sonarqube.ws.client.WsClientFactories; import org.sonarqube.ws.client.issue.SearchWsRequest; import java.util.List; /* * This Java source file was generated by the Gradle 'init' task. */ public class SearchIssues { /** * Main method.. * * @param args */ public static void main(String... args) { // execute search Issues HttpConnector httpConnector = HttpConnector.newBuilder().url("http://localhost:9000").credentials("admin", "admin").token("your_token").build(); WsClient wsClient = WsClientFactories.getDefault().newClient(httpConnector); SearchWsRequest searchWsRequest = new SearchWsRequest(); Issues.SearchWsResponse response = wsClient.issues().search(searchWsRequest); // STD Out Paging Object Common.Paging paging = response.getPaging(); System.out.println("------------------------------------------------------------"); System.out.println(ToStringBuilder.reflectionToString(paging, ToStringStyle.JSON_STYLE)); System.out.println("------------------------------------------------------------"); // STD Out issue List<Issues.Issue> issuesList = response.getIssuesList(); for (Issues.Issue issue : issuesList) { System.out.println("------------------------------------------------------------"); System.out.println(ToStringBuilder.reflectionToString(issue, ToStringStyle.JSON_STYLE)); System.out.println("------------------------------------------------------------"); } } }
説明 認証に
ついて で、HttpConnector httpConnector = HttpConnector.newBuilder().url("http://localhost:9000").credentials("admin", "admin").token("your_token").build();
認証情報を 生成しています。 credentials
の設定、 token
の設定は どちらかを 実施すれば、 認証は 通ります。
token
の生成に ついては、 SonarQube Web API を python から 実行する | Monotalk で 実施しています。 生成が 必要であればご 確認ください。 説明
response.getPaging()
の戻り値に ついて
戻り値の 標準出力の 結果は 以下の 通りです。 {"bitField0_":7,"pageIndex_":1,"pageSize_":100,"total_":734,"memoizedIsInitialized":-1,"unknownFields":"","memoizedSize":-1,"memoizedHashCode":0}
"total_":734
が、issue の 総数を 示しています。
"pageSize_":100
で、戻りに 含まれる issue数、 "pageIndex_":1
で何ページ目なのかが 返ります。
全てのissue を 取得する 場合は、 この 戻り値を 元に、 SearchWsRequest
に値を 設定し、 loop を 回す 必要が あります。 説明
SearchWsRequest
について
パッケージ違いでSearchWsRequest
が存在します。 叩く API ごとに、 使用する SearchWsRequest
が違いますので、 ご注意ください。 SearchIssuesTest.java
import org.junit.Test; public class SearchIssuesTest { @Test public void main() throws Exception { String arg = null; SearchIssues.main(arg); } }
説明 実行方法
以下で、テストクラス経由で 実行可能です。 gradle 経由ではない./gradlew clean test
場合、 IDE から メインメソッドを 実行していただければよいかと 思います。
データ取得は
作成した
SonarQube 6.5 sonar-ws example
以上です。
コメント