Skip to content

Commit e76d7c4

Browse files
AniketDev7harshitha-cstk
authored andcommitted
chore: testing slack notification for sanity report
1 parent 8adf784 commit e76d7c4

1 file changed

Lines changed: 108 additions & 0 deletions

File tree

sanity-report-ts-sdk.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
const fs = require("fs");
2+
const { App } = require("@slack/bolt");
3+
const { JSDOM } = require("jsdom");
4+
const dotenv = require("dotenv");
5+
const path = require("path");
6+
7+
dotenv.config();
8+
9+
const user1 = process.env.USER1;
10+
const user2 = process.env.USER2;
11+
const user3 = process.env.USER3;
12+
const user4 = process.env.USER4;
13+
14+
const reportPath = path.join(__dirname, "reports/sanity.html");
15+
const data = fs.readFileSync(reportPath, "utf8");
16+
const dom = new JSDOM(data);
17+
18+
const textarea = dom.window.document.querySelector("#jest-html-reports-result-data");
19+
const testResults = JSON.parse(textarea.textContent.trim());
20+
21+
const startTime = testResults.startTime;
22+
const endTime = Math.max(...testResults.testResults.map(t => t.perfStats.end));
23+
const totalSeconds = (endTime - startTime) / 1000;
24+
const minutes = Math.floor(totalSeconds / 60);
25+
const seconds = (totalSeconds % 60).toFixed(2);
26+
const duration = `${minutes}m ${seconds}s`;
27+
28+
const summary = {
29+
totalSuites: testResults.numTotalTestSuites,
30+
passedSuites: testResults.numPassedTestSuites,
31+
failedSuites: testResults.numFailedTestSuites,
32+
totalTests: testResults.numTotalTests,
33+
passedTests: testResults.numPassedTests,
34+
failedTests: testResults.numFailedTests,
35+
skippedTests: testResults.numPendingTests + testResults.numTodoTests,
36+
pendingTests: testResults.numPendingTests,
37+
duration: duration,
38+
};
39+
40+
const resultMessage =
41+
summary.passedTests === summary.totalTests
42+
? `:white_check_mark: Success (${summary.passedTests} / ${summary.totalTests} Passed)`
43+
: `:x: Failure (${summary.passedTests} / ${summary.totalTests} Passed)`;
44+
45+
// Static path message — local or remote can be set up
46+
const reportPathMessage = `_(Report: \`reports/sanity.html\`)`;
47+
48+
let tagUsers = "";
49+
if (summary.failedTests > 0) {
50+
tagUsers = `<@${user1}> <@${user2}> <@${user3}> <@${user4}>`;
51+
}
52+
53+
const slackMessage = {
54+
text: `*Dev11 - SDK-Typescript-CDA Sanity*\n*Result:* ${resultMessage}\n*Duration:* ${summary.duration}\n*Failed Tests:* ${summary.failedTests + summary.skippedTests}\n${reportPathMessage}\n${tagUsers}`,
55+
};
56+
57+
const app = new App({
58+
token: process.env.SLACK_BOT_TOKEN,
59+
signingSecret: process.env.SLACK_SIGNING_SECRET,
60+
});
61+
62+
const sendSlackMessage = async () => {
63+
try {
64+
const result = await app.client.chat.postMessage({
65+
token: process.env.SLACK_BOT_TOKEN,
66+
channel: process.env.SLACK_CHANNEL2,
67+
text: slackMessage.text,
68+
});
69+
70+
if (summary.failedTests > 0) {
71+
await sendFailureDetails(result.ts);
72+
}
73+
} catch (error) {
74+
console.error("❌ Error sending Slack message:", error);
75+
}
76+
};
77+
78+
const sendFailureDetails = async (threadTs) => {
79+
const failedTestSuites = testResults.testResults.filter(
80+
(suite) => suite.numFailingTests > 0
81+
);
82+
83+
if (failedTestSuites.length > 0) {
84+
let failureDetails = "*Failed Test Modules:*\n";
85+
for (const suite of failedTestSuites) {
86+
let modulePath = suite.testFilePath;
87+
let formattedModuleName = path
88+
.relative(__dirname, modulePath)
89+
.replace(/^test\//, "")
90+
.replace(/\.ts$/, "")
91+
.replace(/\//g, " ");
92+
failureDetails += ` - ${formattedModuleName}: ${suite.numFailingTests} failed\n`;
93+
}
94+
95+
try {
96+
await app.client.chat.postMessage({
97+
token: process.env.SLACK_BOT_TOKEN,
98+
channel: process.env.SLACK_CHANNEL2,
99+
text: failureDetails,
100+
thread_ts: threadTs,
101+
});
102+
} catch (error) {
103+
console.error("❌ Error sending failure details:", error);
104+
}
105+
}
106+
};
107+
108+
sendSlackMessage();

0 commit comments

Comments
 (0)