|
| 1 | +import dotenv from "dotenv"; |
| 2 | +import fs from "fs"; |
| 3 | + |
| 4 | +dotenv.config(); |
| 5 | + |
| 6 | +const mochawesomeJsonOutput = fs.readFileSync( |
| 7 | + "./mochawesome-report/mochawesome.json", |
| 8 | + "utf-8" |
| 9 | +); |
| 10 | +const mochawesomeReport = JSON.parse(mochawesomeJsonOutput); |
| 11 | + |
| 12 | +const totalTests = mochawesomeReport.stats.tests; |
| 13 | +const passedTests = mochawesomeReport.stats.passes; |
| 14 | +const failedTests = mochawesomeReport.stats.failures; |
| 15 | + |
| 16 | +const resultMessage = |
| 17 | + passedTests === totalTests |
| 18 | + ? `:white_check_mark: Success (${passedTests} / ${totalTests} Passed)` |
| 19 | + : `:x: Failure (${passedTests} / ${totalTests} Passed)`; |
| 20 | + |
| 21 | +const slackMessage = { |
| 22 | + text: ` |
| 23 | + *Dev11, CMA SDK Full Sanity* |
| 24 | + Result: ${resultMessage} |
| 25 | + Failed Tests: ${failedTests} |
| 26 | + View Report: <file://${process.cwd()}/mochawesome-report/sanity-report.html> |
| 27 | + `, |
| 28 | +}; |
| 29 | + |
| 30 | +const slackWebhookUrl = process.env.SLACK_WEBHOOK_URL; |
| 31 | + |
| 32 | +const sendSlackMessage = async (message) => { |
| 33 | + const payload = { |
| 34 | + text: message, |
| 35 | + }; |
| 36 | + |
| 37 | + try { |
| 38 | + const response = await fetch(slackWebhookUrl, { |
| 39 | + method: "POST", |
| 40 | + headers: { |
| 41 | + "Content-Type": "application/json", |
| 42 | + }, |
| 43 | + body: JSON.stringify(payload), |
| 44 | + }); |
| 45 | + |
| 46 | + if (!response.ok) { |
| 47 | + throw new Error(`Error sending message to Slack: ${response.statusText}`); |
| 48 | + } |
| 49 | + |
| 50 | + console.log("Message sent to Slack successfully"); |
| 51 | + } catch (error) { |
| 52 | + console.error("Error:", error); |
| 53 | + } |
| 54 | +}; |
| 55 | +sendSlackMessage(slackMessage.text); |
0 commit comments