3 * Copyright (C) 2009-2019 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.component;
22 import java.util.HashMap;
23 import java.util.List;
25 import java.util.Optional;
26 import javax.annotation.CheckForNull;
27 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
28 import org.sonar.db.DbClient;
29 import org.sonar.db.DbSession;
30 import org.sonar.db.component.BranchDto;
31 import org.sonar.db.component.ComponentDto;
33 import static com.google.common.base.Preconditions.checkState;
34 import static org.sonar.db.component.ComponentDto.removeBranchAndPullRequestFromKey;
37 * Cache a map between component keys and uuids in the merge branch and optionally the target branch (for PRs, and only if this target branch is analyzed)
39 public class MergeAndTargetBranchComponentUuids {
40 private final AnalysisMetadataHolder analysisMetadataHolder;
41 private final DbClient dbClient;
42 private Map<String, String> mergeBranchComponentsUuidsByKey;
43 private Map<String, String> targetBranchComponentsUuidsByKey;
44 private String mergeBranchName;
45 private boolean hasMergeBranchAnalysis;
46 private boolean hasTargetBranchAnalysis;
48 private String targetBranchUuid;
50 public MergeAndTargetBranchComponentUuids(AnalysisMetadataHolder analysisMetadataHolder, DbClient dbClient) {
51 this.analysisMetadataHolder = analysisMetadataHolder;
52 this.dbClient = dbClient;
55 private void lazyInit() {
56 if (mergeBranchComponentsUuidsByKey == null) {
57 String mergeBranchUuid = analysisMetadataHolder.getBranch().getMergeBranchUuid();
59 mergeBranchComponentsUuidsByKey = new HashMap<>();
60 targetBranchComponentsUuidsByKey = new HashMap<>();
62 try (DbSession dbSession = dbClient.openSession(false)) {
64 Optional<BranchDto> opt = dbClient.branchDao().selectByUuid(dbSession, mergeBranchUuid);
65 checkState(opt.isPresent(), "Merge branch '%s' does not exist", mergeBranchUuid);
66 mergeBranchName = opt.get().getKey();
68 initForMergeBranch(mergeBranchUuid, dbSession);
70 if (analysisMetadataHolder.isPullRequest()) {
71 initForTargetBranch(mergeBranchUuid, dbSession);
73 hasTargetBranchAnalysis = false;
79 private void initForTargetBranch(String mergeBranchUuid, DbSession dbSession) {
80 Optional<BranchDto> branchDtoOpt = dbClient.branchDao().selectByBranchKey(dbSession, analysisMetadataHolder.getProject().getUuid(),
81 analysisMetadataHolder.getBranch().getTargetBranchName());
82 targetBranchUuid = branchDtoOpt.map(BranchDto::getUuid).orElse(null);
83 hasTargetBranchAnalysis = targetBranchUuid != null && dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(dbSession, targetBranchUuid).isPresent();
84 if (hasTargetBranchAnalysis && !targetBranchUuid.equals(mergeBranchUuid)) {
85 List<ComponentDto> targetComponents = dbClient.componentDao().selectByProjectUuid(targetBranchUuid, dbSession);
86 for (ComponentDto dto : targetComponents) {
87 targetBranchComponentsUuidsByKey.put(dto.getKey(), dto.uuid());
92 private void initForMergeBranch(String mergeBranchUuid, DbSession dbSession) {
93 hasMergeBranchAnalysis = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(dbSession, mergeBranchUuid).isPresent();
95 if (hasMergeBranchAnalysis) {
96 List<ComponentDto> components = dbClient.componentDao().selectByProjectUuid(mergeBranchUuid, dbSession);
97 for (ComponentDto dto : components) {
98 mergeBranchComponentsUuidsByKey.put(dto.getKey(), dto.uuid());
103 public boolean hasMergeBranchAnalysis() {
105 return hasMergeBranchAnalysis;
108 public boolean hasTargetBranchAnalysis() {
110 return hasTargetBranchAnalysis;
113 public String getMergeBranchName() {
115 return mergeBranchName;
118 public boolean areTargetAndMergeBranchesDifferent() {
120 return targetBranchUuid == null || !analysisMetadataHolder.getBranch().getMergeBranchUuid().equals(targetBranchUuid);
124 public String getMergeBranchComponentUuid(String dbKey) {
126 String cleanComponentKey = removeBranchAndPullRequestFromKey(dbKey);
127 return mergeBranchComponentsUuidsByKey.get(cleanComponentKey);
131 public String getTargetBranchComponentUuid(String dbKey) {
133 String cleanComponentKey = removeBranchAndPullRequestFromKey(dbKey);
134 return targetBranchComponentsUuidsByKey.get(cleanComponentKey);