]> source.dussan.org Git - sonarqube.git/blob
899372371a548fefe4bb70da688ef124a8126a11
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 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
22 import com.google.common.annotations.VisibleForTesting;
23 import java.util.HashMap;
24 import java.util.LinkedHashMap;
25 import java.util.List;
26 import java.util.Map;
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;
39
40 import static java.util.Optional.ofNullable;
41
42 public class ShowResponseBuilder {
43
44   private final ComponentDao componentDao;
45
46   @Inject
47   public ShowResponseBuilder(DbClient dbClient) {
48     this.componentDao = dbClient.componentDao();
49   }
50
51   @VisibleForTesting
52   ShowResponseBuilder(ComponentDao componentDao) {
53     this.componentDao = componentDao;
54   }
55
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();
59     blocks.stream()
60       .map(block -> toWsDuplication(block, refByComponentKey))
61       .forEach(response::addDuplications);
62
63     writeFileRefs(session, refByComponentKey, response, branch, pullRequest);
64     return response.build();
65   }
66
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);
72
73     return wsDuplication;
74   }
75
76   private static Block.Builder toWsBlock(Duplication duplication, Map<String, Reference> refByComponentKey) {
77     Block.Builder block = Block.newBuilder();
78
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());
85     }
86
87     block.setFrom(duplication.from());
88     block.setSize(duplication.size());
89
90     return block;
91   }
92
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<>();
95
96     for (Map.Entry<String, Reference> entry : refByComponentKey.entrySet()) {
97       Reference ref = entry.getValue();
98       ComponentDto file = ref.getDto();
99
100       if (file != null) {
101         ComponentDto project = getProject(file.branchUuid(), projectsByUuid, session);
102         response.putFiles(ref.getId(), toWsFile(file, project, branch, pullRequest));
103       } else {
104         response.putFiles(ref.getId(), toWsFile(ref.getComponentKey(), branch, pullRequest));
105       }
106     }
107   }
108
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();
117   }
118
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);
131     });
132     return wsFile.build();
133   }
134
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);
142       }
143     }
144     return project;
145   }
146
147   private static class Reference {
148     private final String id;
149     private final ComponentDto dto;
150     private final String componentKey;
151
152     public Reference(String id, @Nullable ComponentDto dto, String componentKey) {
153       this.id = id;
154       this.dto = dto;
155       this.componentKey = componentKey;
156     }
157
158     public String getId() {
159       return id;
160     }
161
162     @CheckForNull
163     public ComponentDto getDto() {
164       return dto;
165     }
166
167     public String getComponentKey() {
168       return componentKey;
169     }
170
171   }
172
173 }