File size: 3,820 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
// NOTE: if we decide to use this again in the future, see https://github.com/Automattic/wp-calypso/pull/85597
// for more context on why this was disabled. It needs to be updated to avoid
// adding so much noise to the channel.
import fs from 'fs';
import { exit } from 'process';
import yargs from 'yargs';
/* CLI Arguments */
const { argv } = yargs( process.argv ).options( {
file: {
type: 'string',
required: true,
describe: 'Path to a JSON file produced by Jest',
},
} );
const filePath = argv.file;
/* Methods */
/**
* Opens and parses the test result JSON produced by Jest.
* @returns {JSON} JSON parsed test suite result.
* @throws {Error} If file was not present.
*/
function openJestOutputJSON() {
let data;
try {
fs.accessSync( filePath );
data = fs.readFileSync( filePath, 'utf8' );
} catch ( error ) {
console.error( 'An error occurred while accessing the file:', error );
exit();
}
return JSON.parse( data );
}
/**
* Extracts all stack traces for failing test steps.
* @param {JSON} results Test suite results produced by Jest.
* @returns {{step: string, error: string}[]} Array of failure objects.
*/
function extractFailureMessages( results ) {
const failureMessages = [];
// Short circuit if nothing consistently failed.
// Consistently here means failed on both initial run and then the retry.
if ( ! results.numFailedTests ) {
return failureMessages;
}
for ( const result of results.testResults ) {
for ( const step of result.assertionResults ) {
if ( step.failureMessages.length !== 0 ) {
failureMessages.push( { step: step.ancestorTitles, error: step.failureMessages } );
}
}
}
return failureMessages;
}
/**
* Builds the message to be posted to Slack using Block Kit.
* @param {{step: string, error: string}[]} failures Array of failure objects.
* @returns {JSON} Body for the POST request.
*/
function buildSlackMessage( failures ) {
// Build the body using Slack Block Kit.
const body = {
channel: 'C02DQP0FP',
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: `:x: E2E Build Failed on branch *${ process.env.tc_build_branch }*: ${ process.env.tc_project_name }: ${ process.env.tc_build_conf_name }, #${ process.env.tc_build_number }`,
},
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: `:teamcity: <${ process.env.BUILD_URL }|*Build*>`,
},
},
{
type: 'divider',
},
{
type: 'header',
text: {
type: 'plain_text',
text: 'Stacktraces',
},
},
],
};
for ( const failure of failures ) {
body.blocks.push(
{
type: 'section',
text: {
type: 'mrkdwn',
text:
'*' + failure.step.join( ': ' ) + '*' + '\n' + '```' + failure.error.pop() + '\n```',
},
},
{ type: 'divider' }
);
}
body.blocks.push( {
type: 'section',
text: {
type: 'mrkdwn',
// text: 'placeholder text so that I do not waste a8c GPT bandwidth',
text: '@gpt, can you tell me more about the error(s) above, provide link(s) to the E2E test file in GitHub, and a snippet of the relevant code section?',
},
} );
return body;
}
/**
* Sends the message to Slack.
* @param {JSON} body Body for the POST request.
* @returns {Promise<Response>} Response from Slack endpoint.
*/
async function postMessage( body ) {
return await fetch( 'https://slack.com/api/chat.postMessage', {
method: 'POST',
headers: {
'Content-type': 'application/json; charset=utf-8',
Authorization: `Bearer ${ process.env.slack_oauth_token }`,
},
body: JSON.stringify( body ),
} );
}
const json = openJestOutputJSON( filePath );
if ( ! json ) {
exit();
}
const failures = extractFailureMessages( json );
if ( failures.length === 0 ) {
exit();
}
const body = buildSlackMessage( failures );
await postMessage( body );
|