1+ const fs = require ( "fs" ) ;
2+ const dotenv = require ( "dotenv" ) ;
3+ const cheerio = require ( "cheerio" ) ;
4+
5+ dotenv . config ( ) ;
6+
7+ const user1 = process . env . USER1 ;
8+ const user2 = process . env . USER2 ;
9+ const user3 = process . env . USER3 ;
10+ const user4 = process . env . USER4 ;
11+
12+ const tapHtmlContent = fs . readFileSync ( "./tap-html.html" , "utf8" ) ;
13+ const $ = cheerio . load ( tapHtmlContent ) ;
14+
15+ const totalCount = $ ( ".nav a:nth-child(2)" )
16+ . text ( )
17+ . trim ( )
18+ . replace ( "Total Count" , "" ) ;
19+ const totalPass = $ ( ".nav a:nth-child(3)" )
20+ . text ( )
21+ . trim ( )
22+ . replace ( "Total Pass" , "" ) ;
23+ const totalFail = $ ( ".nav a:nth-child(4)" )
24+ . text ( )
25+ . trim ( )
26+ . replace ( "Total Fail" , "" ) ;
27+
28+ const totalTime = $ ( ".nav a:nth-child(1)" )
29+ . text ( )
30+ . trim ( )
31+ . replace ( "Total Time" , "" ) ;
32+
33+ const milliseconds = parseInt ( totalTime . replace ( / \D / g, '' ) , 10 ) ;
34+ const totalSeconds = Math . floor ( milliseconds / 1000 ) ;
35+ const durationInMinutes = Math . floor ( totalSeconds / 60 ) ;
36+ const durationInSeconds = totalSeconds % 60 ;
37+
38+ const passedTests = parseInt ( totalPass , 10 ) ;
39+ const totalTests = parseInt ( totalCount , 10 ) ;
40+
41+ const resultMessage =
42+ passedTests === totalTests
43+ ? `:white_check_mark: Success (${ passedTests } / ${ totalTests } Passed)`
44+ : `:x: Failure (${ passedTests } / ${ totalTests } Passed)` ;
45+
46+ const pipelineName = process . env . GO_PIPELINE_NAME ;
47+ const pipelineCounter = process . env . GO_PIPELINE_COUNTER ;
48+ const goCdServer = process . env . GOCD_SERVER ;
49+
50+ const reportUrl = `http://${ goCdServer } /go/files/${ pipelineName } /${ pipelineCounter } /sanity/1/sanity/test-results/tap-html.html` ;
51+
52+ let tagUsers = `` ;
53+ if ( totalFail > 0 ) {
54+ tagUsers = `<@${ user1 } > <@${ user2 } > <@${ user3 } > <@${ user4 } >` ;
55+ }
56+
57+ const slackMessage = {
58+ text : `Dev11, CDA SDK Full Sanity
59+ *Result:* ${ resultMessage } . ${ durationInMinutes } m ${ durationInSeconds } s
60+ *Failed Tests:* ${ totalFail }
61+ <${ reportUrl } |View Report>
62+ ${ tagUsers } `,
63+ } ;
64+
65+ const slackWebhookUrl = process . env . SLACK_WEBHOOK_URL ;
66+
67+ const sendSlackMessage = async ( message ) => {
68+ const payload = {
69+ text : message ,
70+ } ;
71+
72+ try {
73+ const response = await fetch ( slackWebhookUrl , {
74+ method : "POST" ,
75+ headers : {
76+ "Content-Type" : "application/json" ,
77+ } ,
78+ body : JSON . stringify ( payload ) ,
79+ } ) ;
80+
81+ if ( ! response . ok ) {
82+ throw new Error ( `Error sending message to Slack: ${ response . statusText } ` ) ;
83+ }
84+
85+ console . log ( "Message sent to Slack successfully" ) ;
86+ } catch ( error ) {
87+ console . error ( "Error:" , error ) ;
88+ }
89+ } ;
90+
91+ sendSlackMessage ( slackMessage . text ) ;
0 commit comments