3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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 java.util.Date;
25 import java.util.Optional;
26 import javax.annotation.Nullable;
27 import javax.annotation.concurrent.Immutable;
28 import org.sonar.api.ce.posttask.CeTask;
29 import org.sonar.api.ce.posttask.PostProjectAnalysisTask;
30 import org.sonar.api.ce.posttask.Project;
31 import org.sonar.api.ce.posttask.QualityGate;
32 import org.sonar.api.utils.text.JsonWriter;
34 import static java.util.Objects.requireNonNull;
37 public class WebhookPayload {
39 private final String projectKey;
40 private final String json;
42 public WebhookPayload(String projectKey, String json) {
43 this.projectKey = requireNonNull(projectKey);
44 this.json = requireNonNull(json);
47 public String getProjectKey() {
51 public String toJson() {
55 public static WebhookPayload from(PostProjectAnalysisTask.ProjectAnalysis analysis) {
56 Writer string = new StringWriter();
57 JsonWriter writer = JsonWriter.of(string);
59 writeTask(writer, analysis.getCeTask());
60 Optional<Date> analysisDate = analysis.getAnalysisDate();
61 if (analysisDate.isPresent()) {
62 writer.propDateTime("analysedAt", analysisDate.get());
64 writeProject(analysis, writer, analysis.getProject());
65 writeQualityGate(writer, analysis.getQualityGate());
66 writer.endObject().close();
67 return new WebhookPayload(analysis.getProject().getKey(), string.toString());
70 private static void writeTask(JsonWriter writer, CeTask ceTask) {
71 writer.prop("taskId", ceTask.getId());
72 writer.prop("status", ceTask.getStatus().toString());
75 private static void writeProject(PostProjectAnalysisTask.ProjectAnalysis analysis, JsonWriter writer, Project project) {
76 writer.name("project");
78 writer.prop("key", project.getKey());
79 writer.prop("name", analysis.getProject().getName());
83 private static void writeQualityGate(JsonWriter writer, @Nullable QualityGate gate) {
85 writer.name("qualityGate");
87 writer.prop("name", gate.getName());
88 writer.prop("status", gate.getStatus().toString());
89 writer.name("conditions").beginArray();
90 for (QualityGate.Condition condition : gate.getConditions()) {
92 writer.prop("metric", condition.getMetricKey());
93 writer.prop("operator", condition.getOperator().name());
94 if (condition.getStatus() != QualityGate.EvaluationStatus.NO_VALUE) {
95 writer.prop("value", condition.getValue());
97 writer.prop("status", condition.getStatus().name());
98 writer.prop("onLeakPeriod", condition.isOnLeakPeriod());
99 writer.prop("errorThreshold", condition.getErrorThreshold());
100 writer.prop("warningThreshold", condition.getWarningThreshold());