From eddeb111c7065a7807e1f171b60180294d7b4f6e Mon Sep 17 00:00:00 2001 From: gparyani Date: Wed, 7 Feb 2024 00:10:31 -0800 Subject: [PATCH 01/11] Add a function to add labels to PRs and add the ability to label an issue as a duplicate in the function to reject PRs. --- gitmanager.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/gitmanager.py b/gitmanager.py index 07c319062c..7f145a5fd2 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, thread_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,7 +414,7 @@ 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( + 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)) From dfdb81955f0698115d7a6a64e5658c10122d7e8d Mon Sep 17 00:00:00 2001 From: gparyani Date: Wed, 7 Feb 2024 00:13:43 -0800 Subject: [PATCH 02/11] Update chatcommands.py to support the duplicate command and pass it along --- chatcommands.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/chatcommands.py b/chatcommands.py index 4f61945cbc..2e24d09f6f 100644 --- a/chatcommands.py +++ b/chatcommands.py @@ -699,7 +699,7 @@ 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"]) def reject(msg, args, alias_used="reject"): argsraw = args.split(' "', 1) try: @@ -716,6 +716,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) if not code_permissions: raise CmdException("You need blacklist manager privileges to reject pull requests") @@ -736,7 +737,7 @@ def reject(msg, args, alias_used="reject"): 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)) From 5f387b33ad0c2ae44904e9a86ba753dc526bc893 Mon Sep 17 00:00:00 2001 From: gparyani Date: Wed, 7 Feb 2024 00:22:24 -0800 Subject: [PATCH 03/11] Fix lint error in gitmanager.py --- gitmanager.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gitmanager.py b/gitmanager.py index 7f145a5fd2..f8f9587c36 100644 --- a/gitmanager.py +++ b/gitmanager.py @@ -414,8 +414,8 @@ def reject_pull_request(cls, pr_id, comment="", is_duplicate=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) + 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)) From 608310b76a25cc6999f52632bc87451c945f8dff Mon Sep 17 00:00:00 2001 From: gparyani Date: Wed, 7 Feb 2024 00:23:53 -0800 Subject: [PATCH 04/11] Fix lint error in chatcommands.py --- chatcommands.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/chatcommands.py b/chatcommands.py index 2e24d09f6f..c4effb9905 100644 --- a/chatcommands.py +++ b/chatcommands.py @@ -699,7 +699,10 @@ 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", "reject-duplicate"]) +@command(str, privileged=True, whole_msg=True, give_name=True, aliases=["close", + "reject-force", + "close-force", + "reject-duplicate"]) def reject(msg, args, alias_used="reject"): argsraw = args.split(' "', 1) try: From 578f06ef61127c8dca37f52d798538a30299488f Mon Sep 17 00:00:00 2001 From: gparyani Date: Wed, 7 Feb 2024 00:25:29 -0800 Subject: [PATCH 05/11] Fix another lint error in gitmanager.py --- gitmanager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitmanager.py b/gitmanager.py index f8f9587c36..7f72e8e1b0 100644 --- a/gitmanager.py +++ b/gitmanager.py @@ -414,7 +414,7 @@ def reject_pull_request(cls, pr_id, comment="", is_duplicate=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})." + + 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)) From f80b1581f67c2eff53439fc121bd8e9991bac31a Mon Sep 17 00:00:00 2001 From: gparyani Date: Wed, 7 Feb 2024 01:15:15 -0800 Subject: [PATCH 06/11] Don't require a reason for the new command; explanatory comment on PR --- chatcommands.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/chatcommands.py b/chatcommands.py index c4effb9905..6c6c5809bb 100644 --- a/chatcommands.py +++ b/chatcommands.py @@ -723,7 +723,7 @@ def reject(msg, args, alias_used="reject"): 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://camo.githubusercontent.com/" \ @@ -732,8 +732,8 @@ def reject(msg, args, alias_used="reject"): "1636b6c6973746572732d72656a65637465642d7265642e706e67" 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) From 54783524b04353a0db2e022eb9978390057ea5c9 Mon Sep 17 00:00:00 2001 From: gparyani Date: Wed, 7 Feb 2024 01:17:06 -0800 Subject: [PATCH 07/11] Missed space --- chatcommands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chatcommands.py b/chatcommands.py index 6c6c5809bb..c0d8e97d3d 100644 --- a/chatcommands.py +++ b/chatcommands.py @@ -732,7 +732,7 @@ def reject(msg, args, alias_used="reject"): "1636b6c6973746572732d72656a65637465642d7265642e706e67" 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]({})" + (" as a duplicate" if duplicate else "") + "by [{}]({}) in {}.").format( + 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: From 910c13fb26e814493951db8a3f96c86666df8e9f Mon Sep 17 00:00:00 2001 From: gparyani Date: Wed, 7 Feb 2024 19:15:48 -0800 Subject: [PATCH 08/11] Add close-duplicate as an alias --- chatcommands.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/chatcommands.py b/chatcommands.py index c0d8e97d3d..2a99a4db25 100644 --- a/chatcommands.py +++ b/chatcommands.py @@ -702,7 +702,8 @@ def approve(msg, pr_id): @command(str, privileged=True, whole_msg=True, give_name=True, aliases=["close", "reject-force", "close-force", - "reject-duplicate"]) + "reject-duplicate", + "close-duplicate"]) def reject(msg, args, alias_used="reject"): argsraw = args.split(' "', 1) try: From 7d7306c3df0505a6f3357a3dd8f334252c1387ab Mon Sep 17 00:00:00 2001 From: gparyani Date: Wed, 29 May 2024 18:08:41 -0700 Subject: [PATCH 09/11] Fix argument error in code and punctuation error in output --- gitmanager.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gitmanager.py b/gitmanager.py index 7f72e8e1b0..1d07c5d525 100644 --- a/gitmanager.py +++ b/gitmanager.py @@ -66,8 +66,8 @@ def create_pull_request(cls, payload): return response.json() @classmethod - def comment_on_thread(cls, thread_id, body): - url = "https://api.github.com/repos/{}/issues/{}/comments".format(GlobalVars.bot_repo_slug, thread_id) + def comment_on_thread(cls, issue_id, body): + url = "https://api.github.com/repos/{}/issues/{}/comments".format(GlobalVars.bot_repo_slug, issue_id) payload = json.dumps({'body': body}) response = requests.post(url, data=payload, timeout=GlobalVars.default_requests_timeout, **cls.auth_args) return response.json() @@ -414,8 +414,8 @@ def reject_pull_request(cls, pr_id, comment="", is_duplicate=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) + 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)) From cc8bb635d647299d9df87260361b8c804f4825e5 Mon Sep 17 00:00:00 2001 From: gparyani Date: Wed, 29 May 2024 18:41:48 -0700 Subject: [PATCH 10/11] Note to self: never use a partial find and replace. --- gitmanager.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gitmanager.py b/gitmanager.py index 1d07c5d525..f58a9c70df 100644 --- a/gitmanager.py +++ b/gitmanager.py @@ -66,15 +66,15 @@ def create_pull_request(cls, payload): return response.json() @classmethod - def comment_on_thread(cls, issue_id, body): - url = "https://api.github.com/repos/{}/issues/{}/comments".format(GlobalVars.bot_repo_slug, issue_id) + def comment_on_thread(cls, thread_id, body): + url = "https://api.github.com/repos/{}/issues/{}/comments".format(GlobalVars.bot_repo_slug, thread_id) payload = json.dumps({'body': 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, thread_id) + 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() From c863f57c55ca44389d5f0ac740be70c7ee0a8d08 Mon Sep 17 00:00:00 2001 From: gparyani Date: Tue, 24 Sep 2024 16:39:47 -0700 Subject: [PATCH 11/11] Fix lint errors --- gitmanager.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gitmanager.py b/gitmanager.py index fdec38df7a..1225f7ba6c 100644 --- a/gitmanager.py +++ b/gitmanager.py @@ -78,7 +78,8 @@ def label_issue(cls, issue_id, labels: list): 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. """ url = "https://api.github.com/repos/{}/pulls/{}".format(GlobalVars.bot_repo_slug, pr_id)