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

Add a command to allow rejecting a pull request as a duplicate #10018

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
14 changes: 9 additions & 5 deletions chatcommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
@command(str, privileged=True, whole_msg=True, give_name=True, aliases=["close",
"reject-force",
"close-force",
"reject-duplicate"])
gparyani marked this conversation as resolved.
Show resolved Hide resolved
def reject(msg, args, alias_used="reject"):
argsraw = args.split(' "', 1)
try:
Expand All @@ -716,10 +719,11 @@ 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://camo.githubusercontent.com/" \
Expand All @@ -728,15 +732,15 @@ 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)
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))
Expand Down
15 changes: 12 additions & 3 deletions gitmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
gparyani marked this conversation as resolved.
Show resolved Hide resolved
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. """
Expand Down Expand Up @@ -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:
Expand All @@ -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()
Expand All @@ -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})." +
gparyani marked this conversation as resolved.
Show resolved Hide resolved
(" 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))

Expand Down
Loading