Skip to content

Commit

Permalink
areaLabel: Edit issue instead of posting comment.
Browse files Browse the repository at this point in the history
GitHub sends all subscribed users a notification for new comments.
So spamming subscribers with bot comments is annoying.

Fixes #180.
  • Loading branch information
Gittenburg committed Jul 23, 2020
1 parent 39b257e commit 44c25d8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 43 deletions.
3 changes: 0 additions & 3 deletions config/templates/areaLabelAddition.md

This file was deleted.

55 changes: 15 additions & 40 deletions src/events/responses/areaLabel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const referenced = [];

exports.run = async function(issue, repo, label) {
const areaLabel = label.name;
const number = issue.number;
Expand All @@ -17,43 +15,20 @@ exports.run = async function(issue, repo, label) {
const uniqueTeams = this.util.deduplicate(labelTeams);

const areaTeams = `@${repoOwner}/${uniqueTeams.join(`, @${repoOwner}/`)}`;
const references = issueAreaLabels.join("\", \"");

const payload = issue.pull_request ? "pull request" : "issue";
const labelSize = labelTeams.length === 1 ? "label" : "labels";
const template = this.templates.get("areaLabelAddition");

const comment = template.format({
teams: areaTeams, refs: `"${references}"`, labels: labelSize,
payload: payload
});

const comments = await template.getComments({
owner: repoOwner, repo: repoName, number: number
});

const tag = `${repoOwner}/${repoName}#${number}`;

if (comments.length) {
const id = comments[0].id;
if (issueAreaLabels.length) {
this.issues.editComment({
owner: repoOwner, repo: repoName, comment_id: id, body: comment
});
} else {
this.issues.deleteComment({
owner: repoOwner, repo: repoName, comment_id: id
});
}
} else if (!referenced.includes(tag) && issueAreaLabels.length) {
this.issues.createComment({
owner: repoOwner, repo: repoName, number: number, body: comment
});

// Ignore labels added in bulk
referenced.push(tag);
setTimeout(() => {
referenced.splice(referenced.indexOf(tag), 1);
}, 1000);

const prefix = "CC by @zulipbot: ";

const updatedIssue = {
owner: repoOwner, repo: repoName, issue_number: number, body: issue.body
};

const pattern = new RegExp(`${prefix}.+$`, "mg");

if (issue.description.includes(prefix)) {
issue.body = issue.body.replace(pattern, `${prefix}${areaTeams}`);
} else {
issue.body += `\n\n${prefix}${areaTeams}`;
}

this.issues.update(updatedIssue);
};

0 comments on commit 44c25d8

Please sign in to comment.