3 * Copyright (C) 2009-2017 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.server.computation.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.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;
39 import static java.util.stream.Collectors.groupingBy;
40 import static java.util.stream.Collectors.toMap;
42 public class ShortBranchIssuesLoader {
44 private final ShortBranchComponentsWithIssues shortBranchComponentsWithIssues;
45 private final DbClient dbClient;
47 public ShortBranchIssuesLoader(ShortBranchComponentsWithIssues shortBranchComponentsWithIssues, DbClient dbClient) {
48 this.shortBranchComponentsWithIssues = shortBranchComponentsWithIssues;
49 this.dbClient = dbClient;
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();
58 try (DbSession session = dbClient.openSession(false)) {
59 return dbClient.issueDao().selectResolvedOrConfirmedByComponentUuids(session, uuids)
61 .map(ShortBranchIssueDto::toShortBranchIssue)
62 .collect(Collectors.toList());
66 public Map<ShortBranchIssue, DefaultIssue> loadDefaultIssuesWithChanges(Collection<ShortBranchIssue> lightIssues) {
67 if (lightIssues.isEmpty()) {
68 return Collections.emptyMap();
70 Map<String, ShortBranchIssue> issuesByKey = lightIssues.stream().collect(Collectors.toMap(ShortBranchIssue::getKey, i -> i));
71 try (DbSession session = dbClient.openSession(false)) {
73 Map<String, List<IssueChangeDto>> changeDtoByIssueKey = dbClient.issueChangeDao()
74 .selectByIssueKeys(session, issuesByKey.keySet()).stream().collect(groupingBy(IssueChangeDto::getIssueKey));
76 return dbClient.issueDao().selectByKeys(session, issuesByKey.keySet())
78 .map(IssueDto::toDefaultIssue)
79 .peek(i -> setChanges(changeDtoByIssueKey, i))
80 .collect(toMap(i -> issuesByKey.get(i.key()), i -> i));
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());
90 case IssueChangeDto.TYPE_COMMENT:
91 i.addComment(c.toComment());
94 throw new IllegalStateException("Unknow change type: " + c.getChangeType());