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.webhook;
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;
35 import static org.sonar.core.config.WebhookProperties.ANALYSIS_PROPERTY_PREFIX;
38 public class WebhookPayloadFactoryImpl implements WebhookPayloadFactory {
40 private final Server server;
42 public WebhookPayloadFactoryImpl(Server server) {
47 public WebhookPayload create(PostProjectAnalysisTask.ProjectAnalysis analysis) {
48 Writer string = new StringWriter();
49 try (JsonWriter writer = JsonWriter.of(string)) {
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());
63 private void writeServer(JsonWriter writer) {
64 writer.prop("serverUrl", server.getPublicRootUrl());
67 private static void writeAnalysisProperties(JsonWriter writer, ScannerContext scannerContext) {
71 scannerContext.getProperties().entrySet()
73 .filter(prop -> prop.getKey().startsWith(ANALYSIS_PROPERTY_PREFIX))
74 .forEach(prop -> writer.prop(prop.getKey(), prop.getValue()));
78 private static void writeTask(JsonWriter writer, CeTask ceTask) {
80 .prop("taskId", ceTask.getId())
81 .prop("status", ceTask.getStatus().toString());
84 private static void writeProject(PostProjectAnalysisTask.ProjectAnalysis analysis, JsonWriter writer, Project project) {
88 .prop("key", project.getKey())
89 .prop("name", analysis.getProject().getName())
93 private static void writeBranch(JsonWriter writer, Branch branch) {
97 .prop("name", branch.getName().orElse(null))
98 .prop("type", branch.getType().name())
99 .prop("isMain", branch.isMain())
103 private static void writeQualityGate(JsonWriter writer, @Nullable QualityGate gate) {
108 .prop("name", gate.getName())
109 .prop("status", gate.getStatus().toString())
112 for (QualityGate.Condition condition : gate.getConditions()) {
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());
121 .prop("status", condition.getStatus().name())
122 .prop("onLeakPeriod", condition.isOnLeakPeriod())
123 .prop("errorThreshold", condition.getErrorThreshold())
124 .prop("warningThreshold", condition.getWarningThreshold())