140 lines
3.2 KiB
JavaScript
140 lines
3.2 KiB
JavaScript
/**
|
|
* External dependencies
|
|
*/
|
|
const { getOctokit, context } = require( '@actions/github' );
|
|
const { setFailed, getInput } = 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 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 )
|
|
);
|
|
|
|
if ( Object.keys( changes ).length === 0 ) {
|
|
return;
|
|
}
|
|
|
|
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';
|
|
}
|
|
);
|
|
|
|
const currentComments = await octokit.rest.issues.listComments( {
|
|
owner,
|
|
repo,
|
|
issue_number: payload.pull_request.number,
|
|
} );
|
|
|
|
let reportCommentId;
|
|
if (
|
|
Array.isArray( currentComments.data ) &&
|
|
currentComments.data.length > 0
|
|
) {
|
|
const comment = currentComments.data.find( ( comment ) =>
|
|
comment.body.includes( 'Script Dependencies Report' )
|
|
);
|
|
|
|
if ( comment ) {
|
|
reportCommentId = comment.id;
|
|
}
|
|
}
|
|
|
|
const 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.__';
|
|
|
|
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();
|