From ec355a9212cf0682e1e578043f81706f772f9532 Mon Sep 17 00:00:00 2001 From: "harshitha.d" Date: Mon, 18 Nov 2024 14:55:38 +0530 Subject: [PATCH] updated with new format of slack notification --- sanity-report-dev11.js | 55 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 sanity-report-dev11.js diff --git a/sanity-report-dev11.js b/sanity-report-dev11.js new file mode 100644 index 00000000..d50bcb59 --- /dev/null +++ b/sanity-report-dev11.js @@ -0,0 +1,55 @@ +import dotenv from "dotenv"; +import fs from "fs"; + +dotenv.config(); + +const mochawesomeJsonOutput = fs.readFileSync( + "./mochawesome-report/mochawesome.json", + "utf-8" +); +const mochawesomeReport = JSON.parse(mochawesomeJsonOutput); + +const totalTests = mochawesomeReport.stats.tests; +const passedTests = mochawesomeReport.stats.passes; +const failedTests = mochawesomeReport.stats.failures; + +const resultMessage = + passedTests === totalTests + ? `:white_check_mark: Success (${passedTests} / ${totalTests} Passed)` + : `:x: Failure (${passedTests} / ${totalTests} Passed)`; + +const slackMessage = { + text: ` + *Dev11, CMA SDK Full Sanity* + Result: ${resultMessage} + Failed Tests: ${failedTests} + View Report: + `, +}; + +const slackWebhookUrl = process.env.SLACK_WEBHOOK_URL; + +const sendSlackMessage = async (message) => { + const payload = { + text: message, + }; + + try { + const response = await fetch(slackWebhookUrl, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(payload), + }); + + if (!response.ok) { + throw new Error(`Error sending message to Slack: ${response.statusText}`); + } + + console.log("Message sent to Slack successfully"); + } catch (error) { + console.error("Error:", error); + } +}; +sendSlackMessage(slackMessage.text);