woocommerce/.github/workflows/scripts/evaluate-jobs-conclusions.js
Adrian Moldovan 9b8063f6b2
[testing workflows] Add optional jobs in CI workflow (#47414)
* Include performance tests job in the needed jobs for evaluation

* Use Github api to get the workflow jobs status

* Evaluate project-jobs

* See results

* Check for cancelled jobs

* Add evaluate-project-jobs.js and rename optional jobs

* Fix script path

* Checkout first

* And then fix the path again.

* Update env variable name and be more detailed in what are the required variables

* Use a flat matrix env variable

* Update to trigger test jobs

* Use the jobs name to evaluate optional requirement

* Fix conditions

* Prettier print

* Test lint job failure

* Remove unused MATRIX env variable

* Fix test

* Remove unused MATRIX variable check

* Only run my account e2e tests

* Remove unused function

* Nicer console printing

* Revert change that triggers failing lint job

* Force failing e2e test

* Revert e2e test command and forced failure

* Added test data and test mode

* Added more test data

* Fixed lint errors and warnings

* Exclude .github folder from eslintignore

* Change to trigger everything

* Revert change to trigger everything
2024-05-16 13:31:17 -07:00

91 lines
2.1 KiB
JavaScript

/* eslint-disable no-console */
const { REPOSITORY, RUN_ID, GITHUB_TOKEN, TEST_MODE } = process.env;
const IGNORED_JOBS = [
'Evaluate Project Job Statuses',
'Report e2e tests results',
'Report API tests results',
];
const isJobRequired = ( job ) => {
return (
! job.name.endsWith( '(optional)' ) &&
! IGNORED_JOBS.includes( job.name )
);
};
const fetchJobs = async () => {
try {
const response = await fetch(
`https://api.github.com/repos/${ REPOSITORY }/actions/runs/${ RUN_ID }/jobs`,
{
headers: {
'User-Agent': 'node.js',
Authorization: `Bearer ${ GITHUB_TOKEN }`,
},
}
);
const data = await response.json();
return data.jobs;
} catch ( error ) {
console.error( 'Error:', error );
// We want to fail if there is an error getting the jobs conclusions
process.exit( 1 );
}
};
const evaluateJobs = async () => {
let jobs;
if ( TEST_MODE ) {
jobs = require( './evaluate-jobs-conclusions-test-data.json' );
} else {
jobs = await fetchJobs();
}
const nonSuccessfulCompletedJobs = jobs.filter(
( job ) =>
job.status === 'completed' &&
job.conclusion !== 'success' &&
job.conclusion !== 'skipped'
);
console.log( 'Workflow jobs:', jobs.length );
console.log(
'Non successful completed jobs:',
nonSuccessfulCompletedJobs.length
);
const failed = [];
nonSuccessfulCompletedJobs.forEach( ( job ) => {
const jobPrintName = `'${ job.name }': ${ job.status }, ${ job.conclusion }`;
if ( isJobRequired( job ) ) {
console.error( `${ jobPrintName }, required` );
failed.push( job.name );
} else {
console.warn( `${ jobPrintName }, optional` );
}
} );
if ( failed.length > 0 ) {
console.error( 'Failed required jobs:', failed );
process.exit( 1 );
}
};
const validateEnvironmentVariables = ( variables ) => {
if ( TEST_MODE ) {
return;
}
variables.forEach( ( variable ) => {
if ( ! process.env[ variable ] ) {
console.error( `Missing ${ variable } environment variable` );
process.exit( 1 );
}
} );
};
validateEnvironmentVariables( [ 'REPOSITORY', 'RUN_ID', 'GITHUB_TOKEN' ] );
evaluateJobs().then( () => {
console.log( 'All required jobs passed' );
} );