Skip to content

Commit 750e4ba

Browse files
exmikefuneraLvalfirst
authored
Add support of Rocket.Chat messenger (#229)
Co-authored-by: funeraL <[email protected]> Co-authored-by: Valery Yatsynovich <[email protected]>
1 parent 7bf130e commit 750e4ba

File tree

7 files changed

+125
-1
lines changed

7 files changed

+125
-1
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Allure notifications
2-
**Allure notifications** - это библиотека, позволяющая выполнять автоматическое оповещение о результатах прохождения автотестов, которое направляется в нужный вам мессенджер (Telegram, Slack, Skype, Email, Mattermost, Discord, Loop).
2+
**Allure notifications** - это библиотека, позволяющая выполнять автоматическое оповещение о результатах прохождения автотестов, которое направляется в нужный вам мессенджер (Telegram, Slack, Skype, Email, Mattermost, Discord, Loop, Rocket.Chat).
33

44
Languages: 🇬🇧 🇫🇷 🇷🇺 🇺🇦 🇧🇾 🇨🇳
55

@@ -92,6 +92,12 @@ Languages: 🇬🇧 🇫🇷 🇷🇺 🇺🇦 🇧🇾 🇨🇳
9292
"token": "",
9393
"chat": ""
9494
},
95+
"rocketChat" : {
96+
"url": "",
97+
"auth_token": "",
98+
"user_id": "",
99+
"channel": ""
100+
},
95101
"skype": {
96102
"appId": "",
97103
"appSecret": "",
@@ -246,4 +252,19 @@ java "-DconfigFile=notifications/config.json" -jar ../allure-notifications-4.2.1
246252
<li>Click "Save".</li>
247253
<li>Copy URL of webhook.</li>
248254
</ul>
255+
</details>
256+
+ <details>
257+
<summary>Rocket.Chat config</summary>
258+
To enable Rocket.Chat notifications it's required to provide 4 configuration parameters:
259+
<code>url</code>, <code>auth_token</code>,<code>user_id</code>,<code>channel</code>
260+
<ul>
261+
<li>
262+
<ol>
263+
<li>First of all you need to generate auth_token from user setting.</li>
264+
<li>After generation you can get auth_token and user_id.</li>
265+
<li>You can get the channel parameter using previously generated tokens and following the documentation.</li>
266+
+ (<a href="https://developer.rocket.chat/reference/api/rest-api/endpoints/rooms/channels-endpoints/info" target="_blank">Channels info docs</a>)
267+
</ol>
268+
</li>
269+
</ul>
249270
</details>

src/main/java/guru/qa/allure/notifications/clients/ClientFactory.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package guru.qa.allure.notifications.clients;
22

3+
import guru.qa.allure.notifications.clients.rocket.RocketChatClient;
34
import java.util.ArrayList;
45
import java.util.List;
56

@@ -40,6 +41,9 @@ public static List<Notifier> from(Config config) {
4041
if (config.getLoop() != null) {
4142
notifiers.add(new LoopClient(messageData, config.getLoop()));
4243
}
44+
if (config.getRocketChat() != null) {
45+
notifiers.add(new RocketChatClient(messageData, config.getRocketChat()));
46+
}
4347
return notifiers;
4448
}
4549
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package guru.qa.allure.notifications.clients.rocket;
2+
3+
import guru.qa.allure.notifications.clients.Notifier;
4+
import guru.qa.allure.notifications.config.rocket.RocketChat;
5+
import guru.qa.allure.notifications.exceptions.MessagingException;
6+
import guru.qa.allure.notifications.template.RocketTemplate;
7+
import guru.qa.allure.notifications.template.data.MessageData;
8+
import java.io.ByteArrayInputStream;
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
import kong.unirest.ContentType;
12+
import kong.unirest.Unirest;
13+
14+
public class RocketChatClient implements Notifier {
15+
16+
private final RocketChat rocketChat;
17+
private final RocketTemplate template;
18+
19+
public RocketChatClient(MessageData messageData, RocketChat rocket) {
20+
this.rocketChat = rocket;
21+
this.template = new RocketTemplate(messageData);
22+
}
23+
24+
@Override
25+
public void sendText() throws MessagingException {
26+
Map<String, Object> body = new HashMap<>();
27+
body.put("channel", rocketChat.getChannel());
28+
body.put("text", template.create());
29+
Unirest.post(rocketChat.getUrl() + "/api/v1/chat.postMessage")
30+
.header("X-Auth-Token", rocketChat.getToken())
31+
.header("X-User-Id", rocketChat.getUserId())
32+
.header("Content-Type", ContentType.APPLICATION_JSON.getMimeType())
33+
.body(body)
34+
.asString()
35+
.getBody();
36+
}
37+
38+
@Override
39+
public void sendPhoto(byte[] chartImage) throws MessagingException {
40+
sendText();
41+
String url = String.format("%s/api/v1/rooms.upload/%s", rocketChat.getUrl(), rocketChat.getChannel());
42+
Unirest.post(url)
43+
.header("X-Auth-Token", rocketChat.getToken())
44+
.header("X-User-Id", rocketChat.getUserId())
45+
.field("file", new ByteArrayInputStream(chartImage), ContentType.IMAGE_PNG, "chart.png")
46+
.asString()
47+
.getBody();
48+
}
49+
}

src/main/java/guru/qa/allure/notifications/config/Config.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import guru.qa.allure.notifications.config.mail.Mail;
88
import guru.qa.allure.notifications.config.mattermost.Mattermost;
99
import guru.qa.allure.notifications.config.proxy.Proxy;
10+
import guru.qa.allure.notifications.config.rocket.RocketChat;
1011
import guru.qa.allure.notifications.config.skype.Skype;
1112
import guru.qa.allure.notifications.config.slack.Slack;
1213
import guru.qa.allure.notifications.config.telegram.Telegram;
@@ -35,6 +36,8 @@ public class Config {
3536
private Discord discord;
3637
@SerializedName("loop")
3738
private Loop loop;
39+
@SerializedName("rocketChat")
40+
private RocketChat rocketChat;
3841
@SerializedName("proxy")
3942
private Proxy proxy;
4043
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package guru.qa.allure.notifications.config.rocket;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Getter;
5+
6+
@Getter
7+
public class RocketChat {
8+
9+
@SerializedName("url")
10+
private String url;
11+
@SerializedName("auth_token")
12+
private String token;
13+
@SerializedName("user_id")
14+
private String userId;
15+
@SerializedName("channel")
16+
private String channel;
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package guru.qa.allure.notifications.template;
2+
3+
import guru.qa.allure.notifications.exceptions.MessageBuildException;
4+
import guru.qa.allure.notifications.template.data.MessageData;
5+
6+
public class RocketTemplate {
7+
8+
private final MessageData messageData;
9+
10+
public RocketTemplate(MessageData messageData) {
11+
this.messageData = messageData;
12+
}
13+
14+
public String create() throws MessageBuildException {
15+
return new MessageTemplate(messageData).of("rocket.ftl");
16+
}
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<#compress>
2+
**${results}:**
3+
**-${environment}:** ${env}
4+
**-${comment}:** ${comm}
5+
**-${duration}:** **${time}**
6+
**-${totalScenarios}:** ${total}
7+
<#if passed != 0 > **-${totalPassed}:** ${passed} **(${passedPercentage} %)**</#if>
8+
<#if failed != 0 > **-${totalFailed}:** ${failed} **(${failedPercentage} %)** </#if>
9+
<#if broken != 0 > **-${totalBroken}:** ${broken} </#if>
10+
<#if unknown != 0 > **-${totalUnknown}:** ${unknown} </#if>
11+
<#if skipped != 0 > **-${totalSkipped}:** ${skipped} </#if>
12+
<#if reportLink??>**${reportAvailableAtLink}:** ${reportLink}</#if>
13+
</#compress>

0 commit comments

Comments
 (0)