Skip to content

Commit

Permalink
slack integration using slack bolt and testng to upload test report file
Browse files Browse the repository at this point in the history
  • Loading branch information
reeshika-h committed May 28, 2024
1 parent ff10ced commit e00553a
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 57 deletions.
34 changes: 14 additions & 20 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -200,24 +200,9 @@
<version>1.17.2</version>
</dependency>
<dependency>
<groupId>com.slack.api</groupId>
<artifactId>bolt</artifactId>
<version>1.38.0</version>
</dependency>
<dependency>
<groupId>com.slack.api</groupId>
<artifactId>bolt-servlet</artifactId>
<version>1.38.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>com.slack.api</groupId>
<artifactId>bolt-jetty</artifactId>
<version>1.38.0</version>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>com.slack.api</groupId>
Expand All @@ -229,6 +214,12 @@
<artifactId>slack-app-backend</artifactId>
<version>1.38.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.8.0</version>
<scope>test</scope>
</dependency>


</dependencies>
Expand All @@ -243,9 +234,12 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<includes>
<!-- <includes>
<include>**/*TestSuite.java</include>
</includes>
</includes> -->
<suiteXmlFiles>
<suiteXmlFile>${basedir}/testng.xml</suiteXmlFile>
</suiteXmlFiles>
<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
Expand Down
59 changes: 22 additions & 37 deletions src/test/java/com/contentstack/cms/SanityReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import com.slack.api.Slack;
import com.slack.api.methods.SlackApiException;
import com.slack.api.methods.response.chat.ChatPostMessageResponse;
import io.github.cdimascio.dotenv.Dotenv;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
Expand All @@ -20,12 +19,8 @@
import java.util.Map;

public class SanityReport {
public static void main(String[] args) throws SlackApiException {
try {
Dotenv dotenv = Dotenv.load();
String slackToken = dotenv.get("SLACK_BOT_TOKEN");
String slackChannel = dotenv.get("SLACK_CHANNEL");
File input = new File("./target/site/surefire-report.html");

public static String buildSlackMessage(File input) throws IOException {
Document doc = Jsoup.parse(input, "UTF-8");
Element summaryTable = doc.select("table.bodyTable").first();
Element summaryRow = summaryTable.select("tr.b").first();
Expand Down Expand Up @@ -54,66 +49,56 @@ public static void main(String[] args) throws SlackApiException {
"• Total Duration: *%dm %ds*",
totalCount, Integer.parseInt(totalCount) - (Integer.parseInt(totalErrors) + Integer.parseInt(totalFailures)), totalFailures, totalSkipped, totalErrors, durationInMinutes, durationInSeconds
);
publishMessage(slackToken, slackChannel, slackMessage, input);

} catch (IOException e) {
System.out.println(e);
} catch (NumberFormatException e) {
System.out.println(e);
}
return slackMessage;
}

private static void publishMessage(String token, String channel, String text, File report) {
public static void publishMessage(String token, String channel, String text, File report) throws IOException, SlackApiException, InterruptedException {
try {
Slack slack = Slack.getInstance();
String filePath = "./target/site/surefire-report.html";
String filename = "surefire-report.html";
String filetype = "text";
String initialComment = "Here is the report generated.";
String title = "Reports File";

// Post the message to the Slack channel
ChatPostMessageResponse messageResponse = slack.methods(token).chatPostMessage(req -> req
.channel(channel)
.text(text)
.channel(channel)
.text(text)
);
// Check if posting message was successful
if (!messageResponse.isOk()) {
System.out.println("Message has not been posted");
}
try {
uploadFileToSlack(token, channel, filePath, filename, filetype, initialComment, title);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
// Upload report file (optional)
if (report != null) {
uploadFileToSlack(token, channel, report.getAbsolutePath());
}
} catch (IOException | SlackApiException e) {
System.out.println(e);
}
}
public static void uploadFileToSlack(String token, String channelName, String filePath, String filename, String filetype, String initialComment, String title) throws IOException, InterruptedException {

private static void uploadFileToSlack(String token, String channelName, String filePath) throws IOException, InterruptedException {
Path path = Paths.get(filePath);
String boundary = "----WebKitFormBoundary7MA4YWxkTrZu0gW";
Map<String, String> params = new HashMap<>();
params.put("channels", channelName);
if (filename != null) params.put("filename", filename);
if (filetype != null) params.put("filetype", filetype);
if (initialComment != null) params.put("initial_comment", initialComment);
if (title != null) params.put("title", title);
params.put("filename", new File(filePath).getName());
params.put("filetype", "text");
params.put("initial_comment", "Here is the report generated.");
params.put("title", "Reports File");

String body = buildMultipartBody(params, Files.readAllBytes(path), boundary);

HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://slack.com/api/files.upload"))
.header("Authorization", "Bearer " + token)
.header("Content-Type", "multipart/form-data; boundary=" + boundary)
.POST(BodyPublishers.ofString(body))
.build();
.uri(URI.create("https://slack.com/api/files.upload"))
.header("Authorization", "Bearer " + token)
.header("Content-Type", "multipart/form-data; boundary=" + boundary)
.POST(BodyPublishers.ofString(body))
.build();

HttpClient client = HttpClient.newHttpClient();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

}


private static String buildMultipartBody(Map<String, String> params, byte[] fileContent, String boundary) {
StringBuilder sb = new StringBuilder();

Expand Down
31 changes: 31 additions & 0 deletions src/test/java/com/contentstack/cms/SlackReportingListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.contentstack.cms;
import com.contentstack.cms.SanityReport;
import io.github.cdimascio.dotenv.Dotenv;
import java.io.File;
import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

public class SlackReportingListener implements ITestListener {
@Override
public void onFinish(ITestContext context) {
Dotenv dotenv = Dotenv.load();
String slackToken = dotenv.get("SLACK_BOT_TOKEN");
String slackChannel = dotenv.get("SLACK_CHANNEL");
File input = new File("./target/site/surefire-report.html");
try {
SanityReport.publishMessage(
slackToken,
slackChannel,
SanityReport.buildSlackMessage(input),
input
);
} catch (Exception e) {
System.out.println("Error sending Slack message: " + e.getMessage());
}
}
}
15 changes: 15 additions & 0 deletions testng.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="APISanityTestSuite" verbose="5" parallel="classes"
thread-count="19" preserve-order="true">

<test name="Java Management SDK Sanity tests">
<classes>
<class name="com.contentstack.cms.stack.APISanityTestSuite"/>
</classes>
</test>

<listeners>
<listener class-name="com.contentstack.cms.SlackReportingListener" />
</listeners>
</suite>

0 comments on commit e00553a

Please sign in to comment.