From: Teryk Bellahsene Date: Wed, 20 Jan 2016 11:23:09 +0000 (+0100) Subject: SONAR-7129 WS api/components/tree return refKey X-Git-Tag: 5.4-M9~14 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=49632b19154fa95cf68802645defa16027ab6de4;p=sonarqube.git SONAR-7129 WS api/components/tree return refKey --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ws/TreeAction.java b/server/sonar-server/src/main/java/org/sonar/server/component/ws/TreeAction.java index 5fc9911c3b6..0855467bc91 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ws/TreeAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ws/TreeAction.java @@ -173,7 +173,7 @@ public class TreeAction implements ComponentsWsAction { default: throw new IllegalStateException("Unknown component tree strategy"); } - Map referenceComponentUuidsById = searchReferenceComponentUuidsById(dbSession, components); + Map 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 searchReferenceComponentUuidsById(DbSession dbSession, List components) { + private Map searchReferenceComponentUuidsById(DbSession dbSession, List components) { List referenceComponentIds = from(components) .transform(ComponentDtoWithSnapshotIdToCopyResourceIdFunction.INSTANCE) .filter(Predicates.notNull()) @@ -192,9 +192,9 @@ public class TreeAction implements ComponentsWsAction { } List referenceComponents = dbClient.componentDao().selectByIds(dbSession, referenceComponentIds); - Map referenceComponentUuidsById = new HashMap<>(); + Map 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 components, Map referenceComponentUuidsById, - Paging paging) { + private static TreeWsResponse buildResponse(ComponentDto baseComponent, List components, Map 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 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.emptyMap())); + response.setBaseComponent(componentDtoToWsComponent(baseComponent, Collections.emptyMap())); return response.build(); } + private static WsComponents.Component.Builder componentDtoToWsComponent(ComponentDto component, Map 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 childrenQualifiers = childrenQualifiers(request, baseSnapshot.getQualifier()); diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ws/TreeActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ws/TreeActionTest.java index cc5821ce126..2fe4f1d7669 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/ws/TreeActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/ws/TreeActionTest.java @@ -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 diff --git a/sonar-ws/src/main/protobuf/ws-components.proto b/sonar-ws/src/main/protobuf/ws-components.proto index 9ff36b851a5..128ade44c99 100644 --- a/sonar-ws/src/main/protobuf/ws-components.proto +++ b/sonar-ws/src/main/protobuf/ws-components.proto @@ -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; } diff --git a/sonar-ws/src/main/protobuf/ws-measures.proto b/sonar-ws/src/main/protobuf/ws-measures.proto index 5960ea78943..6014576568f 100644 --- a/sonar-ws/src/main/protobuf/ws-measures.proto +++ b/sonar-ws/src/main/protobuf/ws-measures.proto @@ -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 { diff --git a/sonar-ws/src/main/protobuf/ws-qualitygates.proto b/sonar-ws/src/main/protobuf/ws-qualitygates.proto index f4669419d86..57e867e767d 100644 --- a/sonar-ws/src/main/protobuf/ws-qualitygates.proto +++ b/sonar-ws/src/main/protobuf/ws-qualitygates.proto @@ -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;