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