]> source.dussan.org Git - sonarqube.git/blob
b70917e74520e9c7978e9425c58ec26c10d57f83
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2021 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.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;
38
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;
42
43 public class SiblingsIssuesLoader {
44
45   private final SiblingComponentsWithOpenIssues siblingComponentsWithOpenIssues;
46   private final DbClient dbClient;
47   private final ComponentIssuesLoader componentIssuesLoader;
48
49   public SiblingsIssuesLoader(SiblingComponentsWithOpenIssues siblingComponentsWithOpenIssues, DbClient dbClient,
50     ComponentIssuesLoader componentIssuesLoader) {
51     this.siblingComponentsWithOpenIssues = siblingComponentsWithOpenIssues;
52     this.dbClient = dbClient;
53     this.componentIssuesLoader = componentIssuesLoader;
54   }
55
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();
61     }
62
63     try (DbSession session = dbClient.openSession(false)) {
64       return dbClient.issueDao().selectOpenByComponentUuids(session, uuids)
65         .stream()
66         .map(SiblingsIssuesLoader::toSiblingIssue)
67         .collect(Collectors.toList());
68     }
69   }
70
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()));
75   }
76
77   public Map<SiblingIssue, DefaultIssue> loadDefaultIssuesWithChanges(Collection<SiblingIssue> lightIssues) {
78     if (lightIssues.isEmpty()) {
79       return Collections.emptyMap();
80     }
81
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())
85         .stream()
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()));
91     }
92   }
93
94 }