]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7129 WS api/components/tree return refKey
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Wed, 20 Jan 2016 11:23:09 +0000 (12:23 +0100)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Wed, 20 Jan 2016 11:44:43 +0000 (12:44 +0100)
server/sonar-server/src/main/java/org/sonar/server/component/ws/TreeAction.java
server/sonar-server/src/test/java/org/sonar/server/component/ws/TreeActionTest.java
sonar-ws/src/main/protobuf/ws-components.proto
sonar-ws/src/main/protobuf/ws-measures.proto
sonar-ws/src/main/protobuf/ws-qualitygates.proto

index 5fc9911c3b60b0cbfff03b66439700933fa57a7f..0855467bc91036dd0acebe03c6f683323d8e621e 100644 (file)
@@ -173,7 +173,7 @@ public class TreeAction implements ComponentsWsAction {
         default:
           throw new IllegalStateException("Unknown component tree strategy");
       }
-      Map<Long, String> referenceComponentUuidsById = searchReferenceComponentUuidsById(dbSession, components);
+      Map<Long, ComponentDto> referenceComponentUuidsById = searchReferenceComponentUuidsById(dbSession, components);
 
       return buildResponse(baseComponent, components, referenceComponentUuidsById,
         Paging.forPageIndex(query.getPage()).withPageSize(query.getPageSize()).andTotal(total));
@@ -182,7 +182,7 @@ public class TreeAction implements ComponentsWsAction {
     }
   }
 
