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.period;
22 import com.google.common.base.Preconditions;
23 import java.util.HashMap;
24 import java.util.List;
26 import java.util.Optional;
27 import javax.annotation.CheckForNull;
28 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
29 import org.sonar.db.DbClient;
30 import org.sonar.db.DbSession;
31 import org.sonar.db.component.BranchDto;
32 import org.sonar.db.component.ComponentDto;
33 import org.sonar.db.newcodeperiod.NewCodePeriodType;
35 import static org.sonar.db.component.ComponentDto.removeBranchAndPullRequestFromKey;
38 * Cache a map between component keys and uuids in the reference branch
40 public class NewCodeReferenceBranchComponentUuids {
41 private final DbClient dbClient;
42 private final AnalysisMetadataHolder analysisMetadataHolder;
43 private final PeriodHolder periodHolder;
44 private Map<String, String> referenceBranchComponentsUuidsByKey;
46 public NewCodeReferenceBranchComponentUuids(AnalysisMetadataHolder analysisMetadataHolder, PeriodHolder periodHolder, DbClient dbClient) {
47 this.analysisMetadataHolder = analysisMetadataHolder;
48 this.periodHolder = periodHolder;
49 this.dbClient = dbClient;
52 private void lazyInit() {
53 if (referenceBranchComponentsUuidsByKey == null) {
54 Preconditions.checkState(periodHolder.hasPeriod() && periodHolder.getPeriod().getMode().equals(NewCodePeriodType.REFERENCE_BRANCH.name()));
55 referenceBranchComponentsUuidsByKey = new HashMap<>();
57 try (DbSession dbSession = dbClient.openSession(false)) {
58 String referenceKey = periodHolder.getPeriod().getModeParameter() != null ? periodHolder.getPeriod().getModeParameter() : "";
59 Optional<BranchDto> opt = dbClient.branchDao().selectByBranchKey(dbSession, analysisMetadataHolder.getProject().getUuid(), referenceKey);
60 if (opt.isPresent()) {
61 init(opt.get().getUuid(), dbSession);
67 private void init(String referenceBranchUuid, DbSession dbSession) {
68 boolean hasReferenceBranchAnalysis = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(dbSession, referenceBranchUuid).isPresent();
70 if (hasReferenceBranchAnalysis) {
71 List<ComponentDto> components = dbClient.componentDao().selectByBranchUuid(referenceBranchUuid, dbSession);
72 for (ComponentDto dto : components) {
73 referenceBranchComponentsUuidsByKey.put(dto.getKey(), dto.uuid());
79 public String getComponentUuid(String dbKey) {
81 String cleanComponentKey = removeBranchAndPullRequestFromKey(dbKey);
82 return referenceBranchComponentsUuidsByKey.get(cleanComponentKey);