From 7c21e31c38a08a81f06f961dbf4fe692806912ca Mon Sep 17 00:00:00 2001 From: Marco Reni Date: Thu, 6 Dec 2018 14:52:49 +0100 Subject: [PATCH] Test /2 --- pom.xml | 5 -- .../slack/RepositoryComponentWebhook.groovy | 59 +++++++++++++------ 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/pom.xml b/pom.xml index ae8e7ca..9e90693 100644 --- a/pom.xml +++ b/pom.xml @@ -59,11 +59,6 @@ com.google.guava guava - - in.ashwanthkumar - slack-java-webhook - 0.0.9 - diff --git a/src/main/java/it/marcoreni/nexus3/repository/webhooks/slack/RepositoryComponentWebhook.groovy b/src/main/java/it/marcoreni/nexus3/repository/webhooks/slack/RepositoryComponentWebhook.groovy index 777118b..9477a51 100644 --- a/src/main/java/it/marcoreni/nexus3/repository/webhooks/slack/RepositoryComponentWebhook.groovy +++ b/src/main/java/it/marcoreni/nexus3/repository/webhooks/slack/RepositoryComponentWebhook.groovy @@ -1,9 +1,14 @@ package it.marcoreni.nexus3.repository.webhooks.slack -import in.ashwanthkumar.slack.webhook.Slack -import in.ashwanthkumar.slack.webhook.SlackMessage +import org.apache.http.HttpResponse +import org.apache.http.client.methods.HttpPost +import org.apache.http.entity.StringEntity +import org.apache.http.impl.client.CloseableHttpClient +import org.apache.http.impl.client.HttpClientBuilder +import org.sonatype.nexus.webhooks.WebhookConfiguration import org.sonatype.nexus.webhooks.WebhookSubscription +import javax.annotation.PostConstruct import javax.inject.Inject import javax.inject.Named import javax.inject.Singleton @@ -94,7 +99,7 @@ class RepositoryComponentWebhook ) subscriptions.each { - def configuration = it.configuration as RepositoryWebhook.Configuration + def configuration = it.configuration as Configuration if (configuration.repository == event.repositoryName) { // TODO: discriminate on content-selector queue(it, payload) @@ -103,23 +108,38 @@ class RepositoryComponentWebhook } } + private CloseableHttpClient httpClient + + @PostConstruct + void init() { + httpClient = HttpClientBuilder.create().build() + } + protected void queue(WebhookSubscription subscription, RepositoryComponentWebhookPayload body) { - this.log.debug("Queuing request for {} -> {}", subscription, body); - - new Slack(configuration.url) - .push( - new SlackMessage("Package ") - .bold(body.component.group + "/" + body.component.name) - .text(": new version ") - .bold(body.component.version) - .text(" published!")) - -// request.setWebhook(this); -// request.setPayload(body); -// WebhookConfiguration configuration = subscription.getConfiguration(); -// request.setUrl(configuration.getUrl()); -// request.setSecret(configuration.getSecret()); -// this.eventManager.post(new WebhookRequestSendEvent(request)); + this.log.debug("Queuing request for {} -> {}", subscription, body) + + String message = "{\"text\": \"Package \"}" + + WebhookConfiguration configuration = subscription.getConfiguration(); + String url = configuration.url + HttpPost request = new HttpPost(url) + + try { + request.setEntity(new StringEntity(message)) + + HttpResponse response = httpClient.execute(request) + + if (response.getStatusLine().getStatusCode() >= 400 + && response.getStatusLine().getStatusCode() < 600) { + // either a 4xx or 5xx response from the server, not good + this.log.warn("Got a bad HTTP response '" + response.getStatusLine() + "' for " + url) + } else { + this.log.debug("Response from " + url + " is : " + response.getStatusLine()) + } + + } finally { + request.releaseConnection(); + } } static class RepositoryComponentWebhookPayload @@ -144,4 +164,5 @@ class RepositoryComponentWebhook String version } } + } \ No newline at end of file