From a065e225471c325c7477d61cf7d2ce56d15cf40c Mon Sep 17 00:00:00 2001 From: zugdev Date: Mon, 14 Oct 2024 18:45:29 -0300 Subject: [PATCH] feat: separate knip report and deployment comments --- .github/workflows/deploy.yml | 32 ++++++++++++++--------------- .github/workflows/knip-reporter.yml | 32 +++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 19 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 1e62db8d..b675865a 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -84,25 +84,23 @@ jobs: issue_number, }); - // Find the comment to update or create a new one if not found - let existingComment = comments.data.find(comment => comment.user.login === 'github-actions[bot]'); - let body = '| Preview Deployment |\n| ------------------ |\n'; + // Look for an existing "Preview Deployment" comment + let previewComment = comments.data.find(comment => + comment.body.startsWith("| Preview Deployment |") + ); - // If the comment exists, update its body - if (existingComment) { - // Check if the SHA already exists in the comment body to avoid duplicates - if (!existingComment.body.includes(sha)) { - body = existingComment.body + `| [${sha}](${{ env.CLAIMABLE_URL }}) |\n`; - await github.rest.issues.updateComment({ - owner, - repo, - comment_id: existingComment.id, - body - }); - } + const body = `| Preview Deployment |\n| ------------------ |\n| [${sha}](${{ env.CLAIMABLE_URL }}) |\n`; + + if (previewComment) { + // Update the existing "Preview Deployment" comment + await github.rest.issues.updateComment({ + owner, + repo, + comment_id: previewComment.id, + body + }); } else { - // Create a new comment if no existing comment is found - body += `| [${sha}](${{ env.CLAIMABLE_URL }}) |\n`; + // Create a new "Preview Deployment" comment if not found await github.rest.issues.createComment({ owner, repo, diff --git a/.github/workflows/knip-reporter.yml b/.github/workflows/knip-reporter.yml index 282c9a88..8dfd1bbe 100644 --- a/.github/workflows/knip-reporter.yml +++ b/.github/workflows/knip-reporter.yml @@ -24,11 +24,39 @@ jobs: path: ./pr-number.txt trim: true - - name: Report knip results to pull request + - name: Get or create comment ID for Knip Report + id: get-comment + uses: actions/github-script@v7 + with: + script: | + const { owner, repo } = context.repo; + const issue_number = ${{ steps.pr-number.outputs.content }}; + + // Fetch all comments on the PR + const comments = await github.rest.issues.listComments({ + owner, + repo, + issue_number, + }); + + // Look for an existing Knip report comment + let knipComment = comments.data.find(comment => + comment.body.startsWith("| Knip Report |") + ); + + if (knipComment) { + console.log('Found existing Knip report comment:', knipComment.id); + core.setOutput('comment_id', knipComment.id); // Set the comment ID output + } else { + console.log('No existing Knip report comment found.'); + core.setOutput('comment_id', ''); // No existing comment + } + + - name: Report Knip results to pull request uses: gitcoindev/knip-reporter@main with: verbose: true - comment_id: ${{ github.workflow }}-reporter + comment_id: ${{ steps.get-comment.outputs.comment_id || github.workflow }}-knip-report command_script_name: knip-ci annotations: true ignore_results: false