Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

areaLabel: Edit issue instead of posting comment. #181

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
};