From 413f576affaeb64c30fac088d97ebef8d9e24e1a Mon Sep 17 00:00:00 2001 From: Zipeng WU Date: Thu, 9 Mar 2023 16:16:52 +0100 Subject: [PATCH] SONAR-18647 Remove parameter period in api/measures endpoints --- .../component/ws/MeasuresWsParameters.java | 8 +-- .../server/measure/ws/ComponentAction.java | 8 +-- .../measure/ws/ComponentTreeAction.java | 9 +-- .../measure/ws/MeasureDtoToWsMeasure.java | 2 - .../server/measure/ws/search-example.json | 57 ++++++++++--------- .../measure/ws/ComponentActionTest.java | 20 ++++--- .../measure/ws/ComponentTreeActionTest.java | 19 ++----- .../server/measure/ws/SearchActionTest.java | 4 +- sonar-ws/src/main/protobuf/ws-measures.proto | 16 +----- 9 files changed, 56 insertions(+), 87 deletions(-) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/MeasuresWsParameters.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/MeasuresWsParameters.java index 8b647b05344..6ac47d18f69 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/MeasuresWsParameters.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/MeasuresWsParameters.java @@ -48,15 +48,9 @@ public class MeasuresWsParameters { public static final String ADDITIONAL_METRICS = "metrics"; - /** - * @deprecated since 8.1 - */ - @Deprecated - public static final String DEPRECATED_ADDITIONAL_PERIODS = "periods"; - public static final String ADDITIONAL_PERIOD = "period"; - public static final Set ADDITIONAL_FIELDS = ImmutableSortedSet.of(ADDITIONAL_METRICS, DEPRECATED_ADDITIONAL_PERIODS, ADDITIONAL_PERIOD); + public static final Set ADDITIONAL_FIELDS = ImmutableSortedSet.of(ADDITIONAL_METRICS, ADDITIONAL_PERIOD); private MeasuresWsParameters() { // static constants only diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/ComponentAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/ComponentAction.java index 0c10d0e3923..95a9d4d715a 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/ComponentAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/ComponentAction.java @@ -59,7 +59,6 @@ import static java.util.Collections.singletonMap; import static org.sonar.server.component.ws.MeasuresWsParameters.ACTION_COMPONENT; import static org.sonar.server.component.ws.MeasuresWsParameters.ADDITIONAL_METRICS; import static org.sonar.server.component.ws.MeasuresWsParameters.ADDITIONAL_PERIOD; -import static org.sonar.server.component.ws.MeasuresWsParameters.DEPRECATED_ADDITIONAL_PERIODS; import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_ADDITIONAL_FIELDS; import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_BRANCH; import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_COMPONENT; @@ -97,6 +96,8 @@ public class ComponentAction implements MeasuresWsAction { .setResponseExample(getClass().getResource("component-example.json")) .setSince("5.4") .setChangelog( + new Change("10.0", "the response field periods under measures field is removed."), + new Change("10.0", "the option `periods` of 'additionalFields' request field is removed."), new Change("9.3", "When the new code period is set to 'reference branch', the response field 'date' under the 'period' field has been removed"), new Change("9.3", format("The use of the following metrics in 'metricKeys' parameter is deprecated: %s", MeasuresWsModule.getDeprecatedMetrics())), @@ -271,11 +272,6 @@ public class ComponentAction implements MeasuresWsAction { } } - // backward compatibility - if (additionalFields.contains(DEPRECATED_ADDITIONAL_PERIODS) && period.isPresent()) { - response.getPeriodsBuilder().addPeriods(period.get()); - } - if (additionalFields.contains(ADDITIONAL_PERIOD) && period.isPresent()) { response.setPeriod(period.get()); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/ComponentTreeAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/ComponentTreeAction.java index 4e5eabfa263..3c65f7c95ef 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/ComponentTreeAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/ComponentTreeAction.java @@ -85,7 +85,6 @@ import static org.sonar.db.component.ComponentTreeQuery.Strategy.LEAVES; import static org.sonar.server.component.ws.MeasuresWsParameters.ACTION_COMPONENT_TREE; import static org.sonar.server.component.ws.MeasuresWsParameters.ADDITIONAL_METRICS; import static org.sonar.server.component.ws.MeasuresWsParameters.ADDITIONAL_PERIOD; -import static org.sonar.server.component.ws.MeasuresWsParameters.DEPRECATED_ADDITIONAL_PERIODS; import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_ADDITIONAL_FIELDS; import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_BRANCH; import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_COMPONENT; @@ -106,8 +105,8 @@ import static org.sonar.server.measure.ws.SnapshotDtoToWsPeriod.snapshotToWsPeri 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.writeProtobuf; /** @@ -179,6 +178,8 @@ public class ComponentTreeAction implements MeasuresWsAction { .setHandler(this) .addPagingParams(100, MAX_SIZE) .setChangelog( + new Change("10.0", "the response field periods under measures field is removed."), + new Change("10.0", "the option `periods` of 'additionalFields' request field is removed."), new Change("9.3", format("The use of the following metrics in 'metricKeys' parameter is deprecated: %s", MeasuresWsModule.getDeprecatedMetrics())), new Change("8.8", "parameter 'component' is now required"), @@ -317,10 +318,6 @@ public class ComponentTreeAction implements MeasuresWsAction { } List 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()); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/MeasureDtoToWsMeasure.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/MeasureDtoToWsMeasure.java index f9d2a3b4e8c..96c3536af69 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/MeasureDtoToWsMeasure.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/MeasureDtoToWsMeasure.java @@ -66,8 +66,6 @@ class MeasureDtoToWsMeasure { .setIndex(1) .setValue(formatMeasureValue(doubleValue, stringValue, metric)); ofNullable(bestValue).ifPresent(v -> builderForValue.setBestValue(compare(doubleValue, v) == 0)); - //deprecated since 8.1 - measureBuilder.getPeriodsBuilder().addPeriodsValue(builderForValue); measureBuilder.setPeriod(builderForValue); } } diff --git a/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/measure/ws/search-example.json b/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/measure/ws/search-example.json index 4b88de945e8..6887fd31da3 100644 --- a/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/measure/ws/search-example.json +++ b/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/measure/ws/search-example.json @@ -3,62 +3,65 @@ { "metric": "complexity", "value": "12", - "component": "MY_PROJECT_1" + "component": "MY_PROJECT_1", + "bestValue": false }, { "metric": "complexity", "value": "35", - "component": "MY_PROJECT_2" + "component": "MY_PROJECT_2", + "bestValue": false }, { "metric": "complexity", "value": "42", - "component": "MY_PROJECT_3" + "component": "MY_PROJECT_3", + "bestValue": false }, { "metric": "ncloc", "value": "114", - "component": "MY_PROJECT_1" + "component": "MY_PROJECT_1", + "bestValue": false }, { "metric": "ncloc", "value": "217", - "component": "MY_PROJECT_2" + "component": "MY_PROJECT_2", + "bestValue": false }, { "metric": "ncloc", "value": "1984", - "component": "MY_PROJECT_3" + "component": "MY_PROJECT_3", + "bestValue": false }, { "metric": "new_violations", - "periods": [ - { - "index": 1, - "value": "25" - } - ], - "component": "MY_PROJECT_1" + "component": "MY_PROJECT_1", + "period": { + "index": 1, + "value": "25", + "bestValue": false + } }, { "metric": "new_violations", - "periods": [ - { - "index": 1, - "value": "25" - } - ], - "component": "MY_PROJECT_2" + "component": "MY_PROJECT_2", + "period": { + "index": 1, + "value": "25", + "bestValue": false + } }, { "metric": "new_violations", - "periods": [ - { - "index": 1, - "value": "255" - } - ], - "component": "MY_PROJECT_3" + "component": "MY_PROJECT_3", + "period": { + "index": 1, + "value": "255", + "bestValue": false + } } ] } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/ws/ComponentActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/ws/ComponentActionTest.java index 58573d62867..be90595ee84 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/ws/ComponentActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/ws/ComponentActionTest.java @@ -19,7 +19,9 @@ */ package org.sonar.server.measure.ws; +import java.util.Optional; import java.util.function.Function; +import java.util.function.Predicate; import org.junit.Rule; import org.junit.Test; import org.sonar.api.server.ws.WebService; @@ -42,9 +44,11 @@ import org.sonarqube.ws.Measures.Component; import org.sonarqube.ws.Measures.ComponentWsResponse; import static java.lang.Double.parseDouble; +import static java.util.function.Predicate.not; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.tuple; +import static org.assertj.core.api.InstanceOfAssertFactories.OPTIONAL; import static org.sonar.api.utils.DateUtils.parseDateTime; import static org.sonar.api.web.UserRole.USER; import static org.sonar.db.component.BranchDto.DEFAULT_MAIN_BRANCH_NAME; @@ -94,7 +98,6 @@ public class ComponentActionTest { assertThat(response.getMetrics().getMetricsCount()).isOne(); assertThat(response.hasPeriod()).isFalse(); - assertThat(response.getPeriods().getPeriodsCount()).isZero(); assertThat(response.getComponent().getKey()).isEqualTo(project.getKey()); } @@ -203,12 +206,11 @@ public class ComponentActionTest { assertThat(response.getComponent()).extracting(Component::getKey, Component::getPullRequest) .containsExactlyInAnyOrder(file.getKey(), "pr-123"); - Function extractVariation = m -> { - if (m.getPeriods().getPeriodsValueCount() > 0) { - return parseDouble(m.getPeriods().getPeriodsValue(0).getValue()); - } - return null; - }; + Function extractVariation = m -> Optional.ofNullable(m.getPeriod()) + .map(Measures.PeriodValue::getValue) + .filter(not(String::isEmpty)) + .map(Double::parseDouble) + .orElse(null); assertThat(response.getComponent().getMeasuresList()) .extracting(Measures.Measure::getMetric, extractVariation, m -> m.getValue().isEmpty() ? null : parseDouble(m.getValue())) .containsExactlyInAnyOrder( @@ -448,7 +450,7 @@ public class ComponentActionTest { String response = ws.newRequest() .setParam(PARAM_COMPONENT, file.getKey()) .setParam(PARAM_METRIC_KEYS, "ncloc, complexity, new_violations") - .setParam(PARAM_ADDITIONAL_FIELDS, "metrics,period,periods") + .setParam(PARAM_ADDITIONAL_FIELDS, "metrics,period") .execute() .getInput(); @@ -459,7 +461,7 @@ public class ComponentActionTest { return ws.newRequest() .setParam(PARAM_COMPONENT, componentKey) .setParam(PARAM_METRIC_KEYS, metricKeys) - .setParam(PARAM_ADDITIONAL_FIELDS, "metrics,period,periods") + .setParam(PARAM_ADDITIONAL_FIELDS, "metrics,period") .executeProtobuf(ComponentWsResponse.class); } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java index 06dfc41d5ed..1360f5ef2e7 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java @@ -77,7 +77,6 @@ import static org.sonar.db.component.ComponentTesting.newFileDto; import static org.sonar.db.component.ComponentTesting.newProjectCopy; import static org.sonar.db.component.SnapshotTesting.newAnalysis; import static org.sonar.server.component.ws.MeasuresWsParameters.ADDITIONAL_PERIOD; -import static org.sonar.server.component.ws.MeasuresWsParameters.DEPRECATED_ADDITIONAL_PERIODS; import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_ADDITIONAL_FIELDS; import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_BRANCH; import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_COMPONENT; @@ -162,7 +161,7 @@ public class ComponentTreeActionTest { String response = ws.newRequest() .setParam(PARAM_COMPONENT, project.getKey()) .setParam(PARAM_METRIC_KEYS, "ncloc, complexity, new_violations") - .setParam(PARAM_ADDITIONAL_FIELDS, "metrics,period,periods") + .setParam(PARAM_ADDITIONAL_FIELDS, "metrics,period") .execute() .getInput(); @@ -183,7 +182,6 @@ public class ComponentTreeActionTest { assertThat(response.getComponentsList()).isEmpty(); assertThat(response.getMetrics().getMetricsList()).isEmpty(); assertThat(response.hasPeriod()).isFalse(); - assertThat(response.getPeriods().getPeriodsList()).isEmpty(); } @Test @@ -209,7 +207,7 @@ public class ComponentTreeActionTest { ComponentTreeWsResponse response = ws.newRequest() .setParam(PARAM_COMPONENT, project.getKey()) .setParam(PARAM_METRIC_KEYS, "ncloc,coverage") - .setParam(PARAM_ADDITIONAL_FIELDS, DEPRECATED_ADDITIONAL_PERIODS + "," + ADDITIONAL_PERIOD) + .setParam(PARAM_ADDITIONAL_FIELDS, ADDITIONAL_PERIOD) .executeProtobuf(ComponentTreeWsResponse.class); assertThat(response.getComponentsList().get(0).getMeasuresList()).extracting("metric").containsOnly("coverage"); @@ -218,7 +216,6 @@ public class ComponentTreeActionTest { assertThat(fileMeasures).extracting("metric").containsOnly("ncloc", "coverage"); assertThat(fileMeasures).extracting("value").containsOnly("5", "15.5"); assertThat(response.getPeriod().getMode()).isEqualTo("last_version"); - assertThat(response.getPeriods().getPeriodsList()).extracting("mode").containsOnly("last_version"); } @Test @@ -300,12 +297,6 @@ public class ComponentTreeActionTest { // verify backward compatibility List 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) @@ -342,10 +333,10 @@ public class ComponentTreeActionTest { .executeProtobuf(ComponentTreeWsResponse.class); // directory - assertThat(response.getComponentsList().get(0).getMeasuresList().get(0).getPeriods().getPeriodsValue(0).getValue()).isEqualTo("2.0"); + assertThat(response.getComponentsList().get(0).getMeasuresList().get(0).getPeriod().getValue()).isEqualTo("2.0"); assertThat(response.getComponentsList().get(0).getMeasuresList().get(0).getPeriod().getValue()).isEqualTo("2.0"); // file measures - assertThat(response.getComponentsList().get(1).getMeasuresList().get(0).getPeriods().getPeriodsValue(0).getValue()).isEqualTo("1.0"); + assertThat(response.getComponentsList().get(1).getMeasuresList().get(0).getPeriod().getValue()).isEqualTo("1.0"); assertThat(response.getComponentsList().get(1).getMeasuresList().get(0).getPeriod().getValue()).isEqualTo("1.0"); } @@ -631,7 +622,7 @@ public class ComponentTreeActionTest { assertThat(response.getBaseComponent()).extracting(Component::getKey, Component::getPullRequest) .containsExactlyInAnyOrder(file.getKey(), "pr-123"); assertThat(response.getBaseComponent().getMeasuresList()) - .extracting(Measure::getMetric, m -> parseDouble(m.getPeriods().getPeriodsValue(0).getValue()), Measure::getValue) + .extracting(Measure::getMetric, m -> parseDouble(m.getPeriod().getValue()), Measure::getValue) .containsExactlyInAnyOrder(tuple(newBug.getKey(), measure.getValue(), "")); assertThat(response.getBaseComponent().getMeasuresList()) diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/ws/SearchActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/ws/SearchActionTest.java index c2a229ea1e7..2895d76bc06 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/ws/SearchActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/ws/SearchActionTest.java @@ -156,9 +156,7 @@ public class SearchActionTest { Measure measure = measures.get(0); assertThat(measure.getMetric()).isEqualTo(coverage.getKey()); assertThat(measure.getValue()).isEmpty(); - assertThat(measure.getPeriods().getPeriodsValueList()) - .extracting(Measures.PeriodValue::getIndex, Measures.PeriodValue::getValue) - .containsOnly(tuple(1, "10.0")); + assertThat(measure.getPeriod().getValue()).isEqualTo("10.0"); } @Test diff --git a/sonar-ws/src/main/protobuf/ws-measures.proto b/sonar-ws/src/main/protobuf/ws-measures.proto index ebe34f594a3..2d86f60418b 100644 --- a/sonar-ws/src/main/protobuf/ws-measures.proto +++ b/sonar-ws/src/main/protobuf/ws-measures.proto @@ -32,8 +32,7 @@ message ComponentTreeWsResponse { optional Component baseComponent = 2; repeated Component components = 3; optional Metrics metrics = 4; - //deprecated since 8.1 - optional Periods periods = 5; + reserved 5; //periods optional Period period = 6; } @@ -41,8 +40,7 @@ message ComponentTreeWsResponse { message ComponentWsResponse { optional Component component = 1; optional Metrics metrics = 2; - //deprecated since 8.1 - optional Periods periods = 3; + reserved 3; //periods optional Period period = 4; } @@ -90,10 +88,6 @@ message Period { optional string parameter = 4; } -message Periods { - repeated Period periods = 1; -} - message Metrics { repeated sonarqube.ws.commons.Metric metrics = 1; } @@ -101,16 +95,12 @@ message Metrics { message Measure { optional string metric = 1; optional string value = 2; - //deprecated since 8.1 - optional PeriodsValue periods = 3; + reserved 3; // periods optional string component = 4; optional bool bestValue = 5; optional PeriodValue period = 6; } -message PeriodsValue { - repeated PeriodValue periodsValue = 1; -} message PeriodValue { //deprecated since 8.1 -- 2.39.5