woocommerce/packages/js/e2e-environment/bin/post-results-to-github-pr.js

48 lines
1.2 KiB
JavaScript
Raw Normal View History

const fs = require( 'fs' );
const path = require( 'path' );
2022-01-25 20:13:18 +00:00
const resultsFile = path.resolve( __dirname, '../test-results.json' );
const buildOutput = ( results ) => {
const { TITLE, SMOKE_TEST_URL } = process.env;
2022-01-25 16:41:28 +00:00
const resultKeys = Object.keys( results );
let output = `## ${ TITLE }:\n\n`;
output += `**Test URL:** ${ SMOKE_TEST_URL }\n`;
2022-01-25 16:41:28 +00:00
resultKeys.forEach( ( key ) => {
// The keys that we care about all start with 'num'
if ( key.includes( 'num' ) ) {
// match only capitalized words
const words = key.match( /[A-Z][a-z]+/g );
output += `**Total Number of ${ words.join( ' ' ) }:** ${
results[ key ]
}\n`;
}
} );
return output;
};
module.exports = async ( { github, context } ) => {
let output = '';
if ( fs.existsSync( resultsFile ) ) {
const data = fs.readFileSync( resultsFile );
const results = JSON.parse( data );
output = buildOutput( results );
} else {
2022-01-24 15:22:29 +00:00
output = `## Test Results Not Found! \n\n`;
2022-02-01 18:27:25 +00:00
output += 'The path to the `test-results.json` file may need to be updated.';
}
await github.rest.issues.createComment( {
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output,
} );
};