-  private Map<Long, String> searchReferenceComponentUuidsById(DbSession dbSession, List<ComponentDtoWithSnapshotId> components) {
+  private Map<Long, ComponentDto> searchReferenceComponentUuidsById(DbSession dbSession, List<ComponentDtoWithSnapshotId> components) {
     List<Long> referenceComponentIds = from(components)
       .transform(ComponentDtoWithSnapshotIdToCopyResourceIdFunction.INSTANCE)
       .filter(Predicates.<Long>notNull())
@@ -192,9 +192,9 @@ public class TreeAction implements ComponentsWsAction {
     }
 
     List<ComponentDto> referenceComponents = dbClient.componentDao().selectByIds(dbSession, referenceComponentIds);
-    Map<Long, String> referenceComponentUuidsById = new HashMap<>();
+    Map<Long, ComponentDto> referenceComponentUuidsById = new HashMap<>();
     for (ComponentDto referenceComponent : referenceComponents) {
-      referenceComponentUuidsById.put(referenceComponent.getId(), referenceComponent.uuid());
+      referenceComponentUuidsById.put(referenceComponent.getId(), referenceComponent);
     }
 
     return referenceComponentUuidsById;
@@ -209,8 +209,8 @@ public class TreeAction implements ComponentsWsAction {
     }
   }
 
-  private static TreeWsResponse buildResponse(ComponentDto baseComponent, List<ComponentDtoWithSnapshotId> components, Map<Long, String> referenceComponentUuidsById,
-    Paging paging) {
+  private static TreeWsResponse buildResponse(ComponentDto baseComponent, List<ComponentDtoWithSnapshotId> components, Map<Long, ComponentDto> referenceComponentsById,
+                                              Paging paging) {
     TreeWsResponse.Builder response = TreeWsResponse.newBuilder();
     response.getPagingBuilder()
       .setPageIndex(paging.pageIndex())
@@ -218,44 +218,46 @@ public class TreeAction implements ComponentsWsAction {
       .setTotal(paging.total())
       .build();
 
-    response.setBaseComponent(componentDtoToWsComponent(baseComponent, referenceComponentUuidsById));
+    response.setBaseComponent(componentDtoToWsComponent(baseComponent, referenceComponentsById));
     for (ComponentDto dto : components) {
-      response.addComponents(componentDtoToWsComponent(dto, referenceComponentUuidsById));
+      response.addComponents(componentDtoToWsComponent(dto, referenceComponentsById));
     }
 
     return response.build();
   }
 
-  private static WsComponents.Component.Builder componentDtoToWsComponent(ComponentDto dto, Map<Long, String> referenceComponentUuidsById) {
-    WsComponents.Component.Builder wsComponent = WsComponents.Component.newBuilder()
-      .setId(dto.uuid())
-      .setKey(dto.key())
-      .setName(dto.name())
-      .setQualifier(dto.qualifier());
-    if (dto.path() != null) {
-      wsComponent.setPath(dto.path());
-    }
-    if (dto.description() != null) {
-      wsComponent.setDescription(dto.description());
-    }
-    if (!referenceComponentUuidsById.isEmpty() && referenceComponentUuidsById.get(dto.getCopyResourceId()) != null) {
-      wsComponent.setRefId(referenceComponentUuidsById.get(dto.getCopyResourceId()));
-    }
-
-    return wsComponent;
-  }
-
   private static TreeWsResponse emptyResponse(ComponentDto baseComponent, TreeWsRequest request) {
     TreeWsResponse.Builder response = TreeWsResponse.newBuilder();
     response.getPagingBuilder()
       .setTotal(0)
       .setPageIndex(request.getPage())
       .setPageSize(request.getPageSize());
-    response.setBaseComponent(componentDtoToWsComponent(baseComponent, Collections.<Long, String>emptyMap()));
+    response.setBaseComponent(componentDtoToWsComponent(baseComponent, Collections.<Long, ComponentDto>emptyMap()));
 
     return response.build();
   }
 
+  private static WsComponents.Component.Builder componentDtoToWsComponent(ComponentDto component, Map<Long, ComponentDto> referenceComponentsById) {
+    WsComponents.Component.Builder wsComponent = WsComponents.Component.newBuilder()
+      .setId(component.uuid())
+      .setKey(component.key())
+      .setName(component.name())
+      .setQualifier(component.qualifier());
+    if (component.path() != null) {
+      wsComponent.setPath(component.path());
+    }
+    if (component.description() != null) {
+      wsComponent.setDescription(component.description());
+    }
+    ComponentDto referenceComponent = referenceComponentsById.get(component.getCopyResourceId());
+    if (!referenceComponentsById.isEmpty() && referenceComponent != null) {
+      wsComponent.setRefId(referenceComponent.uuid());
+      wsComponent.setRefKey(referenceComponent.key());
+    }
+
+    return wsComponent;
+  }
+
   private ComponentTreeQuery toComponentTreeQuery(TreeWsRequest request, SnapshotDto baseSnapshot) {
     List<String> childrenQualifiers = childrenQualifiers(request, baseSnapshot.getQualifier());
 
index cc5821ce1261d3552b5e6c228e478c9fe89cb476..2fe4f1d76691e8895c3b2bb88b7bbf28b3827cee 100644 (file)
@@ -243,7 +243,7 @@ public class TreeActionTest {
   public void direct_children_of_a_view() throws IOException {
     ComponentDto view = newView("view-uuid");
     SnapshotDto viewSnapshot = componentDb.insertViewAndSnapshot(view);
-    ComponentDto project = newProjectDto("project-uuid-1").setName("project-name");
+    ComponentDto project = newProjectDto("project-uuid-1").setName("project-name").setKey("project-key-1");
     componentDb.insertProjectAndSnapshot(project);
     componentDb.insertComponentAndSnapshot(newProjectCopy("project-uuid-1-copy", project, view), viewSnapshot);
     componentDb.insertComponentAndSnapshot(newSubView(view, "sub-view-uuid", "sub-view-key").setName("sub-view-name"), viewSnapshot);
@@ -260,6 +260,7 @@ public class TreeActionTest {
 
     assertThat(response.getComponentsList()).extracting("id").containsExactly("project-uuid-1-copy", "sub-view-uuid");
     assertThat(response.getComponentsList()).extracting("refId").containsExactly("project-uuid-1", "");
+    assertThat(response.getComponentsList()).extracting("refKey").containsExactly("project-key-1", "");
   }
 
   @Test
index 9ff36b851a53f78835b08b300ad07301a79442e5..128ade44c9905c98d2523d38de091c6643f331b4 100644 (file)
@@ -48,12 +48,13 @@ message ShowWsResponse {
 
 message Component {
   optional string id = 1;
-  optional string refId = 2;
-  optional string key = 3;
-  optional string projectId = 4;
-  optional string name = 5;
-  optional string description = 6;
-  optional string qualifier = 7;
-  optional string path = 8;
-  optional string language = 9;
+  optional string key = 2;
+  optional string refId = 3;
+  optional string refKey = 4;
+  optional string projectId = 5;
+  optional string name = 6;
+  optional string description = 7;
+  optional string qualifier = 8;
+  optional string path = 9;
+  optional string language = 10;
 }
index 5960ea78943fcca6d2d3e3ae938e57b39aafea9e..6014576568fdad09a458e5b8750b6d43046663de 100644 (file)
@@ -37,15 +37,16 @@ message ComponentTreeWsResponse {
 
 message Component {
   optional string id = 1;
-  optional string refId = 2;
-  optional string key = 3;
-  optional string projectId = 4;
-  optional string name = 5;
-  optional string description = 6;
-  optional string qualifier = 7;
-  optional string path = 8;
-  optional string language = 9;
-  optional Measures measures = 10;
+  optional string key = 2;
+  optional string refId = 3;
+  optional string refKey = 4;
+  optional string projectId = 5;
+  optional string name = 6;
+  optional string description = 7;
+  optional string qualifier = 8;
+  optional string path = 9;
+  optional string language = 10;
+  optional Measures measures = 11;
 }
 
 message Period {
index f4669419d86a84d4801838d953db99cd1c2bbe36..57e867e767d418b1431e97d6676a9c928b1c0a49 100644 (file)
@@ -20,8 +20,6 @@ syntax = "proto2";
 
 package sonarqube.ws.qualitygate;
 
-import "ws-commons.proto";
-
 option java_package = "org.sonarqube.ws";
 option java_outer_classname = "WsQualityGates";
 option optimize_for = SPEED;