2025-03-31 20:52:56 -04:00
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
/* eslint-disable @typescript-eslint/no-require-imports */
2024-10-09 07:05:11 -04:00
const fs = require ( 'fs' ) ;
2025-03-31 20:52:56 -04:00
2024-10-09 07:05:11 -04:00
const dayjs = require ( 'dayjs' ) ;
const duration = require ( 'dayjs/plugin/duration' ) ;
dayjs . extend ( duration ) ;
const { TYPE , SERVER _TYPE , BRANCH , PULL _REQUEST , BUILD _ID , MM _ENV , MM _DOCKER _IMAGE , MM _DOCKER _TAG , RELEASE _DATE } =
process . env ;
2024-12-04 06:52:18 -05:00
const resultsFile = 'results/reporter/results.json' ;
2024-10-09 07:05:11 -04:00
const summaryFile = 'results/summary.json' ;
const results = JSON . parse ( fs . readFileSync ( resultsFile , 'utf8' ) ) ;
const summary = JSON . parse ( fs . readFileSync ( summaryFile , 'utf8' ) ) ;
2024-12-04 06:52:18 -05:00
const passRate = ( summary . passed * 100 ) / ( summary . passed + summary . failed ) ;
2024-11-18 03:49:17 -05:00
const totalSpecs = summary . passed + summary . failed ;
2024-10-09 07:05:11 -04:00
const playwrightVersion = results . config . version ;
2024-12-04 06:52:18 -05:00
const playwrightDuration = dayjs . duration ( results . stats . duration , 'millisecond' ) . format ( 'HH:mm:ss' ) ;
2024-10-09 07:05:11 -04:00
function generateTitle ( ) {
let dockerImageLink = '' ;
let releaseDate = '' ;
if ( MM _DOCKER _IMAGE && MM _DOCKER _TAG ) {
dockerImageLink = ` with [ ${ MM _DOCKER _IMAGE } : ${ MM _DOCKER _TAG } ](https://hub.docker.com/r/mattermostdevelopment/ ${ MM _DOCKER _IMAGE } /tags?name= ${ MM _DOCKER _TAG } ) ` ;
}
if ( RELEASE _DATE ) {
releaseDate = ` for ${ RELEASE _DATE } ` ;
}
let title = '' ;
switch ( TYPE ) {
case 'PR' :
title = ` E2E for Pull Request Build: [ ${ BRANCH } ]( ${ PULL _REQUEST } ) ${ dockerImageLink } ` ;
break ;
case 'RELEASE' :
title = ` E2E for Release Build ${ dockerImageLink } ${ releaseDate } ` ;
break ;
case 'MASTER' :
title = ` E2E for Master Nightly Build (Prod tests) ${ dockerImageLink } ` ;
break ;
case 'MASTER_UNSTABLE' :
title = ` E2E for Master Nightly Build (Unstable tests) ${ dockerImageLink } ` ;
break ;
case 'CLOUD' :
title = ` E2E for Cloud Build (Prod tests) ${ dockerImageLink } ${ releaseDate } ` ;
break ;
case 'CLOUD_UNSTABLE' :
title = ` E2E for Cloud Build (Unstable tests) ${ dockerImageLink } ` ;
break ;
default :
title = ` E2E for Build ${ dockerImageLink } ` ;
}
return title ;
}
function generateWebhookBody ( ) {
let testResult ;
const testResults = [
{ status : 'Passed' , priority : 'none' , cutOff : 100 , color : '#43A047' } ,
{ status : 'Failed' , priority : 'low' , cutOff : 98 , color : '#FFEB3B' } ,
{ status : 'Failed' , priority : 'medium' , cutOff : 95 , color : '#FF9800' } ,
{ status : 'Failed' , priority : 'high' , cutOff : 0 , color : '#F44336' } ,
] ;
for ( let i = 0 ; i < testResults . length ; i ++ ) {
if ( passRate >= testResults [ i ] . cutOff ) {
testResult = testResults [ i ] ;
break ;
}
}
2024-12-04 06:52:18 -05:00
const summaryField = ` ${ passRate . toFixed ( 2 ) } % ( ${ summary . passed } / ${ totalSpecs } ) | ${ playwrightDuration } | playwright@ ${ playwrightVersion } ` ;
2024-10-09 07:05:11 -04:00
const serverTypeField = SERVER _TYPE ? '\nTest server: ' + SERVER _TYPE : '' ;
const mmEnvField = MM _ENV ? '\nTest server override: ' + MM _ENV : '' ;
const rollingReleaseMatchRegex = BUILD _ID ? . match ( /-rolling(?<version>[^-]+)-/ ) ;
const rollingReleaseFrom = rollingReleaseMatchRegex ? . groups ? . version ;
const rollingReleaseFromField = rollingReleaseFrom ? ` \n Rolling release upgrade from: ${ rollingReleaseFrom } ` : '' ;
return {
username : 'Playwright UI Test' ,
icon _url : 'https://mattermost.com/wp-content/uploads/2022/02/icon_WS.png' ,
attachments : [
{
color : testResult . color ,
author _name : 'Webapp End-to-end Testing (Playwright)' ,
author _icon : 'https://mattermost.com/wp-content/uploads/2022/02/icon_WS.png' ,
author _link : 'https://www.mattermost.com' ,
title : generateTitle ( ) ,
text : ` ${ summaryField } ${ serverTypeField } ${ rollingReleaseFromField } ${ mmEnvField } ` ,
} ,
] ,
} ;
}
2025-03-31 20:52:56 -04:00
const webhookBody = generateWebhookBody ( ) ;
2024-10-09 07:05:11 -04:00
process . stdout . write ( JSON . stringify ( webhookBody ) ) ;