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.step;
22 import com.google.common.base.Optional;
23 import javax.annotation.Nullable;
24 import org.sonar.api.measures.CoreMetrics;
25 import org.sonar.api.notifications.Notification;
26 import org.sonar.api.utils.log.Logger;
27 import org.sonar.api.utils.log.Loggers;
28 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
29 import org.sonar.server.computation.task.projectanalysis.component.Component;
30 import org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor;
31 import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit;
32 import org.sonar.server.computation.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
33 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
34 import org.sonar.server.computation.task.projectanalysis.component.TypeAwareVisitorAdapter;
35 import org.sonar.server.computation.task.projectanalysis.event.Event;
36 import org.sonar.server.computation.task.projectanalysis.event.EventRepository;
37 import org.sonar.server.computation.task.projectanalysis.measure.Measure;
38 import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepository;
39 import org.sonar.server.computation.task.projectanalysis.measure.QualityGateStatus;
40 import org.sonar.server.computation.task.projectanalysis.metric.Metric;
41 import org.sonar.server.computation.task.projectanalysis.metric.MetricRepository;
42 import org.sonar.server.computation.task.step.ComputationStep;
43 import org.sonar.server.notification.NotificationService;
46 * This step must be executed after computation of quality gate measure {@link QualityGateMeasuresStep}
48 public class QualityGateEventsStep implements ComputationStep {
49 private static final Logger LOGGER = Loggers.get(QualityGateEventsStep.class);
51 private final TreeRootHolder treeRootHolder;
52 private final MetricRepository metricRepository;
53 private final MeasureRepository measureRepository;
54 private final EventRepository eventRepository;
55 private final NotificationService notificationService;
56 private final AnalysisMetadataHolder analysisMetadataHolder;
58 public QualityGateEventsStep(TreeRootHolder treeRootHolder,
59 MetricRepository metricRepository, MeasureRepository measureRepository, EventRepository eventRepository,
60 NotificationService notificationService, AnalysisMetadataHolder analysisMetadataHolder) {
61 this.treeRootHolder = treeRootHolder;
62 this.metricRepository = metricRepository;
63 this.measureRepository = measureRepository;
64 this.eventRepository = eventRepository;
65 this.notificationService = notificationService;
66 this.analysisMetadataHolder = analysisMetadataHolder;
70 public void execute() {
71 new DepthTraversalTypeAwareCrawler(
72 new TypeAwareVisitorAdapter(CrawlerDepthLimit.PROJECT, ComponentVisitor.Order.PRE_ORDER) {
74 public void visitProject(Component project) {
75 executeForProject(project);
77 }).visit(treeRootHolder.getRoot());
80 private void executeForProject(Component project) {
81 Metric metric = metricRepository.getByKey(CoreMetrics.ALERT_STATUS_KEY);
82 Optional<Measure> rawStatus = measureRepository.getRawMeasure(project, metric);
83 if (!rawStatus.isPresent() || !rawStatus.get().hasQualityGateStatus()) {
87 checkQualityGateStatusChange(project, metric, rawStatus.get().getQualityGateStatus());
90 private void checkQualityGateStatusChange(Component project, Metric metric, QualityGateStatus rawStatus) {
91 Optional<Measure> baseMeasure = measureRepository.getBaseMeasure(project, metric);
92 if (!baseMeasure.isPresent()) {
93 checkNewQualityGate(project, rawStatus);
97 if (!baseMeasure.get().hasQualityGateStatus()) {
98 LOGGER.warn(String.format("Previous Quality gate status for project %s is not a supported value. Can not compute Quality Gate event", project.getKey()));
99 checkNewQualityGate(project, rawStatus);
102 QualityGateStatus baseStatus = baseMeasure.get().getQualityGateStatus();
104 if (baseStatus.getStatus() != rawStatus.getStatus()) {
105 // The QualityGate status has changed
106 String label = String.format("%s (was %s)", rawStatus.getStatus().getColorName(), baseStatus.getStatus().getColorName());
107 createEvent(project, label, rawStatus.getText());
108 boolean isNewKo = rawStatus.getStatus() == Measure.Level.OK;
109 notifyUsers(project, label, rawStatus, isNewKo);
113 private void checkNewQualityGate(Component project, QualityGateStatus rawStatus) {
114 if (rawStatus.getStatus() != Measure.Level.OK) {
115 // There were no defined alerts before, so this one is a new one
116 createEvent(project, rawStatus.getStatus().getColorName(), rawStatus.getText());
117 notifyUsers(project, rawStatus.getStatus().getColorName(), rawStatus, true);
122 * @param label "Red (was Orange)"
123 * @param rawStatus OK, WARN or ERROR + optional text
125 private void notifyUsers(Component project, String label, QualityGateStatus rawStatus, boolean isNewAlert) {
126 Notification notification = new Notification("alerts")
127 .setDefaultMessage(String.format("Alert on %s: %s", project.getName(), label))
128 .setFieldValue("projectName", project.getName())
129 .setFieldValue("projectKey", project.getPublicKey())
130 .setFieldValue("projectUuid", project.getUuid())
131 .setFieldValue("projectVersion", project.getReportAttributes().getVersion())
132 .setFieldValue("alertName", label)
133 .setFieldValue("alertText", rawStatus.getText())
134 .setFieldValue("alertLevel", rawStatus.getStatus().toString())
135 .setFieldValue("isNewAlert", Boolean.toString(isNewAlert));
136 analysisMetadataHolder.getBranch().filter(b -> !b.isMain())
137 .ifPresent(branch -> notification.setFieldValue("branch", branch.getName()));
138 notificationService.deliver(notification);
141 private void createEvent(Component project, String name, @Nullable String description) {
142 eventRepository.add(project, Event.createAlert(name, null, description));
146 public String getDescription() {
147 return "Generate Quality gate events";