我需要示例java客户端代码来访问声纳Qube中的项目问题吗?
我将项目密钥作为输入传递,并获取输出中的问题列表。
答案 0 :(得分:1)
你可以使用sonarqube client lib:
Builder builder = HttpConnector.newBuilder();
builder.url("http://xxxx:9000/sonar/");
builder.connectTimeoutMilliseconds(10000);
HttpConnector httpConnector = builder.build();
SearchWsRequest searchWsRequest = new org.sonarqube.ws.client.issue.SearchWsRequest();
List<String> projectKeys = new ArrayList<String>(1);
projectKeys.add("project_key");
searchWsRequest.setProjectKeys(projectKeys);
final WsClient wsClient = WsClientFactories.getDefault().newClient(httpConnector);
List<Issue> issues = wsClient.issues().search(searchWsRequest).getIssuesList();
并使用
<dependency>
<groupId>org.sonarsource.sonarqube</groupId>
<artifactId>sonar-ws</artifactId>
<version>5.6</version>
</dependency>
答案 1 :(得分:1)
Sonarqube 8.2.blarghminorversion的更新源代码。只需稍作更改即可使用Maxence Lecointe提供的出色示例...
import java.util.ArrayList;
import java.util.List;
import org.sonarqube.ws.Issues.Issue;
import org.sonarqube.ws.Issues.SearchWsResponse;
import org.sonarqube.ws.client.HttpConnector;
import org.sonarqube.ws.client.HttpConnector.Builder;
import org.sonarqube.ws.client.WsClient;
import org.sonarqube.ws.client.WsClientFactories;
import org.sonarqube.ws.client.issues.SearchRequest;
public class SimpleClient {
public static void main(String[] args) {
Builder builder = HttpConnector.newBuilder();
builder.url("http://xxxx:9000/");
builder.connectTimeoutMilliseconds(10000);
HttpConnector httpConnector = builder.build();
SearchRequest searchWsRequest = new org.sonarqube.ws.client.issues.SearchRequest();
List<String> componentKeys = new ArrayList<String>(1);
componentKeys.add("project_key");
searchWsRequest.setComponentKeys(componentKeys);
final WsClient wsClient = WsClientFactories.getDefault().newClient(httpConnector);
SearchWsResponse response = wsClient.issues().search(searchWsRequest);
System.out.println("Total found: " + response.getTotal());
List<Issue> issues = wsClient.issues().search(searchWsRequest).getIssuesList();
for (Issue issue : issues) {
System.out.println("===================================");
System.out.println(issue.toString());
}
}
}
<dependency>
<groupId>org.sonarsource.sonarqube</groupId>
<artifactId>sonar-ws</artifactId>
<version>8.2.0.32929</version>
</dependency>