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 com.google.common.collect.Iterables;
23 import java.util.Arrays;
24 import java.util.List;
25 import java.util.Optional;
26 import org.sonar.api.ce.posttask.PostProjectAnalysisTask;
27 import org.sonar.api.config.Settings;
28 import org.sonar.api.utils.log.Logger;
29 import org.sonar.api.utils.log.Loggers;
30 import org.sonar.core.config.WebhookProperties;
31 import org.sonar.core.util.stream.MoreCollectors;
32 import org.sonar.server.computation.task.projectanalysis.component.SettingsRepository;
33 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
35 import static java.lang.String.format;
36 import static org.sonar.core.config.WebhookProperties.MAX_WEBHOOKS_PER_TYPE;
38 public class WebhookPostTask implements PostProjectAnalysisTask {
40 private static final Logger LOGGER = Loggers.get(WebhookPostTask.class);
42 private final TreeRootHolder rootHolder;
43 private final SettingsRepository settingsRepository;
44 private final WebhookPayloadFactory payloadFactory;
45 private final WebhookCaller caller;
46 private final WebhookDeliveryStorage deliveryStorage;
48 public WebhookPostTask(TreeRootHolder rootHolder, SettingsRepository settingsRepository, WebhookPayloadFactory payloadFactory,
49 WebhookCaller caller, WebhookDeliveryStorage deliveryStorage) {
50 this.rootHolder = rootHolder;
51 this.settingsRepository = settingsRepository;
52 this.payloadFactory = payloadFactory;
54 this.deliveryStorage = deliveryStorage;
58 public void finished(ProjectAnalysis analysis) {
59 Settings settings = settingsRepository.getSettings(rootHolder.getRoot());
61 Iterable<String> webhookProps = Iterables.concat(
62 getWebhookProperties(settings, WebhookProperties.GLOBAL_KEY),
63 getWebhookProperties(settings, WebhookProperties.PROJECT_KEY));
64 if (!Iterables.isEmpty(webhookProps)) {
65 process(settings, analysis, webhookProps);
66 deliveryStorage.purge(analysis.getProject().getUuid());
70 private static List<String> getWebhookProperties(Settings settings, String propertyKey) {
71 String[] webhookIds = settings.getStringArray(propertyKey);
72 return Arrays.stream(webhookIds)
73 .map(webhookId -> format("%s.%s", propertyKey, webhookId))
74 .limit(MAX_WEBHOOKS_PER_TYPE)
75 .collect(MoreCollectors.toList(webhookIds.length));
78 private void process(Settings settings, ProjectAnalysis analysis, Iterable<String> webhookProperties) {
79 WebhookPayload payload = payloadFactory.create(analysis);
80 for (String webhookProp : webhookProperties) {
81 String name = settings.getString(format("%s.%s", webhookProp, WebhookProperties.NAME_FIELD));
82 String url = settings.getString(format("%s.%s", webhookProp, WebhookProperties.URL_FIELD));
83 // as webhooks are defined as property sets, we can't ensure validity of fields on creation.
84 if (name != null && url != null) {
85 Webhook webhook = new Webhook(analysis.getProject().getUuid(), analysis.getCeTask().getId(), name, url);
86 WebhookDelivery delivery = caller.call(webhook, payload);
88 deliveryStorage.persist(delivery);
93 private static void log(WebhookDelivery delivery) {
94 Optional<String> error = delivery.getErrorMessage();
95 if (error.isPresent()) {
96 LOGGER.debug("Failed to send webhook '{}' | url={} | message={}",
97 delivery.getWebhook().getName(), delivery.getWebhook().getUrl(), error.get());
99 LOGGER.debug("Sent webhook '{}' | url={} | time={}ms | status={}",
100 delivery.getWebhook().getName(), delivery.getWebhook().getUrl(), delivery.getDurationInMs().orElse(-1), delivery.getHttpStatus().orElse(-1));