From 3b285326e3ae7d986489b974420f0d438769de40 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Sun, 18 Dec 2022 20:17:30 -0600 Subject: [PATCH] github/workflows: Update existing comments for code_size_comment. This modifies the automated code size comment to edit an existing comment if one already exists instead of always creating a new comment. This reduces noise on pull requests that are repeatedly updated. Signed-off-by: David Lechner --- .github/workflows/code_size_comment.yml | 44 +++++++++++++++++++++---- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/.github/workflows/code_size_comment.yml b/.github/workflows/code_size_comment.yml index 6ef6e633df..8baf76a47a 100644 --- a/.github/workflows/code_size_comment.yml +++ b/.github/workflows/code_size_comment.yml @@ -62,14 +62,44 @@ jobs: script: | const fs = require('fs'); - github.rest.issues.createComment({ - issue_number: Number(fs.readFileSync('pr_number')), - owner: context.repo.owner, - repo: context.repo.repo, - body: `Code size report: + const prNumber = Number(fs.readFileSync('pr_number')); + const codeSizeReport = `Code size report: \`\`\` ${fs.readFileSync('diff')} \`\`\` - `, - }); + `; + + const comments = await github.paginate( + github.rest.issues.listComments, + { + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: prNumber, + } + ); + + comments.reverse(); + + const previousComment = comments.find(comment => + comment.user.login === 'github-actions[bot]' + ) + + // if github-actions[bot] already made a comment, update it, + // otherwise create a new comment. + + if (previousComment) { + await github.rest.issues.updateComment({ + owner: context.repo.owner, + repo: context.repo.repo, + comment_id: previousComment.id, + body: codeSizeReport, + }); + } else { + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: prNumber, + body: codeSizeReport, + }); + }