From 60bf1e286c5c7088261cea4a6fba8046fceea70d Mon Sep 17 00:00:00 2001 From: Tung Du Date: Fri, 22 Jul 2022 17:10:58 +0700 Subject: [PATCH] ci: try update existing report first --- .../.github/compare-assets/index.js | 59 ++++++++++++++----- 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/plugins/woocommerce-blocks/.github/compare-assets/index.js b/plugins/woocommerce-blocks/.github/compare-assets/index.js index 132da4ff6b5..cf6c8fc5cd4 100644 --- a/plugins/woocommerce-blocks/.github/compare-assets/index.js +++ b/plugins/woocommerce-blocks/.github/compare-assets/index.js @@ -82,24 +82,55 @@ const runner = async () => { } ); - await octokit.rest.issues.createComment( { + const currentComments = await octokit.rest.issues.listComments( { 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.__', } ); + + 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 ); }