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.issue;
22 import java.util.Date;
23 import java.util.Optional;
24 import org.sonar.ce.task.projectanalysis.component.Component;
25 import org.sonar.core.issue.DefaultIssue;
26 import org.sonar.core.issue.IssueChangeContext;
27 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
28 import org.sonar.ce.task.projectanalysis.filemove.MovedFilesRepository;
29 import org.sonar.ce.task.projectanalysis.filemove.MovedFilesRepository.OriginalFile;
30 import org.sonar.server.issue.IssueFieldsSetter;
32 import static com.google.common.base.Preconditions.checkState;
34 public class MovedIssueVisitor extends IssueVisitor {
35 private final AnalysisMetadataHolder analysisMetadataHolder;
36 private final MovedFilesRepository movedFilesRepository;
37 private final IssueFieldsSetter issueUpdater;
39 public MovedIssueVisitor(AnalysisMetadataHolder analysisMetadataHolder, MovedFilesRepository movedFilesRepository, IssueFieldsSetter issueUpdater) {
40 this.analysisMetadataHolder = analysisMetadataHolder;
41 this.movedFilesRepository = movedFilesRepository;
42 this.issueUpdater = issueUpdater;
46 public void onIssue(Component component, DefaultIssue issue) {
47 if (component.getType() != Component.Type.FILE || component.getUuid().equals(issue.componentUuid())) {
50 Optional<OriginalFile> originalFileOptional = movedFilesRepository.getOriginalFile(component);
51 checkState(originalFileOptional.isPresent(),
52 "Issue %s for component %s has a different component key but no original file exist in MovedFilesRepository",
54 OriginalFile originalFile = originalFileOptional.get();
55 checkState(originalFile.getUuid().equals(issue.componentUuid()),
56 "Issue %s doesn't belong to file %s registered as original file of current file %s",
57 issue, originalFile.getUuid(), component);
59 // changes the issue's component uuid, add a change and set issue as changed to enforce it is persisted to DB
60 issueUpdater.setIssueMoved(issue, component.getUuid(), IssueChangeContext.createUser(new Date(analysisMetadataHolder.getAnalysisDate()), null));
61 // other fields (such as module, modulePath, componentKey) are read-only and set/reset for consistency only
62 issue.setComponentKey(component.getKey());
63 issue.setModuleUuidPath(null);