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.Collections;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.List;
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.KeyWithUuidDto;
33 import static org.sonar.db.component.ComponentDto.removeBranchAndPullRequestFromKey;
36 * Cache a map of component key -> set<uuid> in:
37 * - sibling PRs that have open issues
38 * - branches that use reference branch as new code period setting and have it set to currently analysed branch
40 public class SiblingComponentsWithOpenIssues {
41 private final DbClient dbClient;
42 private final AnalysisMetadataHolder metadataHolder;
43 private final TreeRootHolder treeRootHolder;
45 private Map<String, Set<String>> uuidsByKey;
47 public SiblingComponentsWithOpenIssues(TreeRootHolder treeRootHolder, AnalysisMetadataHolder metadataHolder, DbClient dbClient) {
48 this.treeRootHolder = treeRootHolder;
49 this.metadataHolder = metadataHolder;
50 this.dbClient = dbClient;
53 private void loadUuidsByKey() {
54 String currentBranchUuid = treeRootHolder.getRoot().getUuid();
55 String referenceBranchUuid;
57 uuidsByKey = new HashMap<>();
59 try (DbSession dbSession = dbClient.openSession(false)) {
60 if (metadataHolder.isPullRequest()) {
61 referenceBranchUuid = metadataHolder.getBranch().getReferenceBranchUuid();
63 referenceBranchUuid = currentBranchUuid;
64 addComponentsFromBranchesThatUseCurrentBranchAsNewCodePeriodReferenceAndHaveOpenIssues(dbSession);
67 addComponentsFromPullRequestsTargetingCurrentBranchThatHaveOpenIssues(dbSession, referenceBranchUuid, currentBranchUuid);
71 private void addComponentsFromBranchesThatUseCurrentBranchAsNewCodePeriodReferenceAndHaveOpenIssues(DbSession dbSession) {
72 String projectUuid = metadataHolder.getProject().getUuid();
73 String currentBranchName = metadataHolder.getBranch().getName();
75 Set<String> branchUuids = dbClient.newCodePeriodDao().selectBranchesReferencing(dbSession, projectUuid, currentBranchName);
77 List<KeyWithUuidDto> components = dbClient.componentDao().selectComponentsFromBranchesThatHaveOpenIssues(dbSession, branchUuids);
78 for (KeyWithUuidDto dto : components) {
79 uuidsByKey.computeIfAbsent(removeBranchAndPullRequestFromKey(dto.key()), s -> new HashSet<>()).add(dto.uuid());
83 private void addComponentsFromPullRequestsTargetingCurrentBranchThatHaveOpenIssues(DbSession dbSession, String referenceBranchUuid, String currentBranchUuid) {
84 List<KeyWithUuidDto> components = dbClient.componentDao().selectComponentsFromPullRequestsTargetingCurrentBranchThatHaveOpenIssues(
85 dbSession, referenceBranchUuid, currentBranchUuid);
86 for (KeyWithUuidDto dto : components) {
87 uuidsByKey.computeIfAbsent(removeBranchAndPullRequestFromKey(dto.key()), s -> new HashSet<>()).add(dto.uuid());
91 public Set<String> getUuids(String componentKey) {
92 if (uuidsByKey == null) {
96 return uuidsByKey.getOrDefault(componentKey, Collections.emptySet());