|
| 1 | +import pkg from '@slack/bolt'; |
| 2 | +import dotenv from 'dotenv'; |
| 3 | +import fs from 'fs'; |
| 4 | +import path from 'path'; |
| 5 | +import { JSDOM } from 'jsdom'; |
| 6 | + |
| 7 | +const { App } = pkg; |
| 8 | +dotenv.config(); |
| 9 | + |
| 10 | +const user1 = process.env.USER1; |
| 11 | +const user2 = process.env.USER2; |
| 12 | +const user3 = process.env.USER3; |
| 13 | +const user4 = process.env.USER4; |
| 14 | + |
| 15 | +const reportPath = path.resolve('reports/sanity.html'); |
| 16 | +const reportHtml = fs.readFileSync(reportPath, 'utf-8'); |
| 17 | +const dom = new JSDOM(reportHtml); |
| 18 | +const document = dom.window.document; |
| 19 | + |
| 20 | +const totalTests = document.querySelector('#test-summary .summary-total')?.textContent?.match(/\d+/)?.[0] || '0'; |
| 21 | +const passedTests = document.querySelector('#test-summary .summary-passed')?.textContent?.match(/\d+/)?.[0] || '0'; |
| 22 | +const failedTests = document.querySelector('#test-summary .summary-failed')?.textContent?.match(/\d+/)?.[0] || '0'; |
| 23 | + |
| 24 | +const durationElements = document.querySelectorAll('.suite-time'); |
| 25 | +let totalDurationInSeconds = 0; |
| 26 | + |
| 27 | +durationElements.forEach(element => { |
| 28 | + const timeText = element.textContent?.trim() || '0s'; |
| 29 | + const timeValue = parseFloat(timeText.replace(/[^\d.]/g, '')); |
| 30 | + if (timeText.includes('m')) { |
| 31 | + totalDurationInSeconds += timeValue * 60; |
| 32 | + } else { |
| 33 | + totalDurationInSeconds += timeValue; |
| 34 | + } |
| 35 | +}); |
| 36 | + |
| 37 | +const totalDurationMinutes = Math.floor(totalDurationInSeconds / 60); |
| 38 | +const totalDurationSeconds = Math.floor(totalDurationInSeconds % 60); |
| 39 | + |
| 40 | +const resultMessage = |
| 41 | + parseInt(passedTests) === parseInt(totalTests) |
| 42 | + ? `:white_check_mark: Success (${passedTests} / ${totalTests} Passed)` |
| 43 | + : `:x: Failure (${passedTests} / ${totalTests} Passed)`; |
| 44 | + |
| 45 | +const pipelineName = process.env.GO_PIPELINE_NAME; |
| 46 | +const pipelineCounter = process.env.GO_PIPELINE_COUNTER; |
| 47 | +const goCdServer = process.env.GOCD_SERVER; |
| 48 | + |
| 49 | +const reportUrl = `http://${goCdServer}/go/files/${pipelineName}/${pipelineCounter}/sanity/1/sanity/test-results/sanity.html`; |
| 50 | + |
| 51 | +const tagUsers = parseInt(failedTests) > 0 ? `<@${user1}> <@${user2}> <@${user3}> <@${user4}>` : ""; |
| 52 | + |
| 53 | +const slackMessage = { |
| 54 | + text: `Dev11, SDK-CMA Sanity\n*Result:* ${resultMessage}. ${totalDurationMinutes}m ${totalDurationSeconds}s\n*Failed Tests:* ${failedTests}\n<${reportUrl}|View Report>\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 (message) => { |
| 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: message, |
| 68 | + }); |
| 69 | + |
| 70 | + if (parseInt(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 | + let failureDetails = "*Failed Test Modules:*\n"; |
| 80 | + |
| 81 | + const failedTestElements = document.querySelectorAll('.test-result.failed'); |
| 82 | + const failedSuites = new Set(); |
| 83 | + |
| 84 | + failedTestElements.forEach(element => { |
| 85 | + const suiteName = element.querySelector('.test-suitename')?.textContent; |
| 86 | + if (suiteName) { |
| 87 | + failedSuites.add(suiteName); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + for (const suite of failedSuites) { |
| 92 | + failureDetails += `- *${suite}*: 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 | +sendSlackMessage(slackMessage.text); |
0 commit comments