From 162812b251244fe70bd4577b09e0a215614d2686 Mon Sep 17 00:00:00 2001 From: Martin Reinhardt Date: Tue, 27 Dec 2016 13:55:08 +0100 Subject: [PATCH] feat(new-rocket-api): Implement message sending (see #JENKINS-40595) --- .../plugins/rocketchatnotifier/model/Message.java | 3 +++ .../rocket/RocketChatClient.java | 9 ++++++++- .../rocket/RocketChatClientImpl.java | 15 ++++++++++++--- .../rocket/RocketChatRestApiV1.java | 6 +++++- .../rocket/RocketChatClientIT.java | 7 +++++++ 5 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/main/java/jenkins/plugins/rocketchatnotifier/model/Message.java b/src/main/java/jenkins/plugins/rocketchatnotifier/model/Message.java index e2db5ff9..6b60c2ff 100755 --- a/src/main/java/jenkins/plugins/rocketchatnotifier/model/Message.java +++ b/src/main/java/jenkins/plugins/rocketchatnotifier/model/Message.java @@ -4,6 +4,9 @@ public class Message { private String msg; + public Message() { + } + public Message(String messsage) { this.msg = messsage; } diff --git a/src/main/java/jenkins/plugins/rocketchatnotifier/rocket/RocketChatClient.java b/src/main/java/jenkins/plugins/rocketchatnotifier/rocket/RocketChatClient.java index 9ab3df26..40d2e6d6 100644 --- a/src/main/java/jenkins/plugins/rocketchatnotifier/rocket/RocketChatClient.java +++ b/src/main/java/jenkins/plugins/rocketchatnotifier/rocket/RocketChatClient.java @@ -38,6 +38,13 @@ public interface RocketChatClient { void send(Room room, String message) throws ValidatorException, IOException; - void send(String room, String message) throws ValidatorException, IOException; + /** + * + * @param channelName + * @param message + * @throws ValidatorException + * @throws IOException + */ + void send(String channelName, String message) throws ValidatorException, IOException; } diff --git a/src/main/java/jenkins/plugins/rocketchatnotifier/rocket/RocketChatClientImpl.java b/src/main/java/jenkins/plugins/rocketchatnotifier/rocket/RocketChatClientImpl.java index 24773254..17aacaec 100644 --- a/src/main/java/jenkins/plugins/rocketchatnotifier/rocket/RocketChatClientImpl.java +++ b/src/main/java/jenkins/plugins/rocketchatnotifier/rocket/RocketChatClientImpl.java @@ -6,6 +6,8 @@ import sun.security.validator.ValidatorException; import java.io.IOException; +import java.util.HashMap; +import java.util.Map; /** * Client for Rocket.Chat which relies on the REST API v1. @@ -69,11 +71,18 @@ public Room[] getChannels() throws IOException { @Override public void send(Room room, String message) throws ValidatorException, IOException { - + this.send(room.getName(), message); } @Override - public void send(String room, String message) throws ValidatorException, IOException { - + public void send(String channelName, String message) throws ValidatorException, IOException { + Map body = new HashMap(); + body.put("channel", "#" + channelName); + body.put("text", message); + Response res = this.callBuilder.buildCall(RocketChatRestApiV1.PostMessage, + null, body); + if (!res.isSuccessful()) { + throw new IOException("The send of the message was unsuccessful."); + } } } diff --git a/src/main/java/jenkins/plugins/rocketchatnotifier/rocket/RocketChatRestApiV1.java b/src/main/java/jenkins/plugins/rocketchatnotifier/rocket/RocketChatRestApiV1.java index 0047426d..e373e131 100644 --- a/src/main/java/jenkins/plugins/rocketchatnotifier/rocket/RocketChatRestApiV1.java +++ b/src/main/java/jenkins/plugins/rocketchatnotifier/rocket/RocketChatRestApiV1.java @@ -24,7 +24,11 @@ public enum RocketChatRestApiV1 { /** * Retrieves a list of all the channels in the server. */ - ChannelsInfo("channels.info", HttpMethods.GET, true); + ChannelsInfo("channels.info", HttpMethods.GET, true), + /** + * Retrieves a list of all the channels in the server. + */ + PostMessage("chat.postMessage", HttpMethods.POST, true); private String methodName; private HttpMethods httpMethod; diff --git a/src/test/java/jenkins/plugins/rocketchatnotifier/rocket/RocketChatClientIT.java b/src/test/java/jenkins/plugins/rocketchatnotifier/rocket/RocketChatClientIT.java index 7cdec0a0..d8b58b5d 100644 --- a/src/test/java/jenkins/plugins/rocketchatnotifier/rocket/RocketChatClientIT.java +++ b/src/test/java/jenkins/plugins/rocketchatnotifier/rocket/RocketChatClientIT.java @@ -24,4 +24,11 @@ public void shouldReadRooms() throws Exception { Room[] rooms = this.client.getChannels(); assertThat(rooms.length, is(1)); } + + @Test + public void shouldSendMessage() throws Exception { + this.client.send("general", "test"); + } + + }