3 * Copyright (C) 2009-2017 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.server.computation.task.projectanalysis.issue;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.List;
27 import javax.annotation.Nullable;
29 import org.apache.commons.lang.StringUtils;
30 import org.sonar.core.issue.DefaultIssue;
31 import org.sonar.core.issue.tracking.Input;
32 import org.sonar.core.issue.tracking.LazyInput;
33 import org.sonar.core.issue.tracking.LineHashSequence;
34 import org.sonar.db.DbClient;
35 import org.sonar.db.DbSession;
36 import org.sonar.db.component.ComponentDto;
37 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
38 import org.sonar.server.computation.task.projectanalysis.component.Component;
40 public class TrackerMergeBranchInputFactory {
41 private static final LineHashSequence EMPTY_LINE_HASH_SEQUENCE = new LineHashSequence(Collections.<String>emptyList());
43 private final ComponentIssuesLoader mergeIssuesLoader;
44 private final DbClient dbClient;
45 private final AnalysisMetadataHolder analysisMetadataHolder;
46 private Map<String, String> uuidsByKey;
48 public TrackerMergeBranchInputFactory(ComponentIssuesLoader mergeIssuesLoader, AnalysisMetadataHolder analysisMetadataHolder, DbClient dbClient) {
49 this.mergeIssuesLoader = mergeIssuesLoader;
50 this.analysisMetadataHolder = analysisMetadataHolder;
51 this.dbClient = dbClient;
52 // TODO detect file moves?
55 private void loadMergeBranchComponents() {
56 String mergeBranchUuid = analysisMetadataHolder.getBranch().get().getMergeBranchUuid().get();
58 uuidsByKey = new HashMap<>();
59 try (DbSession dbSession = dbClient.openSession(false)) {
61 List<ComponentDto> components = dbClient.componentDao().selectByProjectUuid(mergeBranchUuid, dbSession);
62 for (ComponentDto dto : components) {
63 uuidsByKey.put(removeBranchFromKey(dto.getDbKey()), dto.uuid());
68 public Input<DefaultIssue> create(Component component) {
69 if (uuidsByKey == null) {
70 loadMergeBranchComponents();
73 String cleanComponentKey = removeBranchFromKey(component.getKey());
74 String mergeBranchComponentUuid = uuidsByKey.get(cleanComponentKey);
75 return new MergeLazyInput(component.getType(), mergeBranchComponentUuid);
78 private class MergeLazyInput extends LazyInput<DefaultIssue> {
79 private final Component.Type type;
80 private final String mergeBranchComponentUuid;
82 private MergeLazyInput(Component.Type type, @Nullable String mergeBranchComponentUuid) {
84 this.mergeBranchComponentUuid = mergeBranchComponentUuid;
88 protected LineHashSequence loadLineHashSequence() {
89 if (mergeBranchComponentUuid == 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, mergeBranchComponentUuid);
95 if (hashes == null || hashes.isEmpty()) {
96 return EMPTY_LINE_HASH_SEQUENCE;
98 return new LineHashSequence(hashes);
103 protected List<DefaultIssue> loadIssues() {
104 if (mergeBranchComponentUuid == null) {
105 return Collections.emptyList();
107 return mergeIssuesLoader.loadForComponentUuid(mergeBranchComponentUuid);
111 private static String removeBranchFromKey(String componentKey) {
112 return StringUtils.substringBeforeLast(componentKey, ":BRANCH:");