]> source.dussan.org Git - sonarqube.git/blob
a1f513c8371a741986a4ff84ebd0e610db8cd660
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 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.issue;
21
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Optional;
25 import javax.annotation.Nullable;
26 import org.sonar.ce.task.projectanalysis.component.Component;
27 import org.sonar.ce.task.projectanalysis.filemove.MovedFilesRepository;
28 import org.sonar.ce.task.projectanalysis.filemove.MovedFilesRepository.OriginalFile;
29 import org.sonar.core.issue.DefaultIssue;
30 import org.sonar.core.issue.tracking.Input;
31 import org.sonar.core.issue.tracking.LazyInput;
32 import org.sonar.core.issue.tracking.LineHashSequence;
33 import org.sonar.db.DbClient;
34 import org.sonar.db.DbSession;
35
36 public class TrackerTargetBranchInputFactory {
37   private static final LineHashSequence EMPTY_LINE_HASH_SEQUENCE = new LineHashSequence(Collections.emptyList());
38
39   private final ComponentIssuesLoader componentIssuesLoader;
40   private final DbClient dbClient;
41   private final TargetBranchComponentUuids targetBranchComponentUuids;
42   private final MovedFilesRepository movedFilesRepository;
43
44   public TrackerTargetBranchInputFactory(ComponentIssuesLoader componentIssuesLoader, TargetBranchComponentUuids targetBranchComponentUuids,
45     DbClient dbClient, MovedFilesRepository movedFilesRepository) {
46     this.componentIssuesLoader = componentIssuesLoader;
47     this.targetBranchComponentUuids = targetBranchComponentUuids;
48     this.dbClient = dbClient;
49     this.movedFilesRepository = movedFilesRepository;
50     // TODO detect file moves?
51   }
52
53   public boolean hasTargetBranchAnalysis() {
54     return targetBranchComponentUuids.hasTargetBranchAnalysis();
55   }
56
57   public Input<DefaultIssue> createForTargetBranch(Component component) {
58     String targetBranchComponentUuid = getTargetBranchComponentUuid(component);
59     return new TargetLazyInput(component.getType(), targetBranchComponentUuid);
60   }
61
62   private String getTargetBranchComponentUuid(Component component) {
63     Optional<String> targetBranchOriginalComponentUuid = getOriginalComponentUuid(component);
64
65     if (targetBranchOriginalComponentUuid.isPresent()) {
66       return targetBranchComponentUuids.getTargetBranchComponentUuid(targetBranchOriginalComponentUuid.get());
67     }
68
69     return targetBranchComponentUuids.getTargetBranchComponentUuid(component.getKey());
70   }
71
72   private Optional<String> getOriginalComponentUuid(Component component) {
73     return movedFilesRepository
74       .getOriginalPullRequestFile(component)
75       .map(OriginalFile::getKey);
76   }
77
78   private class TargetLazyInput extends LazyInput<DefaultIssue> {
79     private final Component.Type type;
80     private final String targetBranchComponentUuid;
81
82     private TargetLazyInput(Component.Type type, @Nullable String targetBranchComponentUuid) {
83       this.type = type;
84       this.targetBranchComponentUuid = targetBranchComponentUuid;
85     }
86
87     @Override
88     protected LineHashSequence loadLineHashSequence() {
89       if (targetBranchComponentUuid == null || type != Component.Type.FILE) {
90         return EMPTY_LINE_HASH_SEQUENCE;
91       }
92
93       try (DbSession session = dbClient.openSession(false)) {
94         List<String> hashes = dbClient.fileSourceDao().selectLineHashes(session, targetBranchComponentUuid);
95         if (hashes == null || hashes.isEmpty()) {
96           return EMPTY_LINE_HASH_SEQUENCE;
97         }
98         return new LineHashSequence(hashes);
99       }
100     }
101
102     @Override
103     protected List<DefaultIssue> loadIssues() {
104       if (targetBranchComponentUuid == null) {
105         return Collections.emptyList();
106       }
107       return componentIssuesLoader.loadOpenIssuesWithChanges(targetBranchComponentUuid);
108     }
109   }
110
111 }