]> source.dussan.org Git - sonarqube.git/blob
fa392c144a3d16c6861147d889e698c967452f4e
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 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.ce.task.projectanalysis.filemove;
21
22 import com.google.common.collect.ImmutableMap;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.Map;
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;
44
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;
49
50 public class PullRequestFileMoveDetectionStep implements ComputationStep {
51   private static final Logger LOG = Loggers.get(PullRequestFileMoveDetectionStep.class);
52
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;
58
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;
66   }
67
68   @Override
69   public String getDescription() {
70     return "Detect file moves in Pull Request scope";
71   }
72
73   @Override
74   public void execute(ComputationStep.Context context) {
75     if (!analysisMetadataHolder.isPullRequest()) {
76       LOG.debug("Currently not within Pull Request scope. Do nothing.");
77       return;
78     }
79
80     Map<String, Component> reportFilesByUuid = getReportFilesByUuid(this.rootHolder.getRoot());
81     context.getStatistics().add("reportFiles", reportFilesByUuid.size());
82
83     if (reportFilesByUuid.isEmpty()) {
84       LOG.debug("No files in report. No file move detection.");
85       return;
86     }
87
88     Map<String, DbComponent> targetBranchDbFilesByUuid = getTargetBranchDbFilesByUuid(analysisMetadataHolder);
89     context.getStatistics().add("dbFiles", targetBranchDbFilesByUuid.size());
90
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.");
95       return;
96     }
97
98     Collection<Component> movedFiles = getMovedFilesByUuid(reportFilesByUuid);
99     context.getStatistics().add("movedFiles", movedFiles.size());
100
101     Map<String, Component> newlyAddedFilesByUuid = getNewlyAddedFilesByUuid(reportFilesByUuid, targetBranchDbFilesByUuid);
102     context.getStatistics().add("addedFiles", newlyAddedFilesByUuid.size());
103
104     Map<String, DbComponent> dbFilesByPathReference = toDbFilesByPathReferenceMap(targetBranchDbFilesByUuid.values());
105
106     registerMovedFiles(movedFiles, dbFilesByPathReference);
107     registerNewlyAddedFiles(newlyAddedFilesByUuid);
108   }
109
110   private void registerMovedFiles(Collection<Component> movedFiles, Map<String, DbComponent> dbFilesByPathReference) {
111     movedFiles
112       .forEach(movedFile -> registerMovedFile(dbFilesByPathReference, movedFile));
113   }
114
115   private void registerMovedFile(Map<String, DbComponent> dbFiles, Component movedFile) {
116     retrieveDbFile(dbFiles, movedFile)
117       .ifPresent(dbFile -> movedFilesRepository.setOriginalPullRequestFile(movedFile, toOriginalFile(dbFile)));
118   }
119
120   private void registerNewlyAddedFiles(Map<String, Component> newAddedFilesByUuid) {
121     newAddedFilesByUuid
122       .values()
123       .forEach(addedFileRepository::register);
124   }
125
126   private static Map<String, Component> getNewlyAddedFilesByUuid(Map<String, Component> reportFilesByUuid, Map<String, DbComponent> dbFilesByUuid) {
127     return reportFilesByUuid
128       .values()
129       .stream()
130       .filter(file -> Objects.isNull(file.getFileAttributes().getOldRelativePath()))
131       .filter(file -> !dbFilesByUuid.containsKey(file.getUuid()))
132       .collect(toMap(Component::getUuid, identity()));
133   }
134
135   private static Collection<Component> getMovedFilesByUuid(Map<String, Component> reportFilesByUuid) {
136     return reportFilesByUuid
137       .values()
138       .stream()
139       .filter(file -> Objects.nonNull(file.getFileAttributes().getOldRelativePath()))
140       .collect(toList());
141   }
142
143   private static Optional<DbComponent> retrieveDbFile(Map<String, DbComponent> dbFilesByPathReference, Component file) {
144     return Optional.ofNullable(dbFilesByPathReference.get(file.getFileAttributes().getOldRelativePath()));
145   }
146
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))
151         .orElse(Map.of());
152     }
153   }
154
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));
158     return files;
159   }
160
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);
165     };
166   }
167
168   private static DbComponent rowToDbComponent(FileMoveRowDto row) {
169     return new DbComponent(row.getKey(), row.getUuid(), row.getPath(), row.getLineCount());
170   }
171
172   private Optional<String> getTargetBranchUuid(DbSession dbSession, String projectUuid, String targetBranchName) {
173     return dbClient.branchDao().selectByBranchKey(dbSession, projectUuid, targetBranchName)
174       .map(BranchDto::getUuid);
175   }
176
177   private static Map<String, DbComponent> toDbFilesByPathReferenceMap(Collection<DbComponent> dbFiles) {
178     return dbFiles
179       .stream()
180       .collect(toMap(DbComponent::getPath, identity()));
181   }
182
183   private static Map<String, Component> getReportFilesByUuid(Component root) {
184     final ImmutableMap.Builder<String, Component> builder = ImmutableMap.builder();
185
186     new DepthTraversalTypeAwareCrawler(
187       new TypeAwareVisitorAdapter(CrawlerDepthLimit.FILE, POST_ORDER) {
188         @Override
189         public void visitFile(Component file) {
190           builder.put(file.getUuid(), file);
191         }
192       }).visit(root);
193
194     return builder.build();
195   }
196
197   private static OriginalFile toOriginalFile(DbComponent dbComponent) {
198     return new OriginalFile(dbComponent.getUuid(), dbComponent.getKey());
199   }
200 }