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.Collection;
23 import java.util.Collections;
24 import java.util.List;
27 import java.util.stream.Collectors;
28 import org.sonar.ce.task.projectanalysis.component.Component;
29 import org.sonar.ce.task.projectanalysis.component.SiblingComponentsWithOpenIssues;
30 import org.sonar.core.issue.DefaultIssue;
31 import org.sonar.db.DbClient;
32 import org.sonar.db.DbSession;
33 import org.sonar.db.component.ComponentDto;
34 import org.sonar.db.issue.IssueDto;
35 import org.sonar.db.issue.PrIssueDto;
37 import static org.sonar.api.utils.DateUtils.longToDate;
38 import static org.sonar.core.util.stream.MoreCollectors.toList;
39 import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
41 public class SiblingsIssuesLoader {
43 private final SiblingComponentsWithOpenIssues siblingComponentsWithOpenIssues;
44 private final DbClient dbClient;
45 private final ComponentIssuesLoader componentIssuesLoader;
47 public SiblingsIssuesLoader(SiblingComponentsWithOpenIssues siblingComponentsWithOpenIssues, DbClient dbClient,
48 ComponentIssuesLoader componentIssuesLoader) {
49 this.siblingComponentsWithOpenIssues = siblingComponentsWithOpenIssues;
50 this.dbClient = dbClient;
51 this.componentIssuesLoader = componentIssuesLoader;
54 public Collection<SiblingIssue> loadCandidateSiblingIssuesForMerging(Component component) {
55 String componentKey = ComponentDto.removeBranchAndPullRequestFromKey(component.getKey());
56 Set<String> uuids = siblingComponentsWithOpenIssues.getUuids(componentKey);
57 if (uuids.isEmpty()) {
58 return Collections.emptyList();
61 try (DbSession session = dbClient.openSession(false)) {
62 return dbClient.issueDao().selectOpenByComponentUuids(session, uuids)
64 .map(SiblingsIssuesLoader::toSiblingIssue)
65 .collect(Collectors.toList());
69 private static SiblingIssue toSiblingIssue(PrIssueDto dto) {
70 return new SiblingIssue(dto.getKey(), dto.getLine(), dto.getMessage(), dto.getChecksum(), dto.getRuleKey(), dto.getStatus(), dto.getBranchKey(),
71 dto.getBranchType(), longToDate(dto.getIssueUpdateDate()));
74 public Map<SiblingIssue, DefaultIssue> loadDefaultIssuesWithChanges(Collection<SiblingIssue> lightIssues) {
75 if (lightIssues.isEmpty()) {
76 return Collections.emptyMap();
79 Map<String, SiblingIssue> issuesByKey = lightIssues.stream().collect(Collectors.toMap(SiblingIssue::getKey, i -> i));
80 try (DbSession session = dbClient.openSession(false)) {
81 List<DefaultIssue> issues = dbClient.issueDao().selectByKeys(session, issuesByKey.keySet())
83 .map(IssueDto::toDefaultIssue)
84 .collect(toList(issuesByKey.size()));
85 componentIssuesLoader.loadChanges(session, issues);
86 return issues.stream()
87 .collect(uniqueIndex(i -> issuesByKey.get(i.key()), i -> i, issues.size()));