diff --git a/chatcommands.py b/chatcommands.py index 91706e88e4..a769f444a4 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,6 +721,7 @@ 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) self_reject = False try: @@ -736,8 +741,8 @@ def reject(msg, args, alias_used="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) if self_reject: rejected_by_text = "[Self-rejected]({}) by [{}]({}) in {}.".format(message_url, msg.owner.name, chat_user_profile_link, msg.room.name) @@ -749,7 +754,7 @@ def reject(msg, args, alias_used="reject"): reject_reason_image_text = "" comment = rejected_by_text + reject_reason_text + reject_reason_image_text try: - message = GitManager.reject_pull_request(pr_id, comment, self_reject) + message = GitManager.reject_pull_request(pr_id, comment, self_reject, duplicate) return message except Exception as e: raise CmdException(str(e)) diff --git a/gitmanager.py b/gitmanager.py index 659adc9171..1225f7ba6c 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="", self_reject=False): + def reject_pull_request(cls, pr_id, comment="", self_reject=False, 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="", self_reject=False): 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,11 +414,13 @@ def reject_pull_request(cls, pr_id, comment="", self_reject=False): 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})" + + (" as a duplicate." if is_duplicate else ".")).format(pr_id, GlobalVars.bot_repo_slug) if self_reject: return "You self-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}).".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))