3 * Copyright (C) 2009-2023 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 java.util.Collection;
24 import java.util.HashMap;
26 import java.util.Objects;
27 import java.util.Optional;
28 import org.apache.ibatis.session.ResultHandler;
29 import org.sonar.api.utils.log.Logger;
30 import org.sonar.api.utils.log.Loggers;
31 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
32 import org.sonar.ce.task.projectanalysis.component.Component;
33 import org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit;
34 import org.sonar.ce.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
35 import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
36 import org.sonar.ce.task.projectanalysis.component.TypeAwareVisitorAdapter;
37 import org.sonar.ce.task.projectanalysis.filemove.FileMoveDetectionStep.DbComponent;
38 import org.sonar.ce.task.projectanalysis.filemove.MovedFilesRepository.OriginalFile;
39 import org.sonar.ce.task.step.ComputationStep;
40 import org.sonar.db.DbClient;
41 import org.sonar.db.DbSession;
42 import org.sonar.db.component.BranchDto;
43 import org.sonar.db.component.FileMoveRowDto;
45 import static java.util.function.Function.identity;
46 import static java.util.stream.Collectors.toList;
47 import static java.util.stream.Collectors.toMap;
48 import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.POST_ORDER;
50 public class PullRequestFileMoveDetectionStep implements ComputationStep {
51 private static final Logger LOG = Loggers.get(PullRequestFileMoveDetectionStep.class);
53 private final AnalysisMetadataHolder analysisMetadataHolder;
54 private final TreeRootHolder rootHolder;
55 private final DbClient dbClient;
56 private final MutableMovedFilesRepository movedFilesRepository;
57 private final MutableAddedFileRepository addedFileRepository;
59 public PullRequestFileMoveDetectionStep(AnalysisMetadataHolder analysisMetadataHolder, TreeRootHolder rootHolder, DbClient dbClient,
60 MutableMovedFilesRepository movedFilesRepository, MutableAddedFileRepository addedFileRepository) {
61 this.analysisMetadataHolder = analysisMetadataHolder;
62 this.rootHolder = rootHolder;
63 this.dbClient = dbClient;
64 this.movedFilesRepository = movedFilesRepository;
65 this.addedFileRepository = addedFileRepository;
69 public String getDescription() {
70 return "Detect file moves in Pull Request scope";
74 public void execute(ComputationStep.Context context) {
75 if (!analysisMetadataHolder.isPullRequest()) {
76 LOG.debug("Currently not within Pull Request scope. Do nothing.");
80 Map<String, Component> reportFilesByUuid = getReportFilesByUuid(this.rootHolder.getRoot());
81 context.getStatistics().add("reportFiles", reportFilesByUuid.size());
83 if (reportFilesByUuid.isEmpty()) {
84 LOG.debug("No files in report. No file move detection.");
88 Map<String, DbComponent> targetBranchDbFilesByUuid = getTargetBranchDbFilesByUuid(analysisMetadataHolder);
89 context.getStatistics().add("dbFiles", targetBranchDbFilesByUuid.size());
91 if (targetBranchDbFilesByUuid.isEmpty()) {
92 registerNewlyAddedFiles(reportFilesByUuid);
93 context.getStatistics().add("addedFiles", reportFilesByUuid.size());
94 LOG.debug("Target branch has no files. No file move detection.");
98 Collection<Component> movedFiles = getMovedFilesByUuid(reportFilesByUuid);
99 context.getStatistics().add("movedFiles", movedFiles.size());
101 Map<String, Component> newlyAddedFilesByUuid = getNewlyAddedFilesByUuid(reportFilesByUuid, targetBranchDbFilesByUuid);
102 context.getStatistics().add("addedFiles", newlyAddedFilesByUuid.size());
104 Map<String, DbComponent> dbFilesByPathReference = toDbFilesByPathReferenceMap(targetBranchDbFilesByUuid.values());
106 registerMovedFiles(movedFiles, dbFilesByPathReference);
107 registerNewlyAddedFiles(newlyAddedFilesByUuid);
110 private void registerMovedFiles(Collection<Component> movedFiles, Map<String, DbComponent> dbFilesByPathReference) {
112 .forEach(movedFile -> registerMovedFile(dbFilesByPathReference, movedFile));
115 private void registerMovedFile(Map<String, DbComponent> dbFiles, Component movedFile) {
116 retrieveDbFile(dbFiles, movedFile)
117 .ifPresent(dbFile -> movedFilesRepository.setOriginalPullRequestFile(movedFile, toOriginalFile(dbFile)));
120 private void registerNewlyAddedFiles(Map<String, Component> newAddedFilesByUuid) {
123 .forEach(addedFileRepository::register);
126 private static Map<String, Component> getNewlyAddedFilesByUuid(Map<String, Component> reportFilesByUuid, Map<String, DbComponent> dbFilesByUuid) {
127 return reportFilesByUuid
130 .filter(file -> Objects.isNull(file.getFileAttributes().getOldRelativePath()))
131 .filter(file -> !dbFilesByUuid.containsKey(file.getUuid()))
132 .collect(toMap(Component::getUuid, identity()));
135 private static Collection<Component> getMovedFilesByUuid(Map<String, Component> reportFilesByUuid) {
136 return reportFilesByUuid
139 .filter(file -> Objects.nonNull(file.getFileAttributes().getOldRelativePath()))
143 private static Optional<DbComponent> retrieveDbFile(Map<String, DbComponent> dbFilesByPathReference, Component file) {
144 return Optional.ofNullable(dbFilesByPathReference.get(file.getFileAttributes().getOldRelativePath()));
147 private Map<String, DbComponent> getTargetBranchDbFilesByUuid(AnalysisMetadataHolder analysisMetadataHolder) {
148 try (DbSession dbSession = dbClient.openSession(false)) {
149 return getTargetBranchUuid(dbSession, analysisMetadataHolder.getProject().getUuid(), analysisMetadataHolder.getBranch().getTargetBranchName())
150 .map(targetBranchUUid -> getTargetBranchDbFilesByUuid(dbSession, targetBranchUUid))
155 private Map<String, DbComponent> getTargetBranchDbFilesByUuid(DbSession dbSession, String targetBranchUuid) {
156 Map<String, DbComponent> files = new HashMap<>();
157 dbClient.componentDao().scrollAllFilesForFileMove(dbSession, targetBranchUuid, accumulateFilesForFileMove(files));
161 private static ResultHandler<FileMoveRowDto> accumulateFilesForFileMove(Map<String, DbComponent> accumulator) {
162 return resultContext -> {
163 DbComponent component = rowToDbComponent(resultContext.getResultObject());
164 accumulator.put(component.getUuid(), component);
168 private static DbComponent rowToDbComponent(FileMoveRowDto row) {
169 return new DbComponent(row.getKey(), row.getUuid(), row.getPath(), row.getLineCount());
172 private Optional<String> getTargetBranchUuid(DbSession dbSession, String projectUuid, String targetBranchName) {
173 return dbClient.branchDao().selectByBranchKey(dbSession, projectUuid, targetBranchName)
174 .map(BranchDto::getUuid);
177 private static Map<String, DbComponent> toDbFilesByPathReferenceMap(Collection<DbComponent> dbFiles) {
180 .collect(toMap(DbComponent::getPath, identity()));
183 private static Map<String, Component> getReportFilesByUuid(Component root) {
184 final ImmutableMap.Builder<String, Component> builder = ImmutableMap.builder();
186 new DepthTraversalTypeAwareCrawler(
187 new TypeAwareVisitorAdapter(CrawlerDepthLimit.FILE, POST_ORDER) {
189 public void visitFile(Component file) {
190 builder.put(file.getUuid(), file);
194 return builder.build();
197 private static OriginalFile toOriginalFile(DbComponent dbComponent) {
198 return new OriginalFile(dbComponent.getUuid(), dbComponent.getKey());