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