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 13 commits into
base: master
Choose a base branch
from
Open
13 changes: 9 additions & 4 deletions chatcommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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))
Expand Down
17 changes: 14 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, 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. """
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="", 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:
Expand All @@ -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()
Expand All @@ -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))

Expand Down