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 // no notification on short living branch as there is no real Quality Gate on those
72 if (analysisMetadataHolder.isShortLivingBranch()) {
75 new DepthTraversalTypeAwareCrawler(
76 new TypeAwareVisitorAdapter(CrawlerDepthLimit.PROJECT, ComponentVisitor.Order.PRE_ORDER) {
78 public void visitProject(Component project) {
79 executeForProject(project);
81 }).visit(treeRootHolder.getRoot());
84 private void executeForProject(Component project) {
85 Metric metric = metricRepository.getByKey(CoreMetrics.ALERT_STATUS_KEY);
86 Optional<Measure> rawStatus = measureRepository.getRawMeasure(project, metric);
87 if (!rawStatus.isPresent() || !rawStatus.get().hasQualityGateStatus()) {
91 checkQualityGateStatusChange(project, metric, rawStatus.get().getQualityGateStatus());
94 private void checkQualityGateStatusChange(Component project, Metric metric, QualityGateStatus rawStatus) {
95 Optional<Measure> baseMeasure = measureRepository.getBaseMeasure(project, metric);
96 if (!baseMeasure.isPresent()) {
97 checkNewQualityGate(project, rawStatus);
101 if (!baseMeasure.get().hasQualityGateStatus()) {
102 LOGGER.warn(String.format("Previous Quality gate status for project %s is not a supported value. Can not compute Quality Gate event", project.getKey()));
103 checkNewQualityGate(project, rawStatus);
106 QualityGateStatus baseStatus = baseMeasure.get().getQualityGateStatus();
108 if (baseStatus.getStatus() != rawStatus.getStatus()) {
109 // The QualityGate status has changed
110 String label = String.format("%s (was %s)", rawStatus.getStatus().getColorName(), baseStatus.getStatus().getColorName());
111 createEvent(project, label, rawStatus.getText());
112 boolean isNewKo = rawStatus.getStatus() == Measure.Level.OK;
113 notifyUsers(project, label, rawStatus, isNewKo);
117 private void checkNewQualityGate(Component project, QualityGateStatus rawStatus) {
118 if (rawStatus.getStatus() != Measure.Level.OK) {
119 // There were no defined alerts before, so this one is a new one
120 createEvent(project, rawStatus.getStatus().getColorName(), rawStatus.getText());
121 notifyUsers(project, rawStatus.getStatus().getColorName(), rawStatus, true);
126 * @param label "Red (was Orange)"
127 * @param rawStatus OK, WARN or ERROR + optional text
129 private void notifyUsers(Component project, String label, QualityGateStatus rawStatus, boolean isNewAlert) {
130 Notification notification = new Notification("alerts")
131 .setDefaultMessage(String.format("Alert on %s: %s", project.getName(), label))
132 .setFieldValue("projectName", project.getName())
133 .setFieldValue("projectKey", project.getPublicKey())
134 .setFieldValue("projectVersion", project.getReportAttributes().getVersion())
135 .setFieldValue("alertName", label)
136 .setFieldValue("alertText", rawStatus.getText())
137 .setFieldValue("alertLevel", rawStatus.getStatus().toString())
138 .setFieldValue("isNewAlert", Boolean.toString(isNewAlert));
139 analysisMetadataHolder.getBranch().filter(b -> !b.isMain())
140 .ifPresent(branch -> notification.setFieldValue("branch", branch.getName()));
141 notificationService.deliver(notification);
144 private void createEvent(Component project, String name, @Nullable String description) {
145 eventRepository.add(project, Event.createAlert(name, null, description));
149 public String getDescription() {
150 return "Generate Quality gate events";