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.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 reference branch
39 public class ReferenceBranchComponentUuids {
40 private final AnalysisMetadataHolder analysisMetadataHolder;
41 private final DbClient dbClient;
42 private Map<String, String> referenceBranchComponentsUuidsByKey;
43 private String referenceBranchName;
44 private boolean hasReferenceBranchAnalysis;
46 public ReferenceBranchComponentUuids(AnalysisMetadataHolder analysisMetadataHolder, DbClient dbClient) {
47 this.analysisMetadataHolder = analysisMetadataHolder;
48 this.dbClient = dbClient;
51 private void lazyInit() {
52 if (referenceBranchComponentsUuidsByKey == null) {
53 String referenceBranchUuid = analysisMetadataHolder.getBranch().getReferenceBranchUuid();
55 referenceBranchComponentsUuidsByKey = new HashMap<>();
57 try (DbSession dbSession = dbClient.openSession(false)) {
59 Optional<BranchDto> opt = dbClient.branchDao().selectByUuid(dbSession, referenceBranchUuid);
60 checkState(opt.isPresent(), "Reference branch '%s' does not exist", referenceBranchUuid);
61 referenceBranchName = opt.get().getKey();
63 init(referenceBranchUuid, dbSession);
68 private void init(String referenceBranchUuid, DbSession dbSession) {
69 hasReferenceBranchAnalysis = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(dbSession, referenceBranchUuid).isPresent();
71 if (hasReferenceBranchAnalysis) {
72 List<ComponentDto> components = dbClient.componentDao().selectByProjectUuid(referenceBranchUuid, dbSession);
73 for (ComponentDto dto : components) {
74 referenceBranchComponentsUuidsByKey.put(dto.getKey(), dto.uuid());
79 public boolean hasReferenceBranchAnalysis() {
81 return hasReferenceBranchAnalysis;
84 public String getReferenceBranchName() {
86 return referenceBranchName;
90 public String getComponentUuid(String dbKey) {
92 String cleanComponentKey = removeBranchAndPullRequestFromKey(dbKey);
93 return referenceBranchComponentsUuidsByKey.get(cleanComponentKey);