Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Fix: Compare action should update its original comment instead of a new comment #6736

Merged
merged 3 commits into from
Aug 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 67 additions & 18 deletions .github/compare-assets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]'
Comment on lines +68 to +70
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice update!

);

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;
}

Expand Down Expand Up @@ -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 );
}
Expand Down