]> source.dussan.org Git - sonarqube.git/blob
60c94d1f999a764bfded4bf11fb12ee997a9c67d
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.ce.task.projectanalysis.issue;
21
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
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;
36
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;
40
41 public class SiblingsIssuesLoader {
42
43   private final SiblingComponentsWithOpenIssues siblingComponentsWithOpenIssues;
44   private final DbClient dbClient;
45   private final ComponentIssuesLoader componentIssuesLoader;
46
47   public SiblingsIssuesLoader(SiblingComponentsWithOpenIssues siblingComponentsWithOpenIssues, DbClient dbClient,
48     ComponentIssuesLoader componentIssuesLoader) {
49     this.siblingComponentsWithOpenIssues = siblingComponentsWithOpenIssues;
50     this.dbClient = dbClient;
51     this.componentIssuesLoader = componentIssuesLoader;
52   }
53
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();
59     }
60
61     try (DbSession session = dbClient.openSession(false)) {
62       return dbClient.issueDao().selectOpenByComponentUuids(session, uuids)
63         .stream()
64         .map(SiblingsIssuesLoader::toSiblingIssue)
65         .collect(Collectors.toList());
66     }
67   }
68
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()));
72   }
73
74   public Map<SiblingIssue, DefaultIssue> loadDefaultIssuesWithChanges(Collection<SiblingIssue> lightIssues) {
75     if (lightIssues.isEmpty()) {
76       return Collections.emptyMap();
77     }
78
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())
82         .stream()
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()));
88     }
89   }
90
91 }