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 source branch of a pull request
38 public class SourceBranchComponentUuids {
39 private final AnalysisMetadataHolder analysisMetadataHolder;
40 private final DbClient dbClient;
41 private Map<String, String> sourceBranchComponentsUuidsByKey;
42 private boolean hasSourceBranchAnalysis;
44 public SourceBranchComponentUuids(AnalysisMetadataHolder analysisMetadataHolder, DbClient dbClient) {
45 this.analysisMetadataHolder = analysisMetadataHolder;
46 this.dbClient = dbClient;
49 private void lazyInit() {
50 if (sourceBranchComponentsUuidsByKey == null) {
51 sourceBranchComponentsUuidsByKey = new HashMap<>();
53 if (analysisMetadataHolder.isPullRequest()) {
54 try (DbSession dbSession = dbClient.openSession(false)) {
55 initForSourceBranch(dbSession);
58 hasSourceBranchAnalysis = false;
63 private void initForSourceBranch(DbSession dbSession) {
64 Optional<BranchDto> branchDtoOpt = dbClient.branchDao().selectByBranchKey(dbSession, analysisMetadataHolder.getProject().getUuid(),
65 analysisMetadataHolder.getBranch().getName());
66 String sourceBranchUuid = branchDtoOpt.map(BranchDto::getUuid).orElse(null);
67 hasSourceBranchAnalysis = sourceBranchUuid != null && dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(dbSession, sourceBranchUuid).isPresent();
68 if (hasSourceBranchAnalysis) {
69 List<ComponentDto> targetComponents = dbClient.componentDao().selectByProjectUuid(sourceBranchUuid, dbSession);
70 for (ComponentDto dto : targetComponents) {
71 sourceBranchComponentsUuidsByKey.put(dto.getKey(), dto.uuid());
76 public boolean hasSourceBranchAnalysis() {
78 return hasSourceBranchAnalysis;
82 public String getSourceBranchComponentUuid(String dbKey) {
84 String cleanComponentKey = removeBranchAndPullRequestFromKey(dbKey);
85 return sourceBranchComponentsUuidsByKey.get(cleanComponentKey);