@@ -4,6 +4,38 @@ import { WebClient } from '@slack/web-api';
44
55const SLACK_TOKEN = process . env . SLACK_TOKEN ;
66const CHANNEL_ID = 'C0102JZRG3G' ; // infra_tests channel ID
7+ const failedJsonPath = path . join ( __dirname , 'test-results' , 'failed.json' ) ;
8+ const failedData = JSON . parse ( fs . readFileSync ( failedJsonPath , 'utf-8' ) ) ;
9+ const failedCount = failedData . failedTests ?. length || 0 ;
10+
11+ async function uploadFile ( filePath : string ) : Promise < void > {
12+ if ( ! SLACK_TOKEN ) {
13+ console . log ( 'Slack token not specified, skipping upload' ) ;
14+ return ;
15+ }
16+
17+ const epoch = Date . now ( ) ;
18+ const ext = path . extname ( filePath ) ;
19+ const basename = path . basename ( filePath ) ;
20+
21+ const isPlaywrightReport = basename === 'playwright-report.zip' ;
22+ const filename = isPlaywrightReport
23+ ? `playwright-report-${ epoch } .zip`
24+ : `e2e-artifact-${ epoch } ${ ext } ` ;
25+ const comment = isPlaywrightReport
26+ ? `📊 Playwright HTML Report ${ process . env . GITHUB_ACTION_URL || '' } `
27+ : `✖ Test Run ${ process . env . GITHUB_ACTION_URL || '' } ` ;
28+
29+ console . log ( `Uploading ${ filePath } ` ) ;
30+
31+ const slackClient = new WebClient ( SLACK_TOKEN ) ;
32+ await slackClient . files . uploadV2 ( {
33+ channel_id : CHANNEL_ID ,
34+ file : fs . createReadStream ( filePath ) ,
35+ filename,
36+ initial_comment : comment ,
37+ } ) ;
38+ }
739
840function postMessage ( message : string ) : Promise < unknown > {
941 if ( ! SLACK_TOKEN ) {
@@ -29,27 +61,32 @@ function notifyFailure(failedCount: number): Promise<unknown> {
2961 return postMessage ( message ) ;
3062}
3163
32- const failedJsonPath = path . join ( __dirname , 'test-results' , 'failed.json' ) ;
33-
3464if ( ! fs . existsSync ( failedJsonPath ) ) {
3565 console . log ( 'No failed.json found, skipping Slack notification' ) ;
3666 process . exit ( 0 ) ;
3767}
3868
39- const failedData = JSON . parse ( fs . readFileSync ( failedJsonPath , 'utf-8' ) ) ;
40- const failedCount = failedData . failedTests ?. length || 0 ;
41-
4269if ( failedCount === 0 ) {
4370 console . log ( 'No failed tests, skipping Slack notification' ) ;
4471 process . exit ( 0 ) ;
4572}
4673
47- console . log ( `Sending Slack notification for ${ failedCount } failed test(s)...` ) ;
48- notifyFailure ( failedCount )
49- . then ( ( ) => {
50- console . log ( 'Slack notification sent successfully' ) ;
51- process . exit ( 0 ) ;
52- } )
74+ async function main ( ) {
75+ console . log ( `Sending Slack notification for ${ failedCount } failed test(s)...` ) ;
76+ await notifyFailure ( failedCount ) ;
77+ console . log ( 'Slack notification sent successfully' ) ;
78+
79+ // Upload HTML report if zip file exists
80+ const reportZipPath = path . join ( __dirname , 'playwright-report.zip' ) ;
81+ if ( fs . existsSync ( reportZipPath ) ) {
82+ console . log ( 'Uploading HTML report...' ) ;
83+ await uploadFile ( reportZipPath ) ;
84+ console . log ( 'HTML report uploaded successfully' ) ;
85+ }
86+ }
87+
88+ main ( )
89+ . then ( ( ) => process . exit ( 0 ) )
5390 . catch ( ( error ) => {
5491 console . error ( 'Failed to send Slack notification:' , error ) ;
5592 process . exit ( 1 ) ;
0 commit comments