3 * Copyright (C) 2009-2019 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.qualitygate.changeevent;
22 import com.google.common.collect.Multimap;
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.List;
26 import java.util.Objects;
28 import org.sonar.api.issue.Issue;
29 import org.sonar.api.rules.RuleType;
30 import org.sonar.api.utils.log.Logger;
31 import org.sonar.api.utils.log.Loggers;
32 import org.sonar.core.issue.DefaultIssue;
33 import org.sonar.core.util.stream.MoreCollectors;
34 import org.sonar.server.qualitygate.changeevent.QGChangeEventListener.ChangedIssue;
36 import static java.lang.String.format;
37 import static org.sonar.core.util.stream.MoreCollectors.toSet;
40 * Broadcast a given collection of {@link QGChangeEvent} for a specific trigger to all the registered
41 * {@link QGChangeEventListener} in Pico.
43 * This class ensures that an {@link Exception} occurring calling one of the {@link QGChangeEventListener} doesn't
44 * prevent from calling the others.
46 public class QGChangeEventListenersImpl implements QGChangeEventListeners {
47 private static final Logger LOG = Loggers.get(QGChangeEventListenersImpl.class);
49 private final QGChangeEventListener[] listeners;
52 * Used by Pico when there is no QGChangeEventListener instance in container.
54 public QGChangeEventListenersImpl() {
55 this.listeners = new QGChangeEventListener[0];
58 public QGChangeEventListenersImpl(QGChangeEventListener[] listeners) {
59 this.listeners = listeners;
63 public void broadcastOnIssueChange(List<DefaultIssue> issues, Collection<QGChangeEvent> changeEvents) {
64 if (listeners.length == 0 || issues.isEmpty() || changeEvents.isEmpty()) {
69 Multimap<String, QGChangeEvent> eventsByComponentUuid = changeEvents.stream()
70 .collect(MoreCollectors.index(t -> t.getProject().uuid()));
71 Multimap<String, DefaultIssue> issueByComponentUuid = issues.stream()
72 .collect(MoreCollectors.index(DefaultIssue::projectUuid));
74 issueByComponentUuid.asMap()
75 .forEach((componentUuid, value) -> {
76 Collection<QGChangeEvent> qgChangeEvents = eventsByComponentUuid.get(componentUuid);
77 if (!qgChangeEvents.isEmpty()) {
78 Set<ChangedIssue> changedIssues = value.stream()
79 .map(ChangedIssueImpl::new)
82 .forEach(changeEvent -> Arrays.stream(listeners)
83 .forEach(listener -> broadcastTo(changedIssues, changeEvent, listener)));
87 LOG.warn(format("Broadcasting to listeners failed for %s events", changeEvents.size()), e);
91 private static void broadcastTo(Set<ChangedIssue> changedIssues, QGChangeEvent changeEvent, QGChangeEventListener listener) {
93 LOG.trace("calling onChange() on listener {} for events {}...", listener.getClass().getName(), changeEvent);
94 listener.onIssueChanges(changeEvent, changedIssues);
95 } catch (Exception e) {
96 LOG.warn(format("onChange() call failed on listener %s for events %s", listener.getClass().getName(), changeEvent), e);
100 static class ChangedIssueImpl implements ChangedIssue {
101 private final String key;
102 private final QGChangeEventListener.Status status;
103 private final RuleType type;
104 private final String severity;
106 ChangedIssueImpl(DefaultIssue issue) {
107 this.key = issue.key();
108 this.status = statusOf(issue);
109 this.type = issue.type();
110 this.severity = issue.severity();
113 static QGChangeEventListener.Status statusOf(DefaultIssue issue) {
114 switch (issue.status()) {
115 case Issue.STATUS_OPEN:
116 return QGChangeEventListener.Status.OPEN;
117 case Issue.STATUS_CONFIRMED:
118 return QGChangeEventListener.Status.CONFIRMED;
119 case Issue.STATUS_REOPENED:
120 return QGChangeEventListener.Status.REOPENED;
121 case Issue.STATUS_TO_REVIEW:
122 return QGChangeEventListener.Status.TO_REVIEW;
123 case Issue.STATUS_RESOLVED:
124 return statusOfResolved(issue);
126 throw new IllegalStateException("Unexpected status: " + issue.status());
130 private static QGChangeEventListener.Status statusOfResolved(DefaultIssue issue) {
131 String resolution = issue.resolution();
132 Objects.requireNonNull(resolution, "A resolved issue should have a resolution");
133 switch (resolution) {
134 case Issue.RESOLUTION_FALSE_POSITIVE:
135 return QGChangeEventListener.Status.RESOLVED_FP;
136 case Issue.RESOLUTION_WONT_FIX:
137 return QGChangeEventListener.Status.RESOLVED_WF;
138 case Issue.RESOLUTION_FIXED:
139 return QGChangeEventListener.Status.RESOLVED_FIXED;
141 throw new IllegalStateException("Unexpected resolution for a resolved issue: " + resolution);
146 public String getKey() {
151 public QGChangeEventListener.Status getStatus() {
156 public RuleType getType() {
161 public String getSeverity() {
166 public String toString() {
167 return "ChangedIssueImpl{" +
168 "key='" + key + '\'' +
169 ", status=" + status +
171 ", severity=" + severity +