use of org.sonarqube.ws.Measures.ComponentTreeWsResponse in project sonarqube by SonarSource.
the class ComponentTreeAction method buildResponse.
private static ComponentTreeWsResponse buildResponse(ComponentTreeRequest request, ComponentTreeData data, Paging paging) {
ComponentTreeWsResponse.Builder response = ComponentTreeWsResponse.newBuilder();
response.getPagingBuilder().setPageIndex(paging.pageIndex()).setPageSize(paging.pageSize()).setTotal(paging.total()).build();
response.setBaseComponent(toWsComponent(data.getBaseComponent(), data.getMeasuresByComponentUuidAndMetric().row(data.getBaseComponent().uuid()), data.getReferenceComponentsByUuid()));
for (ComponentDto componentDto : data.getComponents()) {
response.addComponents(toWsComponent(componentDto, data.getMeasuresByComponentUuidAndMetric().row(componentDto.uuid()), data.getReferenceComponentsByUuid()));
}
if (areMetricsInResponse(request)) {
Measures.Metrics.Builder metricsBuilder = response.getMetricsBuilder();
for (MetricDto metricDto : data.getMetrics()) {
metricsBuilder.addMetrics(metricDtoToWsMetric(metricDto));
}
}
List<String> additionalFields = Optional.ofNullable(request.getAdditionalFields()).orElse(Collections.emptyList());
// backward compatibility
if (additionalFields.contains(DEPRECATED_ADDITIONAL_PERIODS) && data.getPeriod() != null) {
response.getPeriodsBuilder().addPeriods(data.getPeriod());
}
if (additionalFields.contains(ADDITIONAL_PERIOD) && data.getPeriod() != null) {
response.setPeriod(data.getPeriod());
}
return response.build();
}
use of org.sonarqube.ws.Measures.ComponentTreeWsResponse in project sonarqube by SonarSource.
the class ComponentTreeActionTest method pull_request.
@Test
public void pull_request() {
ComponentDto project = db.components().insertPrivateProject();
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
SnapshotDto analysis = db.components().insertSnapshot(branch);
ComponentDto file = db.components().insertComponent(newFileDto(branch));
MetricDto complexity = db.measures().insertMetric(m -> m.setValueType(INT.name()));
LiveMeasureDto measure = db.measures().insertLiveMeasure(file, complexity, m -> m.setValue(12.0d));
ComponentTreeWsResponse response = ws.newRequest().setParam(PARAM_COMPONENT, file.getKey()).setParam(PARAM_PULL_REQUEST, "pr-123").setParam(PARAM_METRIC_KEYS, complexity.getKey()).executeProtobuf(ComponentTreeWsResponse.class);
assertThat(response.getBaseComponent()).extracting(Component::getKey, Component::getPullRequest).containsExactlyInAnyOrder(file.getKey(), "pr-123");
assertThat(response.getBaseComponent().getMeasuresList()).extracting(Measure::getMetric, m -> parseDouble(m.getValue())).containsExactlyInAnyOrder(tuple(complexity.getKey(), measure.getValue()));
}
use of org.sonarqube.ws.Measures.ComponentTreeWsResponse in project sonarqube by SonarSource.
the class ComponentTreeActionTest method return_is_best_value_on_leak_measures.
@Test
public void return_is_best_value_on_leak_measures() {
ComponentDto project = db.components().insertPrivateProject();
db.components().insertSnapshot(project);
userSession.anonymous().addProjectPermission(UserRole.USER, project);
ComponentDto file = newFileDto(project, null);
db.components().insertComponent(file);
MetricDto matchingBestValue = db.measures().insertMetric(m -> m.setKey("new_lines").setValueType(INT.name()).setBestValue(100d));
MetricDto doesNotMatchBestValue = db.measures().insertMetric(m -> m.setKey("new_lines_2").setValueType(INT.name()).setBestValue(100d));
MetricDto noBestValue = db.measures().insertMetric(m -> m.setKey("new_violations").setValueType(INT.name()).setBestValue(null));
db.measures().insertLiveMeasure(file, matchingBestValue, m -> m.setValue(null).setData((String) null).setVariation(100d));
db.measures().insertLiveMeasure(file, doesNotMatchBestValue, m -> m.setValue(null).setData((String) null).setVariation(10d));
db.measures().insertLiveMeasure(file, noBestValue, m -> m.setValue(null).setData((String) null).setVariation(42.0d));
ComponentTreeWsResponse response = ws.newRequest().setParam(PARAM_COMPONENT, project.getKey()).setParam(PARAM_METRIC_KEYS, "new_lines,new_lines_2,new_violations").executeProtobuf(ComponentTreeWsResponse.class);
// file measures
// verify backward compatibility
List<Measure> fileMeasures = response.getComponentsList().get(0).getMeasuresList();
assertThat(fileMeasures).extracting(Measure::getMetric, m -> m.getPeriods().getPeriodsValueList()).containsExactlyInAnyOrder(tuple(matchingBestValue.getKey(), singletonList(PeriodValue.newBuilder().setIndex(1).setValue("100").setBestValue(true).build())), tuple(doesNotMatchBestValue.getKey(), singletonList(PeriodValue.newBuilder().setIndex(1).setValue("10").setBestValue(false).build())), tuple(noBestValue.getKey(), singletonList(PeriodValue.newBuilder().setIndex(1).setValue("42").build())));
assertThat(fileMeasures).extracting(Measure::getMetric, Measure::getPeriod).containsExactlyInAnyOrder(tuple(matchingBestValue.getKey(), PeriodValue.newBuilder().setIndex(1).setValue("100").setBestValue(true).build()), tuple(doesNotMatchBestValue.getKey(), PeriodValue.newBuilder().setIndex(1).setValue("10").setBestValue(false).build()), tuple(noBestValue.getKey(), PeriodValue.newBuilder().setIndex(1).setValue("42").build()));
}
use of org.sonarqube.ws.Measures.ComponentTreeWsResponse in project sonarqube by SonarSource.
the class ComponentTreeActionTest method project_reference_from_portfolio.
@Test
public void project_reference_from_portfolio() {
ComponentDto project = db.components().insertPrivateProject();
ComponentDto view = db.components().insertPrivatePortfolio();
SnapshotDto viewAnalysis = db.components().insertSnapshot(view);
ComponentDto projectCopy = db.components().insertComponent(newProjectCopy(project, view));
MetricDto ncloc = insertNclocMetric();
db.measures().insertLiveMeasure(projectCopy, ncloc, m -> m.setValue(5d));
ComponentTreeWsResponse result = ws.newRequest().setParam(PARAM_COMPONENT, view.getKey()).setParam(PARAM_METRIC_KEYS, ncloc.getKey()).executeProtobuf(ComponentTreeWsResponse.class);
assertThat(result.getComponentsList()).extracting(Component::getKey, Component::getRefKey).containsExactlyInAnyOrder(tuple(projectCopy.getKey(), project.getKey()));
}
use of org.sonarqube.ws.Measures.ComponentTreeWsResponse in project sonarqube by SonarSource.
the class ComponentTreeActionTest method load_measures_multi_sort_with_metric_key_and_paginated.
@Test
public void load_measures_multi_sort_with_metric_key_and_paginated() {
ComponentDto project = db.components().insertPrivateProject();
SnapshotDto projectSnapshot = db.components().insertSnapshot(project);
ComponentDto file9 = db.components().insertComponent(newFileDto(project, null, "file-uuid-9").setName("file-1").setDbKey("file-9-key"));
ComponentDto file8 = db.components().insertComponent(newFileDto(project, null, "file-uuid-8").setName("file-1").setDbKey("file-8-key"));
ComponentDto file7 = db.components().insertComponent(newFileDto(project, null, "file-uuid-7").setName("file-1").setDbKey("file-7-key"));
ComponentDto file6 = db.components().insertComponent(newFileDto(project, null, "file-uuid-6").setName("file-1").setDbKey("file-6-key"));
ComponentDto file5 = db.components().insertComponent(newFileDto(project, null, "file-uuid-5").setName("file-1").setDbKey("file-5-key"));
ComponentDto file4 = db.components().insertComponent(newFileDto(project, null, "file-uuid-4").setName("file-1").setDbKey("file-4-key"));
ComponentDto file3 = db.components().insertComponent(newFileDto(project, null, "file-uuid-3").setName("file-1").setDbKey("file-3-key"));
ComponentDto file2 = db.components().insertComponent(newFileDto(project, null, "file-uuid-2").setName("file-1").setDbKey("file-2-key"));
ComponentDto file1 = db.components().insertComponent(newFileDto(project, null, "file-uuid-1").setName("file-1").setDbKey("file-1-key"));
MetricDto coverage = insertCoverageMetric();
db.commit();
db.measures().insertLiveMeasure(file1, coverage, m -> m.setValue(1.0d));
db.measures().insertLiveMeasure(file2, coverage, m -> m.setValue(2.0d));
db.measures().insertLiveMeasure(file3, coverage, m -> m.setValue(3.0d));
db.measures().insertLiveMeasure(file4, coverage, m -> m.setValue(4.0d));
db.measures().insertLiveMeasure(file5, coverage, m -> m.setValue(5.0d));
db.measures().insertLiveMeasure(file6, coverage, m -> m.setValue(6.0d));
db.measures().insertLiveMeasure(file7, coverage, m -> m.setValue(7.0d));
db.measures().insertLiveMeasure(file8, coverage, m -> m.setValue(8.0d));
db.measures().insertLiveMeasure(file9, coverage, m -> m.setValue(9.0d));
ComponentTreeWsResponse response = ws.newRequest().setParam(PARAM_COMPONENT, project.getKey()).setParam(SORT, NAME_SORT + ", " + METRIC_SORT).setParam(PARAM_METRIC_SORT, "coverage").setParam(PARAM_METRIC_KEYS, "coverage").setParam(PARAM_STRATEGY, "leaves").setParam(PARAM_QUALIFIERS, "FIL,UTS").setParam(Param.PAGE, "2").setParam(Param.PAGE_SIZE, "3").executeProtobuf(ComponentTreeWsResponse.class);
assertThat(response.getComponentsList()).extracting("key").containsExactly("file-4-key", "file-5-key", "file-6-key");
assertThat(response.getPaging().getPageIndex()).isEqualTo(2);
assertThat(response.getPaging().getPageSize()).isEqualTo(3);
assertThat(response.getPaging().getTotal()).isEqualTo(9);
}
Aggregations