]> source.dussan.org Git - sonarqube.git/blob
4f8ae6a03bd272bb5399d0cfa4399ab7694385d3
[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.webhook;
21
22 import java.io.StringWriter;
23 import java.io.Writer;
24 import javax.annotation.Nullable;
25 import org.sonar.api.ce.ComputeEngineSide;
26 import org.sonar.api.ce.posttask.CeTask;
27 import org.sonar.api.ce.posttask.PostProjectAnalysisTask;
28 import org.sonar.api.ce.posttask.Project;
29 import org.sonar.api.ce.posttask.QualityGate;
30 import org.sonar.api.ce.posttask.ScannerContext;
31 import org.sonar.api.platform.Server;
32 import org.sonar.api.utils.text.JsonWriter;
33
34 import static org.sonar.core.config.WebhookProperties.ANALYSIS_PROPERTY_PREFIX;
35
36 @ComputeEngineSide
37 public class WebhookPayloadFactoryImpl implements WebhookPayloadFactory {
38
39   private final Server server;
40
41   public WebhookPayloadFactoryImpl(Server server) {
42     this.server = server;
43   }
44
45   @Override
46   public WebhookPayload create(PostProjectAnalysisTask.ProjectAnalysis analysis) {
47     Writer string = new StringWriter();
48     try (JsonWriter writer = JsonWriter.of(string)) {
49       writer.beginObject();
50       writeServer(writer);
51       writeTask(writer, analysis.getCeTask());
52       analysis.getAnalysisDate().ifPresent(date -> writer.propDateTime("analysedAt", date));
53       writeProject(analysis, writer, analysis.getProject());
54       writeQualityGate(writer, analysis.getQualityGate());
55       writeAnalysisProperties(writer, analysis.getScannerContext());
56       writer.endObject().close();
57       return new WebhookPayload(analysis.getProject().getKey(), string.toString());
58     }
59   }
60
61   private void writeServer(JsonWriter writer) {
62     writer.prop("serverUrl", server.getPublicRootUrl());
63   }
64
65   private static void writeAnalysisProperties(JsonWriter writer, ScannerContext scannerContext) {
66     writer
67       .name("properties")
68       .beginObject();
69     scannerContext.getProperties().entrySet()
70       .stream()
71       .filter(prop -> prop.getKey().startsWith(ANALYSIS_PROPERTY_PREFIX))
72       .forEach(prop -> writer.prop(prop.getKey(), prop.getValue()));
73     writer.endObject();
74   }
75
76   private static void writeTask(JsonWriter writer, CeTask ceTask) {
77     writer
78       .prop("taskId", ceTask.getId())
79       .prop("status", ceTask.getStatus().toString());
80   }
81
82   private static void writeProject(PostProjectAnalysisTask.ProjectAnalysis analysis, JsonWriter writer, Project project) {
83     writer
84       .name("project")
85       .beginObject()
86       .prop("key", project.getKey())
87       .prop("name", analysis.getProject().getName())
88       .endObject();
89   }
90
91   private static void writeQualityGate(JsonWriter writer, @Nullable QualityGate gate) {
92     if (gate != null) {
93       writer
94         .name("qualityGate")
95         .beginObject()
96         .prop("name", gate.getName())
97         .prop("status", gate.getStatus().toString())
98         .name("conditions")
99         .beginArray();
100       for (QualityGate.Condition condition : gate.getConditions()) {
101         writer
102           .beginObject()
103           .prop("metric", condition.getMetricKey())
104           .prop("operator", condition.getOperator().name());
105         if (condition.getStatus() != QualityGate.EvaluationStatus.NO_VALUE) {
106           writer.prop("value", condition.getValue());
107         }
108         writer
109           .prop("status", condition.getStatus().name())
110           .prop("onLeakPeriod", condition.isOnLeakPeriod())
111           .prop("errorThreshold", condition.getErrorThreshold())
112           .prop("warningThreshold", condition.getWarningThreshold())
113           .endObject();
114       }
115       writer
116         .endArray()
117         .endObject();
118     }
119   }
120 }