]> source.dussan.org Git - sonarqube.git/blob
acc598059a6d1cf7ffbc34ec8a57b40c61924b29
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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.server.computation.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.core.issue.DefaultIssue;
29 import org.sonar.core.issue.ShortBranchIssue;
30 import org.sonar.db.DbClient;
31 import org.sonar.db.DbSession;
32 import org.sonar.db.component.ComponentDto;
33 import org.sonar.db.issue.IssueChangeDto;
34 import org.sonar.db.issue.IssueDto;
35 import org.sonar.db.issue.ShortBranchIssueDto;
36 import org.sonar.server.computation.task.projectanalysis.component.Component;
37 import org.sonar.server.computation.task.projectanalysis.component.ShortBranchComponentsWithIssues;
38
39 import static java.util.stream.Collectors.groupingBy;
40 import static java.util.stream.Collectors.toMap;
41
42 public class ShortBranchIssuesLoader {
43
44   private final ShortBranchComponentsWithIssues shortBranchComponentsWithIssues;
45   private final DbClient dbClient;
46
47   public ShortBranchIssuesLoader(ShortBranchComponentsWithIssues shortBranchComponentsWithIssues, DbClient dbClient) {
48     this.shortBranchComponentsWithIssues = shortBranchComponentsWithIssues;
49     this.dbClient = dbClient;
50   }
51
52   public Collection<ShortBranchIssue> loadCandidateIssuesForMergingInTargetBranch(Component component) {
53     String componentKey = ComponentDto.removeBranchFromKey(component.getKey());
54     Set<String> uuids = shortBranchComponentsWithIssues.getUuids(componentKey);
55     if (uuids.isEmpty()) {
56       return Collections.emptyList();
57     }
58     try (DbSession session = dbClient.openSession(false)) {
59       return dbClient.issueDao().selectResolvedOrConfirmedByComponentUuids(session, uuids)
60         .stream()
61         .map(ShortBranchIssueDto::toShortBranchIssue)
62         .collect(Collectors.toList());
63     }
64   }
65
66   public Map<ShortBranchIssue, DefaultIssue> loadDefaultIssuesWithChanges(Collection<ShortBranchIssue> lightIssues) {
67     if (lightIssues.isEmpty()) {
68       return Collections.emptyMap();
69     }
70     Map<String, ShortBranchIssue> issuesByKey = lightIssues.stream().collect(Collectors.toMap(ShortBranchIssue::getKey, i -> i));
71     try (DbSession session = dbClient.openSession(false)) {
72
73       Map<String, List<IssueChangeDto>> changeDtoByIssueKey = dbClient.issueChangeDao()
74         .selectByIssueKeys(session, issuesByKey.keySet()).stream().collect(groupingBy(IssueChangeDto::getIssueKey));
75
76       return dbClient.issueDao().selectByKeys(session, issuesByKey.keySet())
77         .stream()
78         .map(IssueDto::toDefaultIssue)
79         .peek(i -> setChanges(changeDtoByIssueKey, i))
80         .collect(toMap(i -> issuesByKey.get(i.key()), i -> i));
81     }
82   }
83
84   private static void setChanges(Map<String, List<IssueChangeDto>> changeDtoByIssueKey, DefaultIssue i) {
85     changeDtoByIssueKey.get(i.key()).forEach(c -> {
86       switch (c.getChangeType()) {
87         case IssueChangeDto.TYPE_FIELD_CHANGE:
88           i.addChange(c.toFieldDiffs());
89           break;
90         case IssueChangeDto.TYPE_COMMENT:
91           i.addComment(c.toComment());
92           break;
93         default:
94           throw new IllegalStateException("Unknow change type: " + c.getChangeType());
95       }
96     });
97   }
98 }