3 * Copyright (C) 2009-2019 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.ce.task.projectanalysis.webhook;
23 import java.util.Optional;
24 import org.sonar.api.ce.posttask.PostProjectAnalysisTask;
25 import org.sonar.ce.task.projectanalysis.api.posttask.QGToEvaluatedQG;
26 import org.sonar.server.qualitygate.EvaluatedQualityGate;
27 import org.sonar.server.webhook.Analysis;
28 import org.sonar.server.webhook.Branch;
29 import org.sonar.server.webhook.CeTask;
30 import org.sonar.server.webhook.Project;
31 import org.sonar.server.webhook.WebHooks;
32 import org.sonar.server.webhook.WebhookPayloadFactory;
34 public class WebhookPostTask implements PostProjectAnalysisTask {
35 private final WebhookPayloadFactory payloadFactory;
36 private final WebHooks webHooks;
38 public WebhookPostTask(WebhookPayloadFactory payloadFactory, WebHooks webHooks) {
39 this.payloadFactory = payloadFactory;
40 this.webHooks = webHooks;
44 public void finished(ProjectAnalysis analysis) {
45 webHooks.sendProjectAnalysisUpdate(
46 new WebHooks.Analysis(
47 analysis.getProject().getUuid(),
48 analysis.getAnalysis().map(org.sonar.api.ce.posttask.Analysis::getAnalysisUuid).orElse(null),
49 analysis.getCeTask().getId()),
50 () -> payloadFactory.create(convert(analysis)));
53 private static org.sonar.server.webhook.ProjectAnalysis convert(ProjectAnalysis projectAnalysis) {
54 CeTask ceTask = new CeTask(projectAnalysis.getCeTask().getId(), CeTask.Status.valueOf(projectAnalysis.getCeTask().getStatus().name()));
55 Analysis analysis = projectAnalysis.getAnalysis().map(a -> new Analysis(a.getAnalysisUuid(), a.getDate().getTime(), a.getRevision().orElse(null))).orElse(null);
56 Branch branch = projectAnalysis.getBranch().map(b -> new Branch(b.isMain(), b.getName().orElse(null), Branch.Type.valueOf(b.getType().name()))).orElse(null);
57 EvaluatedQualityGate qualityGate = Optional.ofNullable(projectAnalysis.getQualityGate())
58 .map(QGToEvaluatedQG.INSTANCE)
60 Long date = projectAnalysis.getAnalysis().map(a -> a.getDate().getTime()).orElse(null);
61 Map<String, String> properties = projectAnalysis.getScannerContext().getProperties();
63 Project project = new Project(projectAnalysis.getProject().getUuid(), projectAnalysis.getProject().getKey(), projectAnalysis.getProject().getName());
64 return new org.sonar.server.webhook.ProjectAnalysis(project, ceTask, analysis, branch, qualityGate, date, properties);