diff --git a/chatcommands.py b/chatcommands.py index 39fff25b2c..218290d09f 100644 --- a/chatcommands.py +++ b/chatcommands.py @@ -700,7 +700,11 @@ def approve(msg, pr_id): raise CmdException(str(e)) -@command(str, privileged=True, whole_msg=True, give_name=True, aliases=["close", "reject-force", "close-force"]) +@command(str, privileged=True, whole_msg=True, give_name=True, aliases=["close", + "reject-force", + "close-force", + "reject-duplicate", + "close-duplicate"]) def reject(msg, args, alias_used="reject"): argsraw = args.split(' "', 1) try: @@ -717,24 +721,25 @@ def reject(msg, args, alias_used="reject"): except IndexError: reason = '' force = alias_used.split("-")[-1] == "force" + duplicate = alias_used.split("-")[-1] == "duplicate" code_permissions = is_code_privileged(msg._client.host, msg.owner.id) if not code_permissions: raise CmdException("You need blacklist manager privileges to reject pull requests") - if len(reason) < 20 and not force: + if len(reason) < 20 and not force and not duplicate: raise CmdException("Please provide an adequate reason for rejection (at least 20 characters long) so the user" " can learn from their mistakes. Use `-force` to force the reject") rejected_image = "https://img.shields.io/badge/blacklisters-rejected-red" message_url = "https://chat.{}/transcript/{}?m={}".format(msg._client.host, msg.room.id, msg.id) chat_user_profile_link = "https://chat.{}/users/{}".format(msg._client.host, msg.owner.id) - rejected_by_text = "[Rejected]({}) by [{}]({}) in {}.".format(message_url, msg.owner.name, - chat_user_profile_link, msg.room.name) + rejected_by_text = ("[Rejected]({})" + (" as a duplicate" if duplicate else "") + " by [{}]({}) in {}.").format( + message_url, msg.owner.name, chat_user_profile_link, msg.room.name) reject_reason_text = " No rejection reason was provided.\n\n" if reason: reject_reason_text = " Reason: '{}'".format(reason) reject_reason_image_text = "\n\n![Rejected with SmokeyReject]({})".format(rejected_image) comment = rejected_by_text + reject_reason_text + reject_reason_image_text try: - message = GitManager.reject_pull_request(pr_id, comment) + message = GitManager.reject_pull_request(pr_id, comment, duplicate) return message except Exception as e: raise CmdException(str(e)) diff --git a/gitmanager.py b/gitmanager.py index c6122078e8..30f3c7f9ba 100644 --- a/gitmanager.py +++ b/gitmanager.py @@ -72,6 +72,13 @@ def comment_on_thread(cls, thread_id, body): response = requests.post(url, data=payload, timeout=GlobalVars.default_requests_timeout, **cls.auth_args) return response.json() + @classmethod + def label_issue(cls, issue_id, labels: list): + url = "https://api.github.com/repos/{}/issues/{}/labels".format(GlobalVars.bot_repo_slug, issue_id) + payload = json.dumps({'labels': labels}) + response = requests.post(url, data=payload, timeout=GlobalVars.default_requests_timeout, **cls.auth_args) + return response.json() + @classmethod def get_pull_request(cls, pr_id, payload): """ Get pull requests info. """ @@ -380,7 +387,7 @@ def merge_pull_request(cls, pr_id, comment=""): cls.gitmanager_lock.release() @classmethod - def reject_pull_request(cls, pr_id, comment=""): + def reject_pull_request(cls, pr_id, comment="", is_duplicate=False): response = requests.get("https://api.github.com/repos/{}/pulls/{}".format(GlobalVars.bot_repo_slug, pr_id), timeout=GlobalVars.default_requests_timeout) if not response: @@ -396,6 +403,8 @@ def reject_pull_request(cls, pr_id, comment=""): if comment: # yay we have comments now GitHubManager.comment_on_thread(pr_id, comment) + if is_duplicate: + GitHubManager.label_issue(pr_id, ["type: duplicate"]) with cls.gitmanager_lock: origin_or_auth = cls.get_origin_or_auth() @@ -405,8 +414,8 @@ def reject_pull_request(cls, pr_id, comment=""): if response: if response.json()["state"] == "closed": git.push('-d', origin_or_auth, ref) - return "Closed pull request [#{0}](https://github.com/{1}/pull/{0}).".format( - pr_id, GlobalVars.bot_repo_slug) + return ("Closed pull request [#{0}](https://github.com/{1}/pull/{0})" + + (" as a duplicate." if is_duplicate else ".")).format(pr_id, GlobalVars.bot_repo_slug) raise RuntimeError("Closing pull request #{} failed. Manual operations required.".format(pr_id))