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.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 org.sonar.db.component.ComponentDto.removeBranchAndPullRequestFromKey;
36 * Cache a map between component keys and uuids in the merge branch and optionally the target branch (for PR and SLB, and only if this target branch is analyzed)
38 public class TargetBranchComponentUuids {
39 private final AnalysisMetadataHolder analysisMetadataHolder;
40 private final DbClient dbClient;
41 private Map<String, String> targetBranchComponentsUuidsByKey;
42 private boolean hasTargetBranchAnalysis;
44 public TargetBranchComponentUuids(AnalysisMetadataHolder analysisMetadataHolder, DbClient dbClient) {
45 this.analysisMetadataHolder = analysisMetadataHolder;
46 this.dbClient = dbClient;
49 private void lazyInit() {
50 if (targetBranchComponentsUuidsByKey == null) {
51 targetBranchComponentsUuidsByKey = new HashMap<>();
53 if (analysisMetadataHolder.isPullRequest()) {
54 try (DbSession dbSession = dbClient.openSession(false)) {
55 initForTargetBranch(dbSession);
58 hasTargetBranchAnalysis = false;
63 private void initForTargetBranch(DbSession dbSession) {
64 Optional<BranchDto> branchDtoOpt = dbClient.branchDao().selectByBranchKey(dbSession, analysisMetadataHolder.getProject().getUuid(),
65 analysisMetadataHolder.getBranch().getTargetBranchName());
66 String targetBranchUuid = branchDtoOpt.map(BranchDto::getUuid).orElse(null);
67 hasTargetBranchAnalysis = targetBranchUuid != null && dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(dbSession, targetBranchUuid).isPresent();
68 if (hasTargetBranchAnalysis) {
69 List<ComponentDto> targetComponents = dbClient.componentDao().selectByBranchUuid(targetBranchUuid, dbSession);
70 for (ComponentDto dto : targetComponents) {
71 targetBranchComponentsUuidsByKey.put(dto.getKey(), dto.uuid());
76 public boolean hasTargetBranchAnalysis() {
78 return hasTargetBranchAnalysis;
82 public String getTargetBranchComponentUuid(String key) {
84 return targetBranchComponentsUuidsByKey.get(key);