From 0b1d1c21c69a41ebc53c5bfafcee308e4d0e8ca2 Mon Sep 17 00:00:00 2001 From: "harshitha.d" Date: Mon, 18 Nov 2024 14:54:38 +0530 Subject: [PATCH 1/5] updated with new format of slack notification --- sanity-report-dev11.js | 65 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 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..baacef14 --- /dev/null +++ b/sanity-report-dev11.js @@ -0,0 +1,65 @@ +const fs = require("fs"); +const dotenv = require("dotenv"); +const cheerio = require("cheerio"); + +dotenv.config(); + +const tapHtmlContent = fs.readFileSync("./tap-html.html", "utf8"); +const $ = cheerio.load(tapHtmlContent); + +const totalCount = $(".nav a:nth-child(2)") + .text() + .trim() + .replace("Total Count", ""); +const totalPass = $(".nav a:nth-child(3)") + .text() + .trim() + .replace("Total Pass", ""); +const totalFail = $(".nav a:nth-child(4)") + .text() + .trim() + .replace("Total Fail", ""); + +const passedTests = parseInt(totalPass, 10); +const totalTests = parseInt(totalCount, 10); + +const resultMessage = + passedTests === totalTests + ? `:white_check_mark: Success (${passedTests} / ${totalTests} Passed)` + : `:x: Failure (${passedTests} / ${totalTests} Passed)`; + +const slackMessage = { + text: ` + *Dev11, CDA SDK Full Sanity* + Result: ${resultMessage} + Failed Tests: *${totalFail}* + 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); From 56aab30408e8c11afc2ef52e4e9ee15f81363409 Mon Sep 17 00:00:00 2001 From: "harshitha.d" Date: Mon, 18 Nov 2024 19:36:55 +0530 Subject: [PATCH 2/5] updated to provide report link and indentation --- sanity-report-dev11.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/sanity-report-dev11.js b/sanity-report-dev11.js index baacef14..f46a046e 100644 --- a/sanity-report-dev11.js +++ b/sanity-report-dev11.js @@ -1,6 +1,7 @@ const fs = require("fs"); const dotenv = require("dotenv"); const cheerio = require("cheerio"); +const fetch = require("node-fetch"); dotenv.config(); @@ -28,13 +29,17 @@ const resultMessage = ? `:white_check_mark: Success (${passedTests} / ${totalTests} Passed)` : `:x: Failure (${passedTests} / ${totalTests} Passed)`; +const pipelineName = process.env.GOCD_PIPELINE_NAME; +const buildNumber = process.env.GOCD_PIPELINE_LABEL; +const goCdServer = process.env.GOCD_SERVER; + +const reportUrl = `http://${goCdServer}/go/files/${pipelineName}/${buildNumber}/sanity/1/sanity/test-results/tap-html.html`; + const slackMessage = { - text: ` - *Dev11, CDA SDK Full Sanity* - Result: ${resultMessage} - Failed Tests: *${totalFail}* - View Report: - `, + text: `Dev11, CDA SDK Full Sanity +*Result:* ${resultMessage} +*Failed Tests:* ${totalFail} +<${reportUrl}|View Report>`, }; const slackWebhookUrl = process.env.SLACK_WEBHOOK_URL; @@ -62,4 +67,5 @@ const sendSlackMessage = async (message) => { console.error("Error:", error); } }; + sendSlackMessage(slackMessage.text); From cdff8ad79ff3386dad6bcdb2be4987d0a7d17bf5 Mon Sep 17 00:00:00 2001 From: "harshitha.d" Date: Tue, 19 Nov 2024 10:53:24 +0530 Subject: [PATCH 3/5] change envs for slack notification --- sanity-report-dev11.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sanity-report-dev11.js b/sanity-report-dev11.js index f46a046e..a76006c6 100644 --- a/sanity-report-dev11.js +++ b/sanity-report-dev11.js @@ -29,8 +29,8 @@ const resultMessage = ? `:white_check_mark: Success (${passedTests} / ${totalTests} Passed)` : `:x: Failure (${passedTests} / ${totalTests} Passed)`; -const pipelineName = process.env.GOCD_PIPELINE_NAME; -const buildNumber = process.env.GOCD_PIPELINE_LABEL; +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}/${buildNumber}/sanity/1/sanity/test-results/tap-html.html`; From 932d7a76f86fb88c2b2aa484dbbcd00cf6ecd487 Mon Sep 17 00:00:00 2001 From: "harshitha.d" Date: Tue, 19 Nov 2024 11:18:56 +0530 Subject: [PATCH 4/5] changes const variable name --- sanity-report-dev11.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sanity-report-dev11.js b/sanity-report-dev11.js index a76006c6..4fa9460e 100644 --- a/sanity-report-dev11.js +++ b/sanity-report-dev11.js @@ -33,7 +33,7 @@ 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}/${buildNumber}/sanity/1/sanity/test-results/tap-html.html`; +const reportUrl = `http://${goCdServer}/go/files/${pipelineName}/${pipelineCounter}/sanity/1/sanity/test-results/tap-html.html`; const slackMessage = { text: `Dev11, CDA SDK Full Sanity From 1b6b1abd24be0f7a89709f4a750209b9e41037bd Mon Sep 17 00:00:00 2001 From: "harshitha.d" Date: Tue, 19 Nov 2024 17:20:16 +0530 Subject: [PATCH 5/5] update notification with time stamp --- sanity-report-dev11.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/sanity-report-dev11.js b/sanity-report-dev11.js index 4fa9460e..95fd2ae3 100644 --- a/sanity-report-dev11.js +++ b/sanity-report-dev11.js @@ -21,6 +21,16 @@ const totalFail = $(".nav a:nth-child(4)") .trim() .replace("Total Fail", ""); +const totalTime = $(".nav a:nth-child(1)") + .text() + .trim() + .replace("Total Time", ""); + +const milliseconds = parseInt(totalTime.replace(/\D/g, ''), 10); +const totalSeconds = Math.floor(milliseconds / 1000); +const durationInMinutes = Math.floor(totalSeconds / 60); +const durationInSeconds = totalSeconds % 60; + const passedTests = parseInt(totalPass, 10); const totalTests = parseInt(totalCount, 10); @@ -37,7 +47,7 @@ const reportUrl = `http://${goCdServer}/go/files/${pipelineName}/${pipelineCount const slackMessage = { text: `Dev11, CDA SDK Full Sanity -*Result:* ${resultMessage} +*Result:* ${resultMessage}. ${durationInMinutes}m ${durationInSeconds}s *Failed Tests:* ${totalFail} <${reportUrl}|View Report>`, };