@VisibleForTesting WsConnector wsConnector() { return target.wsConnector(); }
private Issues.SearchWsResponse searchIssues(String componentKey, String branch, int page) { SearchRequest searchRequest = new SearchRequest().setComponentKeys(Collections.singletonList(componentKey)).setP(String.valueOf(page)).setResolved("false"); if (isNotBlankAndNotEmpty(branch)) { searchRequest.setBranch(branch); } return wsClient.issues().search(searchRequest); }
private MeasureHolder retrieveMeasureHolder(final WsClient wsClient, final String key, final String metric) { MeasureHolder measureHolder; final ComponentWsRequest componentWsRequest = new ComponentWsRequest(); componentWsRequest.setComponentKey(key); componentWsRequest.setMetricKeys(ImmutableList.of(metric)); final ComponentWsResponse componentWsResponse = wsClient.measures() .component(componentWsRequest); final List<Measure> measures = componentWsResponse.getComponent() .getMeasuresList(); if (measures.isEmpty()) { measureHolder = new MeasureHolder(metric); } else { measureHolder = new MeasureHolder(measures.get(0)); } return measureHolder; }
private File toFile(Issues.Component component, String branch) { ShowRequest showRequest = new ShowRequest().setComponent(component.getKey()); if (isNotBlankAndNotEmpty(branch)) { showRequest.setBranch(branch); } Components.ShowWsResponse showWsResponse = wsClient.components().show(showRequest); StringBuilder sb = new StringBuilder(component.getPath()); for (Components.Component a : showWsResponse.getAncestorsList()) { if (Qualifiers.MODULE.equals(a.getQualifier()) && a.getPath() != null) { sb.insert(0, a.getPath() + File.separator); } } return new File(sb.toString()); }
final ProjectStatusWsRequest wsRequest = new ProjectStatusWsRequest(); wsRequest.setProjectKey(key); final ProjectStatusWsResponse wsResponse = wsClient.qualityGates() .projectStatus(wsRequest); status = QualityGateBadge.valueOf(wsResponse.getProjectStatus()
private Ce.Task getTask(String ceTaskId) { Ce.TaskResponse taskResponse = wsClient.ce().task(new TaskRequest().setId(ceTaskId)); return taskResponse.getTask(); }
public Integer issueCount(String severity, String rule) { return wsClient.issues().search(new SearchWsRequest() .setSeverities(Arrays.asList(severity)) .setRules(Arrays.asList(rule))) .getIssuesCount(); }
@CheckForNull static Measure getMeasure(String componentKey, String metricKey) { WsMeasures.ComponentWsResponse response = newWsClient().measures().component(new ComponentWsRequest() .setComponent(componentKey) .setMetricKeys(singletonList(metricKey))); List<Measure> measures = response.getComponent().getMeasuresList(); return measures.size() == 1 ? measures.get(0) : null; }
public List<Component> getAllProjects(WsClient sonarClient) { org.sonarqube.ws.client.component.SearchWsRequest query = new org.sonarqube.ws.client.component.SearchWsRequest() .setQualifiers(singletonList(SonarQualifier.PROJECT.getQualifier())) .setPageSize(500); //-1 is not allowed, neither int max. The limit is 500. List<WsComponents.Component> components = new ArrayList<>(); WsComponents.SearchWsResponse response = sonarClient.components().search(query); Common.Paging paging = response.getPaging(); int total = paging.getTotal(); int pageSize = paging.getPageSize(); int pages = total / pageSize + (total % pageSize > 0 ? 1 : 0); if (total > 0) { components.addAll(response.getComponentsList()); for (int pageIndex = 2; pageIndex <= pages; pageIndex++) { query.setPage(pageIndex); response = sonarClient.components().search(query); components.addAll(response.getComponentsList()); } } return Collections.unmodifiableList(components); }
@VisibleForTesting void checkQualityGate(WsClient wsClient, String analysisId) { LOGGER.debug("Requesting quality gate status for analysisId {}", analysisId); ProjectStatusWsResponse projectStatusResponse = wsClient .qualityGates() .projectStatus(new ProjectStatusWsRequest().setAnalysisId(analysisId)); ProjectStatus projectStatus = projectStatusResponse.getProjectStatus(); Status status = projectStatus.getStatus(); LOGGER.info("Quality gate status: {}", status); int errors = 0; if (Status.ERROR.equals(status) || Status.WARN.equals(status)) { errors = logConditions(projectStatus.getConditionsList()); } if (Status.ERROR.equals(status)) { LOGGER.error("{} Project did not meet {} conditions", BuildBreakerPlugin.LOG_STAMP, errors); throw new IllegalStateException("Project does not pass the quality gate."); } }
wsRequest.setQuery(key); wsRequest.setOnlyCurrents(true); final CeService ceService = wsClient.ce(); final ActivityResponse activityResponse = ceService.activity(wsRequest); if (activityResponse.getTasksCount() >= 1) {
public String baseUrl() { return target.wsConnector().baseUrl(); }
public ImmutableList<Issue> getAllIssuesFor(String resourceKey) { final ImmutableList.Builder<Issue> builder = ImmutableList.builder(); SearchWsRequest query = new SearchWsRequest(); query.setComponentRoots(singletonList(resourceKey)) .setResolved(false) .setPageSize(-1); IssuesService issuesService = sonarClient.issues(); SearchWsResponse response = issuesService.search(query); builder.addAll(response.getIssuesList()); Common.Paging paging = response.getPaging(); Integer total = paging.getTotal(); Integer pageSize = paging.getPageSize(); Integer pages = total / pageSize + (total % pageSize > 0 ? 1 : 0); for (int pageIndex = 2; pageIndex <= pages; pageIndex++) { final ProgressIndicator progressIndicator = ProgressManager.getInstance().getProgressIndicator(); if (progressIndicator.isCanceled()) break; final String pagesProgressMessage = String.format("%d / %d pages downloaded", pageIndex, pages); ProgressIndicatorUtil.setText(progressIndicator, pagesProgressMessage); ProgressIndicatorUtil.setFraction(progressIndicator, pageIndex * 1.0 / pages); query.setPage(pageIndex); builder.addAll(issuesService.search(query).getIssuesList()); } return builder.build(); } }
private Measure getMeasure(String componentKey, String metricKey) { WsMeasures.ComponentWsResponse response = wsClient.measures().component(new ComponentWsRequest() .setComponent(componentKey) .setMetricKeys(Arrays.asList(metricKey))); List<Measure> measures = response.getComponent().getMeasuresList(); return measures.size() == 1 ? measures.get(0) : null; }
public List<Component> getAllModules(WsClient sonarClient, String projectResourceId) { TreeWsRequest query = new TreeWsRequest() .setQualifiers(singletonList(SonarQualifier.MODULE.getQualifier())) .setBaseComponentId(projectResourceId); return sonarClient.components().tree(query).getComponentsList(); }
private MeasureHolder applyQualityGateTreshold(final WsClient wsClient, final String key, final String metric, final MeasureHolder measureHolder) { final ProjectStatusWsRequest projectStatusWsRequest = new ProjectStatusWsRequest(); projectStatusWsRequest.setProjectKey(key); final ProjectStatusWsResponse projectStatusWsResponse = wsClient.qualityGates() .projectStatus(projectStatusWsRequest); final List<Condition> conditions = projectStatusWsResponse.getProjectStatus() .getConditionsList(); for (final Condition condition : conditions) { if (metric.equals(condition.getMetricKey())) { final Status status = condition.getStatus(); switch (status) { case ERROR: measureHolder.setBackgroundColor(SVGImageColor.RED); break; case OK: measureHolder.setBackgroundColor(SVGImageColor.GREEN); break; case WARN: measureHolder.setBackgroundColor(SVGImageColor.ORANGE); break; default: measureHolder.setBackgroundColor(SVGImageColor.GRAY); break; } } } return measureHolder; } }
@Test public void create_local_client() { LocalConnector connector = mock(LocalConnector.class); WsClient client = WsClientFactories.getLocal().newClient(connector); assertThat(client).isInstanceOf(DefaultWsClient.class); assertThat(client.wsConnector()).isInstanceOf(LocalWsConnector.class); assertThat(((LocalWsConnector) client.wsConnector()).localConnector()).isSameAs(connector); } }
/** * If an exception is not thrown, the response needs to be closed by either calling close() directly, or closing the * body content's stream/reader. * * @throws IllegalStateException if the request could not be executed due to a connectivity problem or timeout. Because networks can * fail during an exchange, it is possible that the remote server accepted the request before the failure * @throws MessageException if there was a problem with authentication or if a error message was parsed from the response. * @throws HttpException if the response code is not in range [200..300). Consider using {@link #createErrorMessage(HttpException)} to create more relevant messages for the users. */ public WsResponse call(WsRequest request) { Preconditions.checkState(!globalMode.isMediumTest(), "No WS call should be made in medium test mode"); Profiler profiler = Profiler.createIfDebug(LOG).start(); WsResponse response = target.wsConnector().call(request); profiler.stopDebug(format("%s %d %s", request.getMethod(), response.code(), response.requestUrl())); failIfUnauthorized(response); return response; }
@Test public void log_and_profile_request_if_debug_level() { WsRequest request = newRequest(); WsResponse response = newResponse().setRequestUrl("https://local/api/issues/search"); when(wsClient.wsConnector().call(request)).thenReturn(response); logTester.setLevel(LoggerLevel.DEBUG); ScannerWsClient underTest = new ScannerWsClient(wsClient, false, new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap()))); WsResponse result = underTest.call(request); // do not fail the execution -> interceptor returns the response assertThat(result).isSameAs(response); // check logs List<String> debugLogs = logTester.logs(LoggerLevel.DEBUG); assertThat(debugLogs).hasSize(1); assertThat(debugLogs.get(0)).contains("GET 200 https://local/api/issues/search | time="); }
@Test public void create_http_client() { HttpConnector connector = HttpConnector.newBuilder().url("http://localhost:9000").build(); WsClient client = WsClientFactories.getDefault().newClient(connector); assertThat(client).isInstanceOf(DefaultWsClient.class); assertThat(client.wsConnector()).isSameAs(connector); }