You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ShowResponseBuilder.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.duplication.ws;
  21. import com.google.common.annotations.VisibleForTesting;
  22. import com.google.common.base.Optional;
  23. import java.util.List;
  24. import java.util.Map;
  25. import javax.annotation.Nullable;
  26. import org.sonar.db.DbClient;
  27. import org.sonar.db.DbSession;
  28. import org.sonar.db.component.ComponentDao;
  29. import org.sonar.db.component.ComponentDto;
  30. import org.sonarqube.ws.Duplications;
  31. import org.sonarqube.ws.Duplications.Block;
  32. import org.sonarqube.ws.Duplications.ShowResponse;
  33. import static com.google.common.collect.Maps.newHashMap;
  34. import static org.sonar.core.util.Protobuf.setNullable;
  35. // TODO Add UT on branch
  36. public class ShowResponseBuilder {
  37. private final ComponentDao componentDao;
  38. public ShowResponseBuilder(DbClient dbClient) {
  39. this.componentDao = dbClient.componentDao();
  40. }
  41. @VisibleForTesting
  42. ShowResponseBuilder(ComponentDao componentDao) {
  43. this.componentDao = componentDao;
  44. }
  45. ShowResponse build(DbSession session, List<DuplicationsParser.Block> blocks, @Nullable String branch, @Nullable String pullRequest) {
  46. ShowResponse.Builder response = ShowResponse.newBuilder();
  47. Map<String, String> refByComponentKey = newHashMap();
  48. blocks.stream()
  49. .map(block -> toWsDuplication(block, refByComponentKey))
  50. .forEach(response::addDuplications);
  51. writeFiles(session, response, refByComponentKey, branch, pullRequest);
  52. return response.build();
  53. }
  54. private static Duplications.Duplication.Builder toWsDuplication(DuplicationsParser.Block block, Map<String, String> refByComponentKey) {
  55. Duplications.Duplication.Builder wsDuplication = Duplications.Duplication.newBuilder();
  56. block.getDuplications().stream()
  57. .map(d -> toWsBlock(refByComponentKey, d))
  58. .forEach(wsDuplication::addBlocks);
  59. return wsDuplication;
  60. }
  61. private static Block.Builder toWsBlock(Map<String, String> refByComponentKey, DuplicationsParser.Duplication duplication) {
  62. String ref = null;
  63. ComponentDto componentDto = duplication.file();
  64. if (componentDto != null) {
  65. String componentKey = componentDto.getDbKey();
  66. ref = refByComponentKey.computeIfAbsent(componentKey, k -> Integer.toString(refByComponentKey.size() + 1));
  67. }
  68. Block.Builder block = Block.newBuilder();
  69. block.setFrom(duplication.from());
  70. block.setSize(duplication.size());
  71. setNullable(ref, block::setRef);
  72. return block;
  73. }
  74. private static Duplications.File toWsFile(ComponentDto file, @Nullable ComponentDto project, @Nullable ComponentDto subProject, @Nullable String branch,
  75. @Nullable String pullRequest) {
  76. Duplications.File.Builder wsFile = Duplications.File.newBuilder();
  77. wsFile.setKey(file.getKey());
  78. wsFile.setUuid(file.uuid());
  79. wsFile.setName(file.longName());
  80. setNullable(project, p -> {
  81. wsFile.setProject(p.getKey());
  82. wsFile.setProjectUuid(p.uuid());
  83. wsFile.setProjectName(p.longName());
  84. // Do not return sub project if sub project and project are the same
  85. boolean displaySubProject = subProject != null && !subProject.uuid().equals(project.uuid());
  86. if (displaySubProject) {
  87. wsFile.setSubProject(subProject.getKey());
  88. wsFile.setSubProjectUuid(subProject.uuid());
  89. wsFile.setSubProjectName(subProject.longName());
  90. }
  91. setNullable(branch, wsFile::setBranch);
  92. setNullable(pullRequest, wsFile::setPullRequest);
  93. return wsFile;
  94. });
  95. return wsFile.build();
  96. }
  97. private void writeFiles(DbSession session, ShowResponse.Builder response, Map<String, String> refByComponentKey, @Nullable String branch, @Nullable String pullRequest) {
  98. Map<String, ComponentDto> projectsByUuid = newHashMap();
  99. Map<String, ComponentDto> parentModulesByUuid = newHashMap();
  100. Map<String, Duplications.File> filesByRef = response.getMutableFiles();
  101. for (Map.Entry<String, String> entry : refByComponentKey.entrySet()) {
  102. String componentKey = entry.getKey();
  103. String ref = entry.getValue();
  104. Optional<ComponentDto> fileOptional = componentDao.selectByKey(session, componentKey);
  105. if (fileOptional.isPresent()) {
  106. ComponentDto file = fileOptional.get();
  107. ComponentDto project = getProject(file.projectUuid(), projectsByUuid, session);
  108. ComponentDto parentModule = getParentProject(file.getRootUuid(), parentModulesByUuid, session);
  109. filesByRef.put(ref, toWsFile(file, project, parentModule, branch, pullRequest));
  110. }
  111. }
  112. }
  113. private ComponentDto getProject(String projectUuid, Map<String, ComponentDto> projectsByUuid, DbSession session) {
  114. ComponentDto project = projectsByUuid.get(projectUuid);
  115. if (project == null) {
  116. Optional<ComponentDto> projectOptional = componentDao.selectByUuid(session, projectUuid);
  117. if (projectOptional.isPresent()) {
  118. project = projectOptional.get();
  119. projectsByUuid.put(project.uuid(), project);
  120. }
  121. }
  122. return project;
  123. }
  124. private ComponentDto getParentProject(String rootUuid, Map<String, ComponentDto> subProjectsByUuid, DbSession session) {
  125. ComponentDto project = subProjectsByUuid.get(rootUuid);
  126. if (project == null) {
  127. Optional<ComponentDto> projectOptional = componentDao.selectByUuid(session, rootUuid);
  128. if (projectOptional.isPresent()) {
  129. project = projectOptional.get();
  130. subProjectsByUuid.put(project.uuid(), project);
  131. }
  132. }
  133. return project;
  134. }
  135. }