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.ce.task.projectanalysis.filemove;
22 import com.google.common.collect.ImmutableMap;
23 import com.google.common.collect.Sets;
24 import java.util.Collection;
25 import java.util.LinkedList;
26 import java.util.List;
28 import java.util.Objects;
29 import java.util.Optional;
31 import java.util.function.Consumer;
32 import java.util.function.Function;
33 import javax.annotation.concurrent.Immutable;
34 import org.apache.ibatis.session.ResultHandler;
35 import org.jetbrains.annotations.NotNull;
36 import org.sonar.api.utils.log.Logger;
37 import org.sonar.api.utils.log.Loggers;
38 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
39 import org.sonar.ce.task.projectanalysis.component.Component;
40 import org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit;
41 import org.sonar.ce.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
42 import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
43 import org.sonar.ce.task.projectanalysis.component.TypeAwareVisitorAdapter;
44 import org.sonar.ce.task.projectanalysis.filemove.MovedFilesRepository.OriginalFile;
45 import org.sonar.ce.task.step.ComputationStep;
46 import org.sonar.db.DbClient;
47 import org.sonar.db.DbSession;
48 import org.sonar.db.component.BranchDto;
49 import org.sonar.db.component.FileMoveRowDto;
51 import static java.util.stream.Collectors.toMap;
52 import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.POST_ORDER;
54 public class PullRequestFileMoveDetectionStep implements ComputationStep {
55 private static final Logger LOG = Loggers.get(PullRequestFileMoveDetectionStep.class);
57 private final AnalysisMetadataHolder analysisMetadataHolder;
58 private final TreeRootHolder rootHolder;
59 private final DbClient dbClient;
60 private final MutableMovedFilesRepository movedFilesRepository;
61 private final MutableAddedFileRepository addedFileRepository;
63 public PullRequestFileMoveDetectionStep(AnalysisMetadataHolder analysisMetadataHolder, TreeRootHolder rootHolder, DbClient dbClient,
64 MutableMovedFilesRepository movedFilesRepository, MutableAddedFileRepository addedFileRepository) {
65 this.analysisMetadataHolder = analysisMetadataHolder;
66 this.rootHolder = rootHolder;
67 this.dbClient = dbClient;
68 this.movedFilesRepository = movedFilesRepository;
69 this.addedFileRepository = addedFileRepository;
73 public String getDescription() {
74 return "Detect file moves in Pull Request scope";
78 public void execute(ComputationStep.Context context) {
79 if (!analysisMetadataHolder.isPullRequest()) {
80 LOG.debug("Currently not within Pull Request scope. Do nothing.");
84 Map<String, Component> reportFilesByUuid = getReportFilesByUuid(this.rootHolder.getRoot());
85 context.getStatistics().add("reportFiles", reportFilesByUuid.size());
87 if (reportFilesByUuid.isEmpty()) {
88 LOG.debug("No files in report. No file move detection.");
92 Map<String, DbComponent> targetBranchDbFilesByUuid = getTargetBranchDbFilesByUuid(analysisMetadataHolder);
93 context.getStatistics().add("dbFiles", targetBranchDbFilesByUuid.size());
95 if (targetBranchDbFilesByUuid.isEmpty()) {
96 registerNewlyAddedFiles(reportFilesByUuid);
97 context.getStatistics().add("addedFiles", reportFilesByUuid.size());
98 LOG.debug("Previous snapshot has no file. No file move detection.");
102 Map<String, Component> movedFilesByUuid = getMovedFilesByUuid(reportFilesByUuid);
103 context.getStatistics().add("movedFiles", movedFilesByUuid.size());
105 Map<String, Component> newlyAddedFilesByUuid = getNewlyAddedFilesByUuid(reportFilesByUuid, targetBranchDbFilesByUuid);
106 context.getStatistics().add("addedFiles", newlyAddedFilesByUuid.size());
108 registerMovedFiles(movedFilesByUuid.values(), targetBranchDbFilesByUuid.values());
109 registerNewlyAddedFiles(newlyAddedFilesByUuid);
112 private void registerMovedFiles(Collection<Component> movedFiles, Collection<DbComponent> dbFiles) {
114 .forEach(registerMovedFile(dbFiles));
117 private Consumer<Component> registerMovedFile(Collection<DbComponent> dbFiles) {
118 return movedFile -> retrieveDbFile(dbFiles, movedFile)
119 .ifPresent(dbFile -> movedFilesRepository.setOriginalPullRequestFile(movedFile, toOriginalFile(dbFile)));
122 private void registerNewlyAddedFiles(Map<String, Component> newAddedFilesByUuid) {
125 .forEach(addedFileRepository::register);
129 private Map<String, Component> getNewlyAddedFilesByUuid(Map<String, Component> reportFilesByUuid, Map<String, DbComponent> dbFilesByUuid) {
130 return reportFilesByUuid
133 .filter(file -> Objects.isNull(file.getOldName()))
134 .filter(file -> !dbFilesByUuid.containsKey(file.getUuid()))
135 .collect(toMap(Component::getUuid, Function.identity()));
138 private Map<String, Component> getMovedFilesByUuid(Map<String, Component> reportFilesByUuid) {
139 return reportFilesByUuid
142 .filter(file -> Objects.nonNull(file.getOldName()))
143 .collect(toMap(Component::getUuid, Function.identity()));
146 private Optional<DbComponent> retrieveDbFile(Collection<DbComponent> dbFiles, Component file) {
149 .filter(dbFile -> dbFile.getPath().equals(file.getOldName()))
153 public Set<String> difference(Set<String> set1, Set<String> set2) {
154 if (set1.isEmpty() || set2.isEmpty()) {
158 return Sets.difference(set1, set2).immutableCopy();
161 private Map<String, DbComponent> getTargetBranchDbFilesByUuid(AnalysisMetadataHolder analysisMetadataHolder) {
162 try (DbSession dbSession = dbClient.openSession(false)) {
163 String targetBranchUuid = getTargetBranchUuid(dbSession, analysisMetadataHolder.getProject().getUuid(), analysisMetadataHolder.getBranch().getTargetBranchName());
165 return getTargetBranchDbFiles(dbSession, targetBranchUuid)
167 .collect(toMap(DbComponent::getUuid, Function.identity()));
171 private List<DbComponent> getTargetBranchDbFiles(DbSession dbSession, String targetBranchUuid) {
172 List<DbComponent> files = new LinkedList<>();
174 ResultHandler<FileMoveRowDto> storeFileMove = resultContext -> {
175 FileMoveRowDto row = resultContext.getResultObject();
176 files.add(new DbComponent(row.getKey(), row.getUuid(), row.getPath(), row.getLineCount()));
179 dbClient.componentDao().scrollAllFilesForFileMove(dbSession, targetBranchUuid, storeFileMove);
184 private String getTargetBranchUuid(DbSession dbSession, String projectUuid, String targetBranchName) {
185 return dbClient.branchDao().selectByBranchKey(dbSession, projectUuid, targetBranchName)
186 .map(BranchDto::getUuid)
187 .orElseThrow(() -> new IllegalStateException("Pull Request has no target branch"));
190 private static Map<String, Component> getReportFilesByUuid(Component root) {
191 final ImmutableMap.Builder<String, Component> builder = ImmutableMap.builder();
193 new DepthTraversalTypeAwareCrawler(
194 new TypeAwareVisitorAdapter(CrawlerDepthLimit.FILE, POST_ORDER) {
196 public void visitFile(Component file) {
197 builder.put(file.getUuid(), file);
201 return builder.build();
204 private static OriginalFile toOriginalFile(DbComponent dbComponent) {
205 return new OriginalFile(dbComponent.getUuid(), dbComponent.getKey());
209 private static final class DbComponent {
210 private final String key;
211 private final String uuid;
212 private final String path;
213 private final int lineCount;
215 private DbComponent(String key, String uuid, String path, int lineCount) {
219 this.lineCount = lineCount;
222 public String getKey() {
226 public String getUuid() {
230 public String getPath() {
234 public int getLineCount() {