woocommerce/plugins/woocommerce-blocks/.github/compare-assets/index.js

159 lines
3.7 KiB
JavaScript

/**
* External dependencies
*/
const { getOctokit, context } = require( '@actions/github' );
const { setFailed, getInput, setOutput } = require( '@actions/core' );
const runner = async () => {
try {
const token = getInput( 'repo-token', { required: true } );
const octokit = getOctokit( token );
const payload = context.payload;
const repo = payload.repository.name;
const owner = payload.repository.owner.login;
const oldAssets = require( '../../' +
getInput( 'compare', {
required: true,
} ) );
if ( ! oldAssets ) {
return;
}
const newAssets = require( '../../build/assets.json' );
if ( ! newAssets ) {
return;
}
const createComment = getInput( 'create-comment' );
const changes = Object.fromEntries(
Object.entries( newAssets )
.map( ( [ key, { dependencies = [] } ] ) => {
const oldDependencies =
oldAssets[ key ]?.dependencies || [];
const added = dependencies.filter(
( dependency ) =>
! oldDependencies.includes( dependency )
);
const removed = oldDependencies.filter(
( dependency ) => ! dependencies.includes( dependency )
);
return added.length || removed.length
? [
key,
{
added,
removed,
},
]
: null;
} )
.filter( Boolean )
);
let reportCommentId;
{
const currentComments = await octokit.rest.issues.listComments( {
owner,
repo,
issue_number: payload.pull_request.number,
} );
if (
Array.isArray( currentComments.data ) &&
currentComments.data.length > 0
) {
const comment = currentComments.data.find(
( comment ) =>
comment.body.includes( 'Script Dependencies Report' ) &&
comment.user.login === 'github-actions[bot]'
);
if ( comment ) {
reportCommentId = comment.id;
}
}
}
let commentBody = '';
if ( Object.keys( changes ).length > 0 ) {
let reportContent = '';
Object.entries( changes ).forEach(
( [ handle, { added, removed } ] ) => {
const addedDeps = added.length
? '`' + added.join( '`, `' ) + '`'
: '';
const removedDeps = removed.length
? '`' + removed.join( '`, `' ) + '`'
: '';
let icon = '';
if ( added.length && removed.length ) {
icon = '❓';
} else if ( added.length ) {
icon = '⚠️';
} else if ( removed.length ) {
icon = '🎉';
}
reportContent +=
`| \`${ handle }\` | ${ addedDeps } | ${ removedDeps } | ${ icon } |` +
'\n';
}
);
commentBody =
'## Script Dependencies Report' +
'\n\n' +
'The `compare-assets` action has detected some changed script dependencies between this branch and ' +
'trunk. Please review and confirm the following are correct before merging.' +
'\n\n' +
'| Script Handle | Added | Removed | |' +
'\n' +
'| ------------- | ------| ------- | -- |' +
'\n' +
reportContent +
'\n\n' +
'__This comment was automatically generated by the `./github/compare-assets` action.__';
} else {
commentBody =
'## Script Dependencies Report' +
'\n\n' +
'There is no changed script dependency between this branch and trunk.' +
'\n\n' +
'__This comment was automatically generated by the `./github/compare-assets` action.__';
}
if ( createComment !== 'true' ) {
setOutput( 'comment', commentBody );
return;
}
if ( reportCommentId ) {
await octokit.rest.issues.updateComment( {
owner,
repo,
comment_id: reportCommentId,
body: commentBody,
} );
} else {
await octokit.rest.issues.createComment( {
owner,
repo,
issue_number: payload.pull_request.number,
body: commentBody,
} );
}
} catch ( error ) {
setFailed( error.message );
}
};
runner();