Skip to content

Commit

Permalink
Avoid potential spamming for BCR review bot (#1869)
Browse files Browse the repository at this point in the history
- Don't notify the maintainer if they are the PR author
- Avoid reposting the same comment if it's already posted in the past
two weeks.
  • Loading branch information
meteorcloudy committed Feb 5, 2024
1 parent 7a8d90d commit 6109f3b
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion actions/bcr-pr-review-notifier/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,39 @@ async function notifyMaintainers(octokit, owner, repo, prNumber, maintainersMap)
}

// Notify maintainers based on grouped module lists
const prAuthor = context.payload.pull_request.user.login;
for (const [modulesList, maintainers] of moduleListToMaintainers.entries()) {
const maintainersList = Array.from(maintainers).join(', ');
// Skip notifying the PR author if they are one of the module maintainers
const maintainersCopy = new Set(maintainers);
if (maintainersCopy.has(`@${prAuthor}`)) {
console.log(`Skipping notifying PR author ${prAuthor} from the maintainers list for modules: ${modulesList}`);
maintainersCopy.delete(`@${prAuthor}`);
}
if (maintainersCopy.size === 0) {
continue;
}
const maintainersList = Array.from(maintainersCopy).join(', ');
console.log(`Notifying ${maintainersList} for modules: ${modulesList}`);
const commentBody = `Hello ${maintainersList}, modules you maintain (${modulesList}) have been updated in this PR. Please review the changes.`;
await postComment(octokit, owner, repo, prNumber, commentBody);
}
}

async function postComment(octokit, owner, repo, prNumber, body) {
// Check if the same comment already exists for the PR in the past two weeks
const existingComments = await octokit.rest.issues.listComments({
owner,
repo,
issue_number: prNumber,
since: new Date(Date.now() - 14 * 24 * 60 * 60 * 1000).toISOString(), // Two weeks ago
});

const commentExists = existingComments.data.some(comment => comment.body === body);
if (commentExists) {
console.log('Skipping comment as it\'s already posted for the PR within the past two weeks.');
return;
}

const comment = {
owner,
repo,
Expand Down

0 comments on commit 6109f3b

Please sign in to comment.