From 579871c5226580deb0cd539fd8d317836046e1ab Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Mon, 18 Feb 2019 10:01:41 -0600 Subject: [PATCH] Minor fixes --- .../issue/FillComponentIssuesVisitorRule.java | 5 --- .../server/measure/ws/ComponentAction.java | 36 ++++--------------- .../measure/ws/ComponentTreeAction.java | 26 +++++++------- .../server/measure/ws/ComponentTreeData.java | 14 ++++---- ...eriods.java => SnapshotDtoToWsPeriod.java} | 17 ++++----- 5 files changed, 34 insertions(+), 64 deletions(-) rename server/sonar-server/src/main/java/org/sonar/server/measure/ws/{SnapshotDtoToWsPeriods.java => SnapshotDtoToWsPeriod.java} (81%) diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/FillComponentIssuesVisitorRule.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/FillComponentIssuesVisitorRule.java index 77a5775e411..529ca5f54ef 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/FillComponentIssuesVisitorRule.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/FillComponentIssuesVisitorRule.java @@ -65,11 +65,6 @@ public class FillComponentIssuesVisitorRule extends TypeAwareVisitorAdapter impl }; } - public void setIssues(Component component, DefaultIssue... issues) { - checkNotNull(component, "component cannot be null"); - setIssues(component.getReportAttributes().getRef(), issues); - } - public void setIssues(int componentRef, DefaultIssue... issues) { Component component = treeRootHolder.getComponentByRef(componentRef); checkArgument(component != null, String.format("Component '%s' does not exists in the report ", componentRef)); diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentAction.java b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentAction.java index d0dd316830c..042f746c35d 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentAction.java @@ -76,7 +76,7 @@ import static org.sonar.server.measure.ws.MeasuresWsParametersBuilder.createAddi import static org.sonar.server.measure.ws.MeasuresWsParametersBuilder.createDeveloperParameters; import static org.sonar.server.measure.ws.MeasuresWsParametersBuilder.createMetricKeysParameter; import static org.sonar.server.measure.ws.MetricDtoToWsMetric.metricDtoToWsMetric; -import static org.sonar.server.measure.ws.SnapshotDtoToWsPeriods.snapshotToWsPeriods; +import static org.sonar.server.measure.ws.SnapshotDtoToWsPeriod.snapshotToWsPeriods; import static org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001; import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001; import static org.sonar.server.ws.KeyExamples.KEY_PULL_REQUEST_EXAMPLE_001; @@ -154,10 +154,10 @@ public class ComponentAction implements MeasuresWsAction { checkPermissions(component); SnapshotDto analysis = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(dbSession, component.projectUuid()).orElse(null); List metrics = searchMetrics(dbSession, request); - List periods = snapshotToWsPeriods(analysis); + Optional period = snapshotToWsPeriods(analysis); List measures = searchMeasures(dbSession, component, metrics); - return buildResponse(request, component, refComponent, measures, metrics, periods); + return buildResponse(request, component, refComponent, measures, metrics, period); } } @@ -183,8 +183,8 @@ public class ComponentAction implements MeasuresWsAction { return dbClient.componentDao().selectByUuid(dbSession, component.getCopyResourceUuid()); } - private static ComponentWsResponse buildResponse(ComponentRequest request, ComponentDto component, Optional refComponent, List measures, - List metrics, List periods) { + private static ComponentWsResponse buildResponse(ComponentRequest request, ComponentDto component, Optional refComponent, + List measures, List metrics, Optional period) { ComponentWsResponse.Builder response = ComponentWsResponse.newBuilder(); Map metricsById = Maps.uniqueIndex(metrics, MetricDto::getId); Map measuresByMetric = new HashMap<>(); @@ -205,8 +205,8 @@ public class ComponentAction implements MeasuresWsAction { response.getMetricsBuilder().addMetrics(metricDtoToWsMetric(metric)); } } - if (additionalFields.contains(ADDITIONAL_PERIODS)) { - response.getPeriodsBuilder().addAllPeriods(periods); + if (additionalFields.contains(ADDITIONAL_PERIODS) && period.isPresent()) { + response.getPeriodsBuilder().addPeriods(period.get()); } } @@ -282,8 +282,6 @@ public class ComponentAction implements MeasuresWsAction { private String pullRequest; private List metricKeys; private List additionalFields; - private String developerId; - private String developerKey; /** * @deprecated since 6.6, please use {@link #getComponent()} instead @@ -351,25 +349,5 @@ public class ComponentAction implements MeasuresWsAction { this.additionalFields = additionalFields; return this; } - - @CheckForNull - private String getDeveloperId() { - return developerId; - } - - private ComponentRequest setDeveloperId(@Nullable String developerId) { - this.developerId = developerId; - return this; - } - - @CheckForNull - private String getDeveloperKey() { - return developerKey; - } - - private ComponentRequest setDeveloperKey(@Nullable String developerKey) { - this.developerKey = developerKey; - return this; - } } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeAction.java b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeAction.java index 0af53195acd..dd5a447e4c5 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeAction.java @@ -107,12 +107,12 @@ import static org.sonar.server.measure.ws.MeasuresWsParametersBuilder.createAddi import static org.sonar.server.measure.ws.MeasuresWsParametersBuilder.createDeveloperParameters; import static org.sonar.server.measure.ws.MeasuresWsParametersBuilder.createMetricKeysParameter; import static org.sonar.server.measure.ws.MetricDtoToWsMetric.metricDtoToWsMetric; -import static org.sonar.server.measure.ws.SnapshotDtoToWsPeriods.snapshotToWsPeriods; +import static org.sonar.server.measure.ws.SnapshotDtoToWsPeriod.snapshotToWsPeriods; import static org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001; import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001; import static org.sonar.server.ws.KeyExamples.KEY_PULL_REQUEST_EXAMPLE_001; -import static org.sonar.server.ws.WsParameterBuilder.createQualifiersParameter; import static org.sonar.server.ws.WsParameterBuilder.QualifierParameterContext.newQualifierParameterContext; +import static org.sonar.server.ws.WsParameterBuilder.createQualifiersParameter; import static org.sonar.server.ws.WsUtils.checkRequest; import static org.sonar.server.ws.WsUtils.writeProtobuf; @@ -121,15 +121,15 @@ import static org.sonar.server.ws.WsUtils.writeProtobuf; * To limit the number of rows in database, a best value algorithm exists in database.

* A measure is not stored in database if: *
    - *
  • the component is a file (production or test)
  • - *
  • optimization algorithm is enabled on the metric
  • - *
  • the measure computed equals the metric best value
  • - *
  • the period values are all equal to 0
  • + *
  • the component is a file (production or test)
  • + *
  • optimization algorithm is enabled on the metric
  • + *
  • the measure computed equals the metric best value
  • + *
  • the period values are all equal to 0
  • *
* To recreate a best value 2 different cases: *
    - *
  • Metric starts with 'new_' (ex: new_violations): the best value measure doesn't have a value and period values are all equal to 0
  • - *
  • Other metrics: the best value measure has a value of 0 and no period value
  • + *
  • Metric starts with 'new_' (ex: new_violations): the best value measure doesn't have a value and period values are all equal to 0
  • + *
  • Other metrics: the best value measure has a value of 0 and no period value
  • *
*/ public class ComponentTreeAction implements MeasuresWsAction { @@ -177,8 +177,8 @@ public class ComponentTreeAction implements MeasuresWsAction { public void define(WebService.NewController context) { WebService.NewAction action = context.createAction(ACTION_COMPONENT_TREE) .setDescription(format("Navigate through components based on the chosen strategy with specified measures. The %s or the %s parameter must be provided.
" + - "Requires the following permission: 'Browse' on the specified project.
" + - "When limiting search with the %s parameter, directories are not returned.", + "Requires the following permission: 'Browse' on the specified project.
" + + "When limiting search with the %s parameter, directories are not returned.", DEPRECATED_PARAM_BASE_COMPONENT_ID, PARAM_COMPONENT, Param.TEXT_QUERY)) .setResponseExample(getClass().getResource("component_tree-example.json")) .setSince("5.4") @@ -316,8 +316,8 @@ public class ComponentTreeAction implements MeasuresWsAction { } } - if (arePeriodsInResponse(request)) { - response.getPeriodsBuilder().addAllPeriods(data.getPeriods()); + if (arePeriodsInResponse(request) && data.getPeriod() != null) { + response.getPeriodsBuilder().addPeriods(data.getPeriod()); } return response.build(); @@ -431,7 +431,7 @@ public class ComponentTreeAction implements MeasuresWsAction { .setComponentCount(componentCount) .setMeasuresByComponentUuidAndMetric(measuresByComponentUuidAndMetric) .setMetrics(metrics) - .setPeriods(snapshotToWsPeriods(baseSnapshot.get())) + .setPeriod(snapshotToWsPeriods(baseSnapshot.get()).orElse(null)) .setReferenceComponentsByUuid(searchReferenceComponentsById(dbSession, components)) .build(); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeData.java b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeData.java index 4b963673c70..6f01434455a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeData.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeData.java @@ -39,7 +39,7 @@ class ComponentTreeData { private final int componentCount; private final Map referenceComponentsByUuid; private final List metrics; - private final List periods; + private final Measures.Period period; private final Table measuresByComponentUuidAndMetric; private ComponentTreeData(Builder builder) { @@ -49,7 +49,7 @@ class ComponentTreeData { this.referenceComponentsByUuid = builder.referenceComponentsByUuid; this.metrics = builder.metrics; this.measuresByComponentUuidAndMetric = builder.measuresByComponentUuidAndMetric; - this.periods = builder.periods; + this.period = builder.period; } public ComponentDto getBaseComponent() { @@ -77,8 +77,8 @@ class ComponentTreeData { } @CheckForNull - List getPeriods() { - return periods; + Measures.Period getPeriod() { + return period; } @CheckForNull @@ -96,7 +96,7 @@ class ComponentTreeData { private Map referenceComponentsByUuid; private int componentCount; private List metrics; - private List periods; + private Measures.Period period; private Table measuresByComponentUuidAndMetric; private Builder() { @@ -123,8 +123,8 @@ class ComponentTreeData { return this; } - public Builder setPeriods(List periods) { - this.periods = periods; + public Builder setPeriod(@Nullable Measures.Period period) { + this.period = period; return this; } diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/SnapshotDtoToWsPeriods.java b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/SnapshotDtoToWsPeriod.java similarity index 81% rename from server/sonar-server/src/main/java/org/sonar/server/measure/ws/SnapshotDtoToWsPeriods.java rename to server/sonar-server/src/main/java/org/sonar/server/measure/ws/SnapshotDtoToWsPeriod.java index 26e5fce0d5b..1143d7dc4a8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/SnapshotDtoToWsPeriods.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/SnapshotDtoToWsPeriod.java @@ -19,31 +19,28 @@ */ package org.sonar.server.measure.ws; -import java.util.ArrayList; -import java.util.List; +import java.util.Optional; import javax.annotation.Nullable; import org.sonar.db.component.SnapshotDto; import org.sonarqube.ws.Measures; -import static java.util.Collections.emptyList; import static org.sonar.api.utils.DateUtils.formatDateTime; -class SnapshotDtoToWsPeriods { - private SnapshotDtoToWsPeriods() { +class SnapshotDtoToWsPeriod { + private SnapshotDtoToWsPeriod() { // prevent instantiation } - static List snapshotToWsPeriods(@Nullable SnapshotDto snapshot) { + static Optional snapshotToWsPeriods(@Nullable SnapshotDto snapshot) { if (snapshot == null) { - return emptyList(); + return Optional.empty(); } - List periods = new ArrayList<>(); if (snapshot.getPeriodDate() != null) { - periods.add(snapshotDtoToWsPeriod(snapshot)); + return Optional.of(snapshotDtoToWsPeriod(snapshot)); } - return periods; + return Optional.empty(); } private static Measures.Period snapshotDtoToWsPeriod(SnapshotDto snapshot) { -- 2.39.5