improve CI commenter

This commit is contained in:
Peter Steinberger 2025-06-23 03:20:03 +02:00
parent 456143185a
commit 686860b088

View file

@ -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: '<!-- lint-results -->'
- 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<details>\n<summary>Click to see details</summary>\n\n\`\`\`\n${output}\n\`\`\`\n\n</details>\n`;
}
const commentMarker = '<!-- lint-results -->';
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,
});
}
body = `## 🔍 Code Quality Report\n<!-- lint-results -->\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