]> source.dussan.org Git - sonarqube.git/blob
8ae2c5a4f04bc797a96bca997287752b7876db89
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 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     wsFile.setKey(componentKey);
112     wsFile.setName(StringUtils.substringAfterLast(componentKey, ":"));
113     ofNullable(branch).ifPresent(wsFile::setBranch);
114     ofNullable(pullRequest).ifPresent(wsFile::setPullRequest);
115     return wsFile.build();
116   }
117
118   private static Duplications.File toWsFile(ComponentDto file, @Nullable ComponentDto project,
119     @Nullable String branch, @Nullable String pullRequest) {
120     Duplications.File.Builder wsFile = Duplications.File.newBuilder();
121     wsFile.setKey(file.getKey());
122     wsFile.setUuid(file.uuid());
123     wsFile.setName(file.longName());
124     ofNullable(project).ifPresent(p -> {
125       wsFile.setProject(p.getKey());
126       wsFile.setProjectUuid(p.uuid());
127       wsFile.setProjectName(p.longName());
128       ofNullable(branch).ifPresent(wsFile::setBranch);
129       ofNullable(pullRequest).ifPresent(wsFile::setPullRequest);
130     });
131     return wsFile.build();
132   }
133
134   private ComponentDto getProject(String projectUuid, Map<String, ComponentDto> projectsByUuid, DbSession session) {
135     ComponentDto project = projectsByUuid.get(projectUuid);
136     if (project == null) {
137       Optional<ComponentDto> projectOptional = componentDao.selectByUuid(session, projectUuid);
138       if (projectOptional.isPresent()) {
139         project = projectOptional.get();
140         projectsByUuid.put(project.uuid(), project);
141       }
142     }
143     return project;
144   }
145
146   private static class Reference {
147     private final String id;
148     private final ComponentDto dto;
149     private final String componentKey;
150
151     public Reference(String id, @Nullable ComponentDto dto, String componentKey) {
152       this.id = id;
153       this.dto = dto;
154       this.componentKey = componentKey;
155     }
156
157     public String getId() {
158       return id;
159     }
160
161     @CheckForNull
162     public ComponentDto getDto() {
163       return dto;
164     }
165
166     public String getComponentKey() {
167       return componentKey;
168     }
169
170   }
171
172 }