diff --git a/.github/compare-assets/index.js b/.github/compare-assets/index.js index 132da4ff6b5..764c9388864 100644 --- a/.github/compare-assets/index.js +++ b/.github/compare-assets/index.js @@ -51,7 +51,45 @@ const runner = async () => { .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; + } + } + } + if ( Object.keys( changes ).length === 0 ) { + if ( reportCommentId ) { + await octokit.rest.issues.updateComment( { + owner, + repo, + comment_id: reportCommentId, + body: + '# 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.__', + } ); + } return; } @@ -82,24 +120,35 @@ const runner = async () => { } ); - await octokit.rest.issues.createComment( { - owner, - repo, - issue_number: payload.pull_request.number, - body: - '# 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.__', - } ); + 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 ); }