Skip to content

dx 2317 slack fail msg #298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"author": "Contentstack",
"license": "MIT",
"dependencies": {
"axios": "^1.8.2",
"axios": "^1.8.3",
"form-data": "^4.0.2",
"lodash": "^4.17.21",
"qs": "^6.14.0"
Expand Down
116 changes: 65 additions & 51 deletions sanity-report-dev11.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,88 @@
const dotenv = require('dotenv')
const fs = require('fs')
import Slack from "@slack/bolt";
const { App } = Slack;
import dotenv from "dotenv";
import fs from "fs";

dotenv.config()
dotenv.config();

const user1 = process.env.USER1
const user2 = process.env.USER2
const user3 = process.env.USER3
const user4 = process.env.USER4
const user1 = process.env.USER1;
const user2 = process.env.USER2;
const user3 = process.env.USER3;
const user4 = process.env.USER4;

const mochawesomeJsonOutput = fs.readFileSync(
'./mochawesome-report/mochawesome.json',
'utf-8'
)
const mochawesomeReport = JSON.parse(mochawesomeJsonOutput)
"./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 totalTests = mochawesomeReport.stats.tests;
const passedTests = mochawesomeReport.stats.passes;
const failedTests = mochawesomeReport.stats.failures;

let durationInSeconds = Math.floor(mochawesomeReport.stats.duration / 1000)
const durationInMinutes = Math.floor(durationInSeconds / 60)
durationInSeconds %= 60
let durationInSeconds = Math.floor(mochawesomeReport.stats.duration / 1000);
const durationInMinutes = Math.floor(durationInSeconds / 60);
durationInSeconds %= 60;

const resultMessage =
passedTests === totalTests
? `:white_check_mark: Success (${passedTests} / ${totalTests} Passed)`
: `:x: Failure (${passedTests} / ${totalTests} Passed)`
: `:x: Failure (${passedTests} / ${totalTests} Passed)`;

const pipelineName = process.env.GO_PIPELINE_NAME
const pipelineCounter = process.env.GO_PIPELINE_COUNTER
const goCdServer = process.env.GOCD_SERVER
const pipelineName = process.env.GO_PIPELINE_NAME;
const pipelineCounter = process.env.GO_PIPELINE_COUNTER;
const goCdServer = process.env.GOCD_SERVER;

const reportUrl = `http://${goCdServer}/go/files/${pipelineName}/${pipelineCounter}/sanity/1/sanity/test-results/mochawesome-report/sanity-report.html`
const reportUrl = `http://${goCdServer}/go/files/${pipelineName}/${pipelineCounter}/sanity/1/sanity/test-results/mochawesome-report/sanity-report.html`;

let tagUsers = ``
if (failedTests > 0) {
tagUsers = `<@${user1}> <@${user2}> <@${user3}> <@${user4}>`
}
let tagUsers =
failedTests > 0 ? `<@${user1}> <@${user2}> <@${user3}> <@${user4}>` : "";

const slackMessage = {
text: `Dev11, SDK-CMA Sanity
*Result:* ${resultMessage}. ${durationInMinutes}m ${durationInSeconds}s
*Failed Tests:* ${failedTests}
<${reportUrl}|View Report>
${tagUsers}`
}
text: `Dev11, SDK-CMA Sanity\n*Result:* ${resultMessage}. ${durationInMinutes}m ${durationInSeconds}s\n*Failed Tests:* ${failedTests}\n<${reportUrl}|View Report>\n${tagUsers}`,
};

const slackWebhookUrl = process.env.SLACK_WEBHOOK_URL
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET,
});

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}`)
const result = await app.client.chat.postMessage({
token: process.env.SLACK_BOT_TOKEN,
channel: process.env.SLACK_CHANNEL2,
text: message,
});

if (failedTests > 0) {
await sendFailureDetails(result.ts);
}
} catch (error) {
console.error("Error sending Slack message:", error);
}
};

console.log('Message sent to Slack successfully')
const sendFailureDetails = async (threadTs) => {
const failedSuites = mochawesomeReport.results
.flatMap((result) => result.suites)
.filter((suite) => suite.failures.length > 0);

let failureDetails = "*Failed Test Modules:*\n";
for (const suite of failedSuites) {
failureDetails += `- *${suite.title}*: ${suite.failures.length} failed\n`;
}

try {
await app.client.chat.postMessage({
token: process.env.SLACK_BOT_TOKEN,
channel: process.env.SLACK_CHANNEL,
text: failureDetails,
thread_ts: threadTs,
});
} catch (error) {
console.error('Error:', error)
console.error("Error sending failure details:", error);
}
}
sendSlackMessage(slackMessage.text)
};

sendSlackMessage(slackMessage.text);
Loading