3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.duplication.ws;
22 import com.google.common.annotations.VisibleForTesting;
23 import java.util.HashMap;
24 import java.util.LinkedHashMap;
25 import java.util.List;
27 import java.util.Optional;
28 import javax.annotation.CheckForNull;
29 import javax.annotation.Nullable;
30 import javax.inject.Inject;
31 import org.apache.commons.lang.StringUtils;
32 import org.sonar.db.DbClient;
33 import org.sonar.db.DbSession;
34 import org.sonar.db.component.ComponentDao;
35 import org.sonar.db.component.ComponentDto;
36 import org.sonarqube.ws.Duplications;
37 import org.sonarqube.ws.Duplications.Block;
38 import org.sonarqube.ws.Duplications.ShowResponse;
40 import static java.util.Optional.ofNullable;
42 public class ShowResponseBuilder {
44 private final ComponentDao componentDao;
47 public ShowResponseBuilder(DbClient dbClient) {
48 this.componentDao = dbClient.componentDao();
52 ShowResponseBuilder(ComponentDao componentDao) {
53 this.componentDao = componentDao;
56 ShowResponse build(DbSession session, List<DuplicationsParser.Block> blocks, @Nullable String branch, @Nullable String pullRequest) {
57 Map<String, Reference> refByComponentKey = new LinkedHashMap<>();
58 ShowResponse.Builder response = ShowResponse.newBuilder();
60 .map(block -> toWsDuplication(block, refByComponentKey))
61 .forEach(response::addDuplications);
63 writeFileRefs(session, refByComponentKey, response, branch, pullRequest);
64 return response.build();
67 private static Duplications.Duplication.Builder toWsDuplication(DuplicationsParser.Block block, Map<String, Reference> refByComponentKey) {
68 Duplications.Duplication.Builder wsDuplication = Duplications.Duplication.newBuilder();
69 block.getDuplications().stream()
70 .map(duplication -> toWsBlock(duplication, refByComponentKey))
71 .forEach(wsDuplication::addBlocks);
76 private static Block.Builder toWsBlock(Duplication duplication, Map<String, Reference> refByComponentKey) {
77 Block.Builder block = Block.newBuilder();
79 if (!duplication.removed()) {
80 Reference ref = refByComponentKey.computeIfAbsent(duplication.componentDbKey(), k -> new Reference(
81 Integer.toString(refByComponentKey.size() + 1),
82 duplication.componentDto(),
83 duplication.componentDbKey()));
84 block.setRef(ref.getId());
87 block.setFrom(duplication.from());
88 block.setSize(duplication.size());
93 private void writeFileRefs(DbSession session, Map<String, Reference> refByComponentKey, ShowResponse.Builder response, @Nullable String branch, @Nullable String pullRequest) {
94 Map<String, ComponentDto> projectsByUuid = new HashMap<>();
96 for (Map.Entry<String, Reference> entry : refByComponentKey.entrySet()) {
97 Reference ref = entry.getValue();
98 ComponentDto file = ref.getDto();
101 ComponentDto project = getProject(file.branchUuid(), projectsByUuid, session);
102 response.putFiles(ref.getId(), toWsFile(file, project, branch, pullRequest));
104 response.putFiles(ref.getId(), toWsFile(ref.getComponentKey(), branch, pullRequest));
109 private static Duplications.File toWsFile(String componentKey, @Nullable String branch, @Nullable String pullRequest) {
110 Duplications.File.Builder wsFile = Duplications.File.newBuilder();
111 String keyWithoutBranch = ComponentDto.removeBranchAndPullRequestFromKey(componentKey);
112 wsFile.setKey(keyWithoutBranch);
113 wsFile.setName(StringUtils.substringAfterLast(keyWithoutBranch, ":"));
114 ofNullable(branch).ifPresent(wsFile::setBranch);
115 ofNullable(pullRequest).ifPresent(wsFile::setPullRequest);
116 return wsFile.build();
119 private static Duplications.File toWsFile(ComponentDto file, @Nullable ComponentDto project,
120 @Nullable String branch, @Nullable String pullRequest) {
121 Duplications.File.Builder wsFile = Duplications.File.newBuilder();
122 wsFile.setKey(file.getKey());
123 wsFile.setUuid(file.uuid());
124 wsFile.setName(file.longName());
125 ofNullable(project).ifPresent(p -> {
126 wsFile.setProject(p.getKey());
127 wsFile.setProjectUuid(p.uuid());
128 wsFile.setProjectName(p.longName());
129 ofNullable(branch).ifPresent(wsFile::setBranch);
130 ofNullable(pullRequest).ifPresent(wsFile::setPullRequest);
132 return wsFile.build();
135 private ComponentDto getProject(String projectUuid, Map<String, ComponentDto> projectsByUuid, DbSession session) {
136 ComponentDto project = projectsByUuid.get(projectUuid);
137 if (project == null) {
138 Optional<ComponentDto> projectOptional = componentDao.selectByUuid(session, projectUuid);
139 if (projectOptional.isPresent()) {
140 project = projectOptional.get();
141 projectsByUuid.put(project.uuid(), project);
147 private static class Reference {
148 private final String id;
149 private final ComponentDto dto;
150 private final String componentKey;
152 public Reference(String id, @Nullable ComponentDto dto, String componentKey) {
155 this.componentKey = componentKey;
158 public String getId() {
163 public ComponentDto getDto() {
167 public String getComponentKey() {