ci: find and edit existing comment for benchmark results (#8531)

This commit is contained in:
André Costa
2025-07-08 14:32:12 +02:00
committed by GitHub
parent 44deccc5bd
commit 63b1dcecc8
6 changed files with 41 additions and 46 deletions
@@ -97,7 +97,6 @@ jobs:
--new input/results*.json \
--output comment.md
fi
- name: Comment PR
if: ${{ github.event.workflow_run.event == 'pull_request' }}
uses: actions/github-script@v7
@@ -106,28 +105,49 @@ jobs:
const fs = require('fs');
const commentPath = 'comment.md';
const prPath = 'pr_number';
if (!fs.existsSync(commentPath)) {
throw new Error('Error: comment.md not found! Exiting.');
}
if (!fs.existsSync(prPath)) {
throw new Error('Error: pr_number not found! Exiting.');
}
const commentBody = fs.readFileSync(commentPath, 'utf8').trim();
if (commentBody.length == 0) {
console.log("Not generating a comment as the comment body is empty");
process.exit(0);
}
const prNumber = Number(fs.readFileSync(prPath, 'utf8').trim());
github.rest.issues.createComment({
// Try to find if a comment already exists so we avoid spamming the PR with comments
const { data: comments } = await github.rest.issues.listComments({
issue_number: prNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body: commentBody
});
const existingComment = comments.find(comment =>
comment.body.includes(':robot: This comment was automatically generated by a bot.')
);
try {
// Now we either edit the already existing comment or we generate a new one
if (existingComment) {
await github.rest.issues.updateComment({
comment_id: existingComment.id,
owner: context.repo.owner,
repo: context.repo.repo,
body: commentBody
});
console.log(`Updated existing comment (ID: ${existingComment.id})`);
} else {
await github.rest.issues.createComment({
issue_number: prNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body: commentBody
});
console.log('Created new comment');
}
} catch (error) {
console.error("Error:", error.message);
}
- name: Serialize Results
if: ${{ github.event.workflow_run.event == 'push' && github.event.workflow_run.head_branch == 'master' }}