|
1 |
| -const dotenv = require('dotenv') |
2 |
| -const fs = require('fs') |
| 1 | +import Slack from "@slack/bolt"; |
| 2 | +const { App } = Slack; |
| 3 | +import dotenv from "dotenv"; |
| 4 | +import fs from "fs"; |
3 | 5 |
|
4 |
| -dotenv.config() |
| 6 | +dotenv.config(); |
5 | 7 |
|
6 |
| -const user1 = process.env.USER1 |
7 |
| -const user2 = process.env.USER2 |
8 |
| -const user3 = process.env.USER3 |
9 |
| -const user4 = process.env.USER4 |
| 8 | +const user1 = process.env.USER1; |
| 9 | +const user2 = process.env.USER2; |
| 10 | +const user3 = process.env.USER3; |
| 11 | +const user4 = process.env.USER4; |
10 | 12 |
|
11 | 13 | const mochawesomeJsonOutput = fs.readFileSync(
|
12 |
| - './mochawesome-report/mochawesome.json', |
13 |
| - 'utf-8' |
14 |
| -) |
15 |
| -const mochawesomeReport = JSON.parse(mochawesomeJsonOutput) |
| 14 | + "./mochawesome-report/mochawesome.json", |
| 15 | + "utf-8" |
| 16 | +); |
| 17 | +const mochawesomeReport = JSON.parse(mochawesomeJsonOutput); |
16 | 18 |
|
17 |
| -const totalTests = mochawesomeReport.stats.tests |
18 |
| -const passedTests = mochawesomeReport.stats.passes |
19 |
| -const failedTests = mochawesomeReport.stats.failures |
| 19 | +const totalTests = mochawesomeReport.stats.tests; |
| 20 | +const passedTests = mochawesomeReport.stats.passes; |
| 21 | +const failedTests = mochawesomeReport.stats.failures; |
20 | 22 |
|
21 |
| -let durationInSeconds = Math.floor(mochawesomeReport.stats.duration / 1000) |
22 |
| -const durationInMinutes = Math.floor(durationInSeconds / 60) |
23 |
| -durationInSeconds %= 60 |
| 23 | +let durationInSeconds = Math.floor(mochawesomeReport.stats.duration / 1000); |
| 24 | +const durationInMinutes = Math.floor(durationInSeconds / 60); |
| 25 | +durationInSeconds %= 60; |
24 | 26 |
|
25 | 27 | const resultMessage =
|
26 | 28 | passedTests === totalTests
|
27 | 29 | ? `:white_check_mark: Success (${passedTests} / ${totalTests} Passed)`
|
28 |
| - : `:x: Failure (${passedTests} / ${totalTests} Passed)` |
| 30 | + : `:x: Failure (${passedTests} / ${totalTests} Passed)`; |
29 | 31 |
|
30 |
| -const pipelineName = process.env.GO_PIPELINE_NAME |
31 |
| -const pipelineCounter = process.env.GO_PIPELINE_COUNTER |
32 |
| -const goCdServer = process.env.GOCD_SERVER |
| 32 | +const pipelineName = process.env.GO_PIPELINE_NAME; |
| 33 | +const pipelineCounter = process.env.GO_PIPELINE_COUNTER; |
| 34 | +const goCdServer = process.env.GOCD_SERVER; |
33 | 35 |
|
34 |
| -const reportUrl = `http://${goCdServer}/go/files/${pipelineName}/${pipelineCounter}/sanity/1/sanity/test-results/mochawesome-report/sanity-report.html` |
| 36 | +const reportUrl = `http://${goCdServer}/go/files/${pipelineName}/${pipelineCounter}/sanity/1/sanity/test-results/mochawesome-report/sanity-report.html`; |
35 | 37 |
|
36 |
| -let tagUsers = `` |
37 |
| -if (failedTests > 0) { |
38 |
| - tagUsers = `<@${user1}> <@${user2}> <@${user3}> <@${user4}>` |
39 |
| -} |
| 38 | +let tagUsers = |
| 39 | + failedTests > 0 ? `<@${user1}> <@${user2}> <@${user3}> <@${user4}>` : ""; |
40 | 40 |
|
41 | 41 | const slackMessage = {
|
42 |
| - text: `Dev11, SDK-CMA Sanity |
43 |
| -*Result:* ${resultMessage}. ${durationInMinutes}m ${durationInSeconds}s |
44 |
| -*Failed Tests:* ${failedTests} |
45 |
| -<${reportUrl}|View Report> |
46 |
| -${tagUsers}` |
47 |
| -} |
| 42 | + text: `Dev11, SDK-CMA Sanity\n*Result:* ${resultMessage}. ${durationInMinutes}m ${durationInSeconds}s\n*Failed Tests:* ${failedTests}\n<${reportUrl}|View Report>\n${tagUsers}`, |
| 43 | +}; |
48 | 44 |
|
49 |
| -const slackWebhookUrl = process.env.SLACK_WEBHOOK_URL |
| 45 | +const app = new App({ |
| 46 | + token: process.env.SLACK_BOT_TOKEN, |
| 47 | + signingSecret: process.env.SLACK_SIGNING_SECRET, |
| 48 | +}); |
50 | 49 |
|
51 | 50 | const sendSlackMessage = async (message) => {
|
52 |
| - const payload = { |
53 |
| - text: message |
54 |
| - } |
55 |
| - |
56 | 51 | try {
|
57 |
| - const response = await fetch(slackWebhookUrl, { |
58 |
| - method: 'POST', |
59 |
| - headers: { |
60 |
| - 'Content-Type': 'application/json' |
61 |
| - }, |
62 |
| - body: JSON.stringify(payload) |
63 |
| - }) |
64 |
| - |
65 |
| - if (!response.ok) { |
66 |
| - throw new Error(`Error sending message to Slack: ${response.statusText}`) |
| 52 | + const result = await app.client.chat.postMessage({ |
| 53 | + token: process.env.SLACK_BOT_TOKEN, |
| 54 | + channel: process.env.SLACK_CHANNEL, |
| 55 | + text: message, |
| 56 | + }); |
| 57 | + |
| 58 | + if (failedTests > 0) { |
| 59 | + await sendFailureDetails(result.ts); |
67 | 60 | }
|
| 61 | + } catch (error) { |
| 62 | + console.error("Error sending Slack message:", error); |
| 63 | + } |
| 64 | +}; |
68 | 65 |
|
69 |
| - console.log('Message sent to Slack successfully') |
| 66 | +const sendFailureDetails = async (threadTs) => { |
| 67 | + const failedSuites = mochawesomeReport.results |
| 68 | + .flatMap((result) => result.suites) |
| 69 | + .filter((suite) => suite.failures.length > 0); |
| 70 | + |
| 71 | + let failureDetails = "*Failed Test Modules:*\n"; |
| 72 | + for (const suite of failedSuites) { |
| 73 | + failureDetails += `- *${suite.title}*: ${suite.failures.length} failed\n`; |
| 74 | + } |
| 75 | + |
| 76 | + try { |
| 77 | + await app.client.chat.postMessage({ |
| 78 | + token: process.env.SLACK_BOT_TOKEN, |
| 79 | + channel: process.env.SLACK_CHANNEL, |
| 80 | + text: failureDetails, |
| 81 | + thread_ts: threadTs, |
| 82 | + }); |
70 | 83 | } catch (error) {
|
71 |
| - console.error('Error:', error) |
| 84 | + console.error("Error sending failure details:", error); |
72 | 85 | }
|
73 |
| -} |
74 |
| -sendSlackMessage(slackMessage.text) |
| 86 | +}; |
| 87 | + |
| 88 | +sendSlackMessage(slackMessage.text); |
0 commit comments