]> source.dussan.org Git - sonarqube.git/blob
cea109218f169469204ca40b679a9d89411ee9a3
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 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.ce.task.projectanalysis.webhook;
21
22 import java.util.Map;
23 import java.util.Optional;
24 import java.util.Set;
25 import org.sonar.api.ce.posttask.PostProjectAnalysisTask;
26 import org.sonar.api.measures.Metric;
27 import org.sonar.core.util.stream.MoreCollectors;
28 import org.sonar.server.qualitygate.Condition;
29 import org.sonar.server.qualitygate.EvaluatedCondition;
30 import org.sonar.server.qualitygate.EvaluatedQualityGate;
31 import org.sonar.server.webhook.Analysis;
32 import org.sonar.server.webhook.Branch;
33 import org.sonar.server.webhook.CeTask;
34 import org.sonar.server.webhook.Project;
35 import org.sonar.server.webhook.WebHooks;
36 import org.sonar.server.webhook.WebhookPayloadFactory;
37
38 public class WebhookPostTask implements PostProjectAnalysisTask {
39
40   private final WebhookPayloadFactory payloadFactory;
41   private final WebHooks webHooks;
42
43   public WebhookPostTask(WebhookPayloadFactory payloadFactory, WebHooks webHooks) {
44     this.payloadFactory = payloadFactory;
45     this.webHooks = webHooks;
46   }
47
48   @Override
49   public void finished(ProjectAnalysis analysis) {
50     webHooks.sendProjectAnalysisUpdate(
51       new WebHooks.Analysis(
52         analysis.getProject().getUuid(),
53         analysis.getAnalysis().map(org.sonar.api.ce.posttask.Analysis::getAnalysisUuid).orElse(null),
54         analysis.getCeTask().getId()),
55       () -> payloadFactory.create(convert(analysis)));
56   }
57
58   private static org.sonar.server.webhook.ProjectAnalysis convert(ProjectAnalysis projectAnalysis) {
59     CeTask ceTask = new CeTask(projectAnalysis.getCeTask().getId(), CeTask.Status.valueOf(projectAnalysis.getCeTask().getStatus().name()));
60     Analysis analysis = projectAnalysis.getAnalysis().map(a -> new Analysis(a.getAnalysisUuid(), a.getDate().getTime())).orElse(null);
61     Branch branch = projectAnalysis.getBranch().map(b -> new Branch(b.isMain(), b.getName().orElse(null), Branch.Type.valueOf(b.getType().name()))).orElse(null);
62     EvaluatedQualityGate qualityGate = Optional.ofNullable(projectAnalysis.getQualityGate())
63       .map(qg -> {
64         EvaluatedQualityGate.Builder builder = EvaluatedQualityGate.newBuilder();
65         Set<Condition> conditions = qg.getConditions().stream()
66           .map(q -> {
67             Condition condition = new Condition(q.getMetricKey(), Condition.Operator.valueOf(q.getOperator().name()), q.getErrorThreshold());
68             builder.addCondition(condition,
69               EvaluatedCondition.EvaluationStatus.valueOf(q.getStatus().name()),
70               q.getStatus() == org.sonar.api.ce.posttask.QualityGate.EvaluationStatus.NO_VALUE ? null : q.getValue());
71             return condition;
72           })
73           .collect(MoreCollectors.toSet());
74         return builder.setQualityGate(new org.sonar.server.qualitygate.QualityGate(qg.getId(), qg.getName(), conditions))
75           .setStatus(Metric.Level.valueOf(qg.getStatus().name()))
76           .build();
77       })
78       .orElse(null);
79     Long date = projectAnalysis.getAnalysis().map(a -> a.getDate().getTime()).orElse(null);
80     Map<String, String> properties = projectAnalysis.getScannerContext().getProperties();
81
82     Project project = new Project(projectAnalysis.getProject().getUuid(), projectAnalysis.getProject().getKey(), projectAnalysis.getProject().getName());
83     return new org.sonar.server.webhook.ProjectAnalysis(project, ceTask, analysis, branch, qualityGate, date, properties);
84   }
85 }