]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-17352 Refactor component keys to not include branch suffix
authorLukasz Jarocki <lukasz.jarocki@sonarsource.com>
Mon, 10 Oct 2022 09:37:30 +0000 (11:37 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 12 Oct 2022 20:03:44 +0000 (20:03 +0000)
server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/ComponentAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/ComponentTreeAction.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java

index 8e6f553c2468f1d5b899975d884f88ed234a414c..cfb92adb1f607d6c8c458579f485bae3536a7fcd 100644 (file)
@@ -229,11 +229,12 @@ public class ComponentAction implements MeasuresWsAction {
   }
 
   private Optional<RefComponent> getReference(DbSession dbSession, ComponentDto component) {
-    if (component.getCopyComponentUuid() == null) {
+    String copyComponentUuid = component.getCopyComponentUuid();
+    if (copyComponentUuid == null) {
       return Optional.empty();
     }
 
-    Optional<ComponentDto> refComponent = dbClient.componentDao().selectByUuid(dbSession, component.getCopyComponentUuid());
+    Optional<ComponentDto> refComponent = dbClient.componentDao().selectByUuid(dbSession, copyComponentUuid);
     if (refComponent.isEmpty()) {
       return Optional.empty();
     }
@@ -255,6 +256,12 @@ public class ComponentAction implements MeasuresWsAction {
       response.setComponent(componentDtoToWsComponent(component, measuresByMetric, emptyMap(), isMainBranch ? null : request.getBranch(), request.getPullRequest()));
     }
 
+    setAdditionalFields(request, metrics, period, response);
+
+    return response.build();
+  }
+
+  private static void setAdditionalFields(ComponentRequest request, Collection<MetricDto> metrics, Optional<Measures.Period> period, ComponentWsResponse.Builder response) {
     List<String> additionalFields = request.getAdditionalFields();
     if (additionalFields != null) {
       if (additionalFields.contains(ADDITIONAL_METRICS)) {
@@ -272,8 +279,6 @@ public class ComponentAction implements MeasuresWsAction {
         response.setPeriod(period.get());
       }
     }
-
-    return response.build();
   }
 
   private static ComponentRequest toComponentWsRequest(Request request) {
@@ -348,20 +353,21 @@ public class ComponentAction implements MeasuresWsAction {
   }
 
   private static class RefComponent {
-    public RefComponent(BranchDto refBranch, ComponentDto refComponent) {
+
+    private final BranchDto refBranch;
+    private final ComponentDto component;
+
+    public RefComponent(BranchDto refBranch, ComponentDto component) {
       this.refBranch = refBranch;
-      this.refComponent = refComponent;
+      this.component = component;
     }
 
-    private BranchDto refBranch;
-    private ComponentDto refComponent;
-
     public BranchDto getRefBranch() {
       return refBranch;
     }
 
     public ComponentDto getComponent() {
-      return refComponent;
+      return component;
     }
   }
 }
index 6471ad3b1c3b4dc11c11c46b70239516e05c15a1..3c2d531f2ee8ccb3a21b4affad1d602b8674e3eb 100644 (file)
@@ -22,8 +22,6 @@ package org.sonar.server.measure.ws;
 import com.google.common.base.Joiner;
 import com.google.common.collect.FluentIterable;
 import com.google.common.collect.HashBasedTable;
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.ImmutableSortedSet;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
@@ -108,8 +106,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.QualifierParameterContext.newQualifierParameterContext;
 import static org.sonar.server.ws.WsParameterBuilder.createQualifiersParameter;
+import static org.sonar.server.ws.WsParameterBuilder.QualifierParameterContext.newQualifierParameterContext;
 import static org.sonar.server.ws.WsUtils.writeProtobuf;
 
 /**
@@ -135,7 +133,7 @@ public class ComponentTreeAction implements MeasuresWsAction {
   static final String ALL_STRATEGY = "all";
   static final String CHILDREN_STRATEGY = "children";
   static final String LEAVES_STRATEGY = "leaves";
-  static final Map<String, Strategy> STRATEGIES = ImmutableMap.of(
+  static final Map<String, Strategy> STRATEGIES = Map.of(
     ALL_STRATEGY, LEAVES,
     CHILDREN_STRATEGY, CHILDREN,
     LEAVES_STRATEGY, LEAVES);
@@ -149,10 +147,10 @@ public class ComponentTreeAction implements MeasuresWsAction {
   static final String ALL_METRIC_SORT_FILTER = "all";
   static final String WITH_MEASURES_ONLY_METRIC_SORT_FILTER = "withMeasuresOnly";
   static final Set<String> METRIC_SORT_FILTERS = ImmutableSortedSet.of(ALL_METRIC_SORT_FILTER, WITH_MEASURES_ONLY_METRIC_SORT_FILTER);
-  static final Set<String> FORBIDDEN_METRIC_TYPES = ImmutableSet.of(DISTRIB.name(), DATA.name());
+  static final Set<String> FORBIDDEN_METRIC_TYPES = Set.of(DISTRIB.name(), DATA.name());
   private static final int MAX_METRIC_KEYS = 15;
   private static final Joiner COMMA_JOINER = Joiner.on(", ");
-  private static final Set<String> QUALIFIERS_ELIGIBLE_FOR_BEST_VALUE = ImmutableSet.of(Qualifiers.FILE, Qualifiers.UNIT_TEST_FILE);
+  private static final Set<String> QUALIFIERS_ELIGIBLE_FOR_BEST_VALUE = Set.of(Qualifiers.FILE, Qualifiers.UNIT_TEST_FILE);
 
   private final DbClient dbClient;
   private final ComponentFinder componentFinder;
index 353d901140606c40f8bba3243c148bfdb38720b4..2de3079465f88351f872a2ff659203fd22de317e 100644 (file)
@@ -572,6 +572,24 @@ public class ComponentTreeActionTest {
       .containsExactlyInAnyOrder(file.getKey(), "");
   }
 
+  @Test
+  public void show_branch_on_empty_response_if_not_main_branch() {
+    ComponentDto mainProjectBranch = db.components().insertPrivateProject();
+    ComponentDto project = db.components().insertProjectBranch(mainProjectBranch, b -> b.setKey("develop"));
+    userSession.addProjectPermission(USER, mainProjectBranch);
+    ComponentDto file = db.components().insertComponent(newFileDto(project));
+    MetricDto complexity = db.measures().insertMetric(m -> m.setValueType(INT.name()));
+
+    ComponentTreeWsResponse response = ws.newRequest()
+      .setParam(PARAM_COMPONENT, file.getKey())
+      .setParam(PARAM_BRANCH, "develop")
+      .setParam(PARAM_METRIC_KEYS, complexity.getKey())
+      .executeProtobuf(ComponentTreeWsResponse.class);
+
+    assertThat(response.getBaseComponent()).extracting(Component::getKey, Component::getBranch)
+      .containsExactlyInAnyOrder(file.getKey(), "develop");
+  }
+
   @Test
   public void pull_request() {
     ComponentDto project = db.components().insertPrivateProject();