]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-13703 api/measures/component_tree returns an SVW qualifier for Applications
authorMichal Duda <michal.duda@sonarsource.com>
Thu, 11 Mar 2021 15:56:02 +0000 (16:56 +0100)
committersonartech <sonartech@sonarsource.com>
Mon, 15 Mar 2021 21:29:06 +0000 (21:29 +0000)
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 cbd139a13b70b20609b4b04b5784c74848acb71e..e56d72b6770b9bb8ff64eca7cd4cde60cdca0619 100644 (file)
@@ -374,6 +374,8 @@ public class ComponentTreeAction implements MeasuresWsAction {
     ComponentDto referenceComponent = referenceComponentsByUuid.get(component.getCopyResourceUuid());
     if (referenceComponent != null) {
       wsComponent.setRefKey(referenceComponent.getKey());
+      String displayQualifier = getDisplayQualifier(component, referenceComponent);
+      wsComponent.setQualifier(displayQualifier);
     }
     Measures.Measure.Builder measureBuilder = Measures.Measure.newBuilder();
     for (Map.Entry<MetricDto, ComponentTreeData.Measure> entry : measures.entrySet()) {
@@ -385,6 +387,16 @@ public class ComponentTreeAction implements MeasuresWsAction {
     return wsComponent;
   }
 
+  // https://jira.sonarsource.com/browse/SONAR-13703 - for apps that were added as a local reference to a portfolio, we want to
+  // show them as apps, not sub-portfolios
+  private static String getDisplayQualifier(ComponentDto component, ComponentDto referenceComponent) {
+    String qualifier = component.qualifier();
+    if (qualifier.equals(Qualifiers.SUBVIEW) && referenceComponent.qualifier().equals(Qualifiers.APP)) {
+      return Qualifiers.APP;
+    }
+    return qualifier;
+  }
+
   private ComponentTreeData load(ComponentTreeRequest wsRequest) {
     try (DbSession dbSession = dbClient.openSession(false)) {
       ComponentDto baseComponent = loadComponent(dbSession, wsRequest);
index ace3cf74fec28de0a63b6dd7d8c57727f9d98921..26e4c6f547929d77138e5b3b6850b1f4a92f0d3f 100644 (file)
@@ -73,6 +73,8 @@ import static org.sonar.db.component.BranchType.PULL_REQUEST;
 import static org.sonar.db.component.ComponentTesting.newDirectory;
 import static org.sonar.db.component.ComponentTesting.newFileDto;
 import static org.sonar.db.component.ComponentTesting.newProjectCopy;
+import static org.sonar.db.component.ComponentTesting.newSubView;
+import static org.sonar.db.component.ComponentTesting.newView;
 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;
@@ -657,6 +659,46 @@ public class ComponentTreeActionTest {
       .containsExactlyInAnyOrder(tuple(projectCopy.getKey(), project.getKey()));
   }
 
+  @Test
+  public void portfolio_local_reference_in_portfolio() {
+    ComponentDto view = db.components().insertComponent(newView("VIEW1-UUID").setDbKey("Apache-Projects").setName("Apache Projects"));
+    ComponentDto view2 = db.components().insertPrivatePortfolio();
+    ComponentDto localView = db.components().insertComponent(
+      newSubView(view, "SUB-VIEW-UUID", "All-Projects").setName("All projects").setCopyComponentUuid(view2.uuid()));
+    db.components().insertSnapshot(view);
+    MetricDto ncloc = insertNclocMetric();
+    db.measures().insertLiveMeasure(localView, 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, Component::getQualifier)
+      .containsExactlyInAnyOrder(tuple(localView.getKey(), view2.getKey(), "SVW"));
+  }
+
+  @Test
+  public void application_local_reference_in_portfolio() {
+    ComponentDto view = db.components().insertComponent(newView("VIEW1-UUID").setDbKey("Apache-Projects").setName("Apache Projects"));
+    ComponentDto application = db.components().insertPrivateApplication();
+    ComponentDto localView = db.components().insertComponent(
+      newSubView(view, "SUB-VIEW-UUID", "All-Projects").setName("All projects").setCopyComponentUuid(application.uuid()));
+    db.components().insertSnapshot(view);
+    MetricDto ncloc = insertNclocMetric();
+    db.measures().insertLiveMeasure(localView, 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, Component::getQualifier)
+      .containsExactlyInAnyOrder(tuple(localView.getKey(), application.getKey(), "APP"));
+  }
+
   @Test
   public void project_branch_reference_from_application_branch() {
     MetricDto ncloc = insertNclocMetric();