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.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;
36 public class TrackerTargetBranchInputFactory {
37 private static final LineHashSequence EMPTY_LINE_HASH_SEQUENCE = new LineHashSequence(Collections.emptyList());
39 private final ComponentIssuesLoader componentIssuesLoader;
40 private final DbClient dbClient;
41 private final TargetBranchComponentUuids targetBranchComponentUuids;
42 private final MovedFilesRepository movedFilesRepository;
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?
53 public boolean hasTargetBranchAnalysis() {
54 return targetBranchComponentUuids.hasTargetBranchAnalysis();
57 public Input<DefaultIssue> createForTargetBranch(Component component) {
58 String targetBranchComponentUuid = getTargetBranchComponentUuid(component);
59 return new TargetLazyInput(component.getType(), targetBranchComponentUuid);
62 private String getTargetBranchComponentUuid(Component component) {
63 Optional<String> targetBranchOriginalComponentUuid = getOriginalComponentUuid(component);
65 if (targetBranchOriginalComponentUuid.isPresent()) {
66 return targetBranchComponentUuids.getTargetBranchComponentUuid(targetBranchOriginalComponentUuid.get());
69 return targetBranchComponentUuids.getTargetBranchComponentUuid(component.getKey());
72 private Optional<String> getOriginalComponentUuid(Component component) {
73 return movedFilesRepository
74 .getOriginalPullRequestFile(component)
75 .map(OriginalFile::getKey);
78 private class TargetLazyInput extends LazyInput<DefaultIssue> {
79 private final Component.Type type;
80 private final String targetBranchComponentUuid;
82 private TargetLazyInput(Component.Type type, @Nullable String targetBranchComponentUuid) {
84 this.targetBranchComponentUuid = targetBranchComponentUuid;
88 protected LineHashSequence loadLineHashSequence() {
89 if (targetBranchComponentUuid == null || type != Component.Type.FILE) {
90 return EMPTY_LINE_HASH_SEQUENCE;
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;
98 return new LineHashSequence(hashes);
103 protected List<DefaultIssue> loadIssues() {
104 if (targetBranchComponentUuid == null) {
105 return Collections.emptyList();
107 return componentIssuesLoader.loadOpenIssuesWithChanges(targetBranchComponentUuid);