|
| 1 | +package com.contentstack.cms; |
| 2 | +import com.slack.api.Slack; |
| 3 | +import com.slack.api.methods.SlackApiException; |
| 4 | +import com.slack.api.methods.response.chat.ChatPostMessageResponse; |
| 5 | +import io.github.cdimascio.dotenv.Dotenv; |
| 6 | +import org.jsoup.Jsoup; |
| 7 | +import org.jsoup.nodes.Document; |
| 8 | +import org.jsoup.nodes.Element; |
| 9 | +import java.io.File; |
| 10 | +import java.io.IOException; |
| 11 | +import java.net.URI; |
| 12 | +import java.net.http.HttpClient; |
| 13 | +import java.net.http.HttpRequest; |
| 14 | +import java.net.http.HttpResponse; |
| 15 | +import java.net.http.HttpRequest.BodyPublishers; |
| 16 | +import java.nio.file.Files; |
| 17 | +import java.nio.file.Path; |
| 18 | +import java.nio.file.Paths; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +public class SanityReport { |
| 23 | + public static void main(String[] args) throws SlackApiException { |
| 24 | + try { |
| 25 | + Dotenv dotenv = Dotenv.load(); |
| 26 | + String slackToken = dotenv.get("SLACK_BOT_TOKEN"); |
| 27 | + String slackChannel = dotenv.get("SLACK_CHANNEL"); |
| 28 | + File input = new File("./target/site/surefire-report.html"); |
| 29 | + Document doc = Jsoup.parse(input, "UTF-8"); |
| 30 | + Element summaryTable = doc.select("table.bodyTable").first(); |
| 31 | + Element summaryRow = summaryTable.select("tr.b").first(); |
| 32 | + |
| 33 | + String totalCount = summaryRow.select("td").get(0).text().trim(); |
| 34 | + String totalErrors = summaryRow.select("td").get(1).text().trim(); |
| 35 | + String totalFailures = summaryRow.select("td").get(2).text().trim(); |
| 36 | + String totalSkipped = summaryRow.select("td").get(3).text().trim(); |
| 37 | + String totalTime = summaryRow.select("td").get(5).text().trim(); |
| 38 | + |
| 39 | + int durationInMinutes = 0; |
| 40 | + int durationInSeconds = 0; |
| 41 | + if (!totalTime.isEmpty()) { |
| 42 | + float timeInSeconds = Float.parseFloat(totalTime); |
| 43 | + durationInMinutes = (int) timeInSeconds / 60; |
| 44 | + durationInSeconds = (int) timeInSeconds % 60; |
| 45 | + } |
| 46 | + String slackMessage = String.format( |
| 47 | + "*Test Summary of Java Management SDK*\n" + |
| 48 | + "• Total Test Suite: *1*\n" + |
| 49 | + "• Total Tests: *%s*\n" + |
| 50 | + "• Total Pass: *%d*\n" + |
| 51 | + "• Total Fail: *%s*\n" + |
| 52 | + "• Total Skip: *%s*\n" + |
| 53 | + "• Total Pending: *%s*\n" + |
| 54 | + "• Total Duration: *%dm %ds*", |
| 55 | + totalCount, Integer.parseInt(totalCount) - (Integer.parseInt(totalErrors) + Integer.parseInt(totalFailures)), totalFailures, totalSkipped, totalErrors, durationInMinutes, durationInSeconds |
| 56 | + ); |
| 57 | + publishMessage(slackToken, slackChannel, slackMessage, input); |
| 58 | + |
| 59 | + } catch (IOException e) { |
| 60 | + System.out.println(e); |
| 61 | + } catch (NumberFormatException e) { |
| 62 | + System.out.println(e); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private static void publishMessage(String token, String channel, String text, File report) { |
| 67 | + try { |
| 68 | + Slack slack = Slack.getInstance(); |
| 69 | + String filePath = "./target/site/surefire-report.html"; |
| 70 | + String filename = "surefire-report.html"; |
| 71 | + String filetype = "text"; |
| 72 | + String initialComment = "Here is the report generated."; |
| 73 | + String title = "Reports File"; |
| 74 | + // Post the message to the Slack channel |
| 75 | + ChatPostMessageResponse messageResponse = slack.methods(token).chatPostMessage(req -> req |
| 76 | + .channel(channel) |
| 77 | + .text(text) |
| 78 | + ); |
| 79 | + // Check if posting message was successful |
| 80 | + if (!messageResponse.isOk()) { |
| 81 | + System.out.println("Message has not been posted"); |
| 82 | + } |
| 83 | + try { |
| 84 | + uploadFileToSlack(token, channel, filePath, filename, filetype, initialComment, title); |
| 85 | + } catch (IOException | InterruptedException e) { |
| 86 | + e.printStackTrace(); |
| 87 | + } |
| 88 | + } catch (IOException | SlackApiException e) { |
| 89 | + System.out.println(e); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public static void uploadFileToSlack(String token, String channelName, String filePath, String filename, String filetype, String initialComment, String title) throws IOException, InterruptedException { |
| 94 | + Path path = Paths.get(filePath); |
| 95 | + String boundary = "----WebKitFormBoundary7MA4YWxkTrZu0gW"; |
| 96 | + Map<String, String> params = new HashMap<>(); |
| 97 | + params.put("channels", channelName); |
| 98 | + if (filename != null) params.put("filename", filename); |
| 99 | + if (filetype != null) params.put("filetype", filetype); |
| 100 | + if (initialComment != null) params.put("initial_comment", initialComment); |
| 101 | + if (title != null) params.put("title", title); |
| 102 | + |
| 103 | + String body = buildMultipartBody(params, Files.readAllBytes(path), boundary); |
| 104 | + |
| 105 | + HttpRequest request = HttpRequest.newBuilder() |
| 106 | + .uri(URI.create("https://slack.com/api/files.upload")) |
| 107 | + .header("Authorization", "Bearer " + token) |
| 108 | + .header("Content-Type", "multipart/form-data; boundary=" + boundary) |
| 109 | + .POST(BodyPublishers.ofString(body)) |
| 110 | + .build(); |
| 111 | + |
| 112 | + HttpClient client = HttpClient.newHttpClient(); |
| 113 | + HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | + private static String buildMultipartBody(Map<String, String> params, byte[] fileContent, String boundary) { |
| 118 | + StringBuilder sb = new StringBuilder(); |
| 119 | + |
| 120 | + for (Map.Entry<String, String> entry : params.entrySet()) { |
| 121 | + sb.append("--").append(boundary).append("\r\n"); |
| 122 | + sb.append("Content-Disposition: form-data; name=\"").append(entry.getKey()).append("\"\r\n\r\n"); |
| 123 | + sb.append(entry.getValue()).append("\r\n"); |
| 124 | + } |
| 125 | + sb.append("--").append(boundary).append("\r\n"); |
| 126 | + sb.append("Content-Disposition: form-data; name=\"file\"; filename=\"").append(params.get("filename")).append("\"\r\n"); |
| 127 | + sb.append("Content-Type: application/octet-stream\r\n\r\n"); |
| 128 | + sb.append(new String(fileContent)).append("\r\n"); |
| 129 | + sb.append("--").append(boundary).append("--"); |
| 130 | + return sb.toString(); |
| 131 | + } |
| 132 | + |
| 133 | +} |
| 134 | + |
| 135 | + |
0 commit comments