diff --git a/.github/actions/lint-reporter/action.yml b/.github/actions/lint-reporter/action.yml index b4912577..7a9f88cc 100644 --- a/.github/actions/lint-reporter/action.yml +++ b/.github/actions/lint-reporter/action.yml @@ -17,8 +17,18 @@ inputs: runs: using: 'composite' steps: - - name: Create or Update PR Comment + - name: Find Comment if: github.event_name == 'pull_request' + uses: peter-evans/find-comment@v3 + id: fc + with: + issue-number: ${{ github.event.pull_request.number }} + comment-author: 'github-actions[bot]' + body-includes: '' + + - name: Prepare Comment Body + if: github.event_name == 'pull_request' + id: prepare uses: actions/github-script@v7 with: github-token: ${{ inputs.github-token }} @@ -26,6 +36,7 @@ runs: const title = ${{ toJSON(inputs.title) }}; const result = ${{ toJSON(inputs.lint-result) }}; const output = ${{ toJSON(inputs.lint-output) }}; + const existingCommentId = '${{ steps.fc.outputs.comment-id }}'; const icon = result === 'success' ? '✅' : '❌'; const status = result === 'success' ? 'Passed' : 'Failed'; @@ -37,24 +48,16 @@ runs: sectionContent += `\n
\nClick to see details\n\n\`\`\`\n${output}\n\`\`\`\n\n
\n`; } - const commentMarker = ''; - const issue_number = context.issue.number; - - // Find existing comment - const comments = await github.rest.issues.listComments({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: issue_number, - }); - - const botComment = comments.data.find(comment => - comment.user.type === 'Bot' && comment.body.includes(commentMarker) - ); - let body; - if (botComment) { - // Update existing comment - const existingBody = botComment.body; + if (existingCommentId) { + // Get existing comment body + const { data: comment } = await github.rest.issues.getComment({ + owner: context.repo.owner, + repo: context.repo.repo, + comment_id: parseInt(existingCommentId), + }); + + const existingBody = comment.body; const sectionHeader = `### ${title}`; const nextSectionRegex = /^###\s/m; @@ -86,21 +89,19 @@ runs: // Add new section at the end body = existingBody + '\n\n' + sectionContent; } - - await github.rest.issues.updateComment({ - owner: context.repo.owner, - repo: context.repo.repo, - comment_id: botComment.id, - body: body, - }); } else { // Create new comment - body = `## 🔍 Code Quality Report\n${commentMarker}\n\nThis comment is automatically updated with linting results from CI.\n\n${sectionContent}`; - - await github.rest.issues.createComment({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: issue_number, - body: body, - }); - } \ No newline at end of file + body = `## 🔍 Code Quality Report\n\n\nThis comment is automatically updated with linting results from CI.\n\n${sectionContent}`; + } + + // Store the body for the next step + core.setOutput('comment_body', body); + + - name: Create or Update Comment + if: github.event_name == 'pull_request' + uses: peter-evans/create-or-update-comment@v4 + with: + comment-id: ${{ steps.fc.outputs.comment-id }} + issue-number: ${{ github.event.pull_request.number }} + body: ${{ steps.prepare.outputs.comment_body }} + edit-mode: replace \ No newline at end of file