3 * Copyright (C) 2009-2021 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.api.utils.Preconditions;
29 import org.sonar.ce.task.projectanalysis.component.Component;
30 import org.sonar.ce.task.projectanalysis.component.SiblingComponentsWithOpenIssues;
31 import org.sonar.core.issue.DefaultIssue;
32 import org.sonar.db.DbClient;
33 import org.sonar.db.DbSession;
34 import org.sonar.db.component.BranchType;
35 import org.sonar.db.component.ComponentDto;
36 import org.sonar.db.issue.IssueDto;
37 import org.sonar.db.issue.PrIssueDto;
39 import static org.sonar.api.utils.DateUtils.longToDate;
40 import static org.sonar.core.util.stream.MoreCollectors.toList;
41 import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
43 public class SiblingsIssuesLoader {
45 private final SiblingComponentsWithOpenIssues siblingComponentsWithOpenIssues;
46 private final DbClient dbClient;
47 private final ComponentIssuesLoader componentIssuesLoader;
49 public SiblingsIssuesLoader(SiblingComponentsWithOpenIssues siblingComponentsWithOpenIssues, DbClient dbClient,
50 ComponentIssuesLoader componentIssuesLoader) {
51 this.siblingComponentsWithOpenIssues = siblingComponentsWithOpenIssues;
52 this.dbClient = dbClient;
53 this.componentIssuesLoader = componentIssuesLoader;
56 public Collection<SiblingIssue> loadCandidateSiblingIssuesForMerging(Component component) {
57 String componentKey = ComponentDto.removeBranchAndPullRequestFromKey(component.getDbKey());
58 Set<String> uuids = siblingComponentsWithOpenIssues.getUuids(componentKey);
59 if (uuids.isEmpty()) {
60 return Collections.emptyList();
63 try (DbSession session = dbClient.openSession(false)) {
64 return dbClient.issueDao().selectOpenByComponentUuids(session, uuids)
66 .map(SiblingsIssuesLoader::toSiblingIssue)
67 .collect(Collectors.toList());
71 private static SiblingIssue toSiblingIssue(PrIssueDto dto) {
72 Preconditions.checkState(dto.getBranchType().equals(BranchType.PULL_REQUEST), "Expected all issues to belong to P/Rs");
73 return new SiblingIssue(dto.getKey(), dto.getLine(), dto.getMessage(), dto.getChecksum(), dto.getRuleKey(), dto.getStatus(), dto.getBranchKey(),
74 longToDate(dto.getIssueUpdateDate()));
77 public Map<SiblingIssue, DefaultIssue> loadDefaultIssuesWithChanges(Collection<SiblingIssue> lightIssues) {
78 if (lightIssues.isEmpty()) {
79 return Collections.emptyMap();
82 Map<String, SiblingIssue> issuesByKey = lightIssues.stream().collect(Collectors.toMap(SiblingIssue::getKey, i -> i));
83 try (DbSession session = dbClient.openSession(false)) {
84 List<DefaultIssue> issues = dbClient.issueDao().selectByKeys(session, issuesByKey.keySet())
86 .map(IssueDto::toDefaultIssue)
87 .collect(toList(issuesByKey.size()));
88 componentIssuesLoader.loadChanges(session, issues);
89 return issues.stream()
90 .collect(uniqueIndex(i -> issuesByKey.get(i.key()), i -> i, issues.size()));