Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

integration: Support reviewer in `get_pull_request_event_message`.

Joelute1ddfbc94c314
diff --git a/zerver/lib/webhooks/git.py b/zerver/lib/webhooks/git.py
index 576aa37f319..139960400a8 100644
--- a/zerver/lib/webhooks/git.py
+++ b/zerver/lib/webhooks/git.py
@@ -55,6 +55,7 @@ PULL_REQUEST_OR_ISSUE_MESSAGE_TEMPLATE_WITH_TITLE = (
"{user_name} {action} [{type}{id} {title}]({url})"
)
PULL_REQUEST_OR_ISSUE_ASSIGNEE_INFO_TEMPLATE = "(assigned to {assignee})"
+PULL_REQUEST_REVIEWER_INFO_TEMPLATE = "(assigned reviewers: {reviewer})"
PULL_REQUEST_BRANCH_INFO_TEMPLATE = "from `{target}` to `{base}`"
CONTENT_MESSAGE_TEMPLATE = "\n~~~ quote\n{message}\n~~~"
@@ -68,6 +69,19 @@ TAG_WITHOUT_URL_TEMPLATE = "{tag_name}"
RELEASE_MESSAGE_TEMPLATE = "{user_name} {action} release [{release_name}]({url}) for tag {tagname}."
+def get_assignee_string(assignees: List[Dict[str, Any]]) -> str:
+ assignees_string = ""
+ if len(assignees) == 1:
+ assignees_string = "{username}".format(**assignees[0])
+ else:
+ usernames = []
+ for a in assignees:
+ usernames.append(a["username"])
+
+ assignees_string = ", ".join(usernames[:-1]) + " and " + usernames[-1]
+ return assignees_string
+
+
def get_push_commits_event_message(
user_name: str,
compare_url: Optional[str],
@@ -173,6 +187,7 @@ def get_pull_request_event_message(
message: Optional[str] = None,
assignee: Optional[str] = None,
assignees: Optional[List[Dict[str, Any]]] = None,
+ reviewer: Optional[str] = None,
type: str = "PR",
title: Optional[str] = None,
) -> str:
@@ -198,23 +213,18 @@ def get_pull_request_event_message(
main_message = f"{main_message} {branch_info}"
if assignees:
- assignees_string = ""
- if len(assignees) == 1:
- assignees_string = "{username}".format(**assignees[0])
- else:
- usernames = []
- for a in assignees:
- usernames.append(a["username"])
-
- assignees_string = ", ".join(usernames[:-1]) + " and " + usernames[-1]
-
+ assignee_string = get_assignee_string(assignees)
assignee_info = PULL_REQUEST_OR_ISSUE_ASSIGNEE_INFO_TEMPLATE.format(
- assignee=assignees_string
+ assignee=assignee_string
)
- main_message = f"{main_message} {assignee_info}"
elif assignee:
assignee_info = PULL_REQUEST_OR_ISSUE_ASSIGNEE_INFO_TEMPLATE.format(assignee=assignee)
+
+ elif reviewer:
+ assignee_info = PULL_REQUEST_REVIEWER_INFO_TEMPLATE.format(reviewer=reviewer)
+
+ if assignees or assignee or reviewer:
main_message = f"{main_message} {assignee_info}"
punctuation = ":" if message else "."
diff --git a/zerver/webhooks/bitbucket3/tests.py b/zerver/webhooks/bitbucket3/tests.py
index 574f3517693..534fd7725bd 100644
--- a/zerver/webhooks/bitbucket3/tests.py
+++ b/zerver/webhooks/bitbucket3/tests.py
@@ -135,7 +135,7 @@ class Bitbucket3HookTests(WebhookTestCase):
def test_pr_opened_with_two_reviewers(self) -> None:
expected_topic = "sandbox / PR #5 Add Notes Feature"
- expected_message = """[hypro999](http://139.59.64.214:7990/users/hypro999) opened [PR #5](http://139.59.64.214:7990/projects/SBOX/repos/sandbox/pull-requests/5) from `master` to `master` (assigned to [shimura](http://139.59.64.214:7990/users/shimura) and [sougo](http://139.59.64.214:7990/users/sougo) for review)."""
+ expected_message = """[hypro999](http://139.59.64.214:7990/users/hypro999) opened [PR #5](http://139.59.64.214:7990/projects/SBOX/repos/sandbox/pull-requests/5) from `master` to `master` (assigned reviewers: [shimura](http://139.59.64.214:7990/users/shimura) and [sougo](http://139.59.64.214:7990/users/sougo))."""
self.check_webhook(
"pull_request_opened_with_two_reviewers", expected_topic, expected_message
)
@@ -144,26 +144,26 @@ class Bitbucket3HookTests(WebhookTestCase):
expected_topic = "sandbox / PR #5 Add Notes Feature"
expected_topic = "custom_topic"
self.url = self.build_webhook_url(topic="custom_topic")
- expected_message = """[hypro999](http://139.59.64.214:7990/users/hypro999) opened [PR #5 Add Notes Feature](http://139.59.64.214:7990/projects/SBOX/repos/sandbox/pull-requests/5) from `master` to `master` (assigned to [shimura](http://139.59.64.214:7990/users/shimura) and [sougo](http://139.59.64.214:7990/users/sougo) for review)."""
+ expected_message = """[hypro999](http://139.59.64.214:7990/users/hypro999) opened [PR #5 Add Notes Feature](http://139.59.64.214:7990/projects/SBOX/repos/sandbox/pull-requests/5) from `master` to `master` (assigned reviewers: [shimura](http://139.59.64.214:7990/users/shimura) and [sougo](http://139.59.64.214:7990/users/sougo))."""
self.check_webhook(
"pull_request_opened_with_two_reviewers", expected_topic, expected_message
)
def test_pr_opened_with_multiple_reviewers(self) -> None:
expected_topic = "sandbox / PR #6 sample_file: Add sample_file.txt."
- expected_message = """[hypro999](http://139.59.64.214:7990/users/hypro999) opened [PR #6](http://139.59.64.214:7990/projects/SBOX/repos/sandbox/pull-requests/6) from `master` to `master` (assigned to [sougo](http://139.59.64.214:7990/users/sougo), [zura](http://139.59.64.214:7990/users/zura) and [shimura](http://139.59.64.214:7990/users/shimura) for review):\n\n~~~ quote\nAdd a simple text file for further testing purposes.\n~~~"""
+ expected_message = """[hypro999](http://139.59.64.214:7990/users/hypro999) opened [PR #6](http://139.59.64.214:7990/projects/SBOX/repos/sandbox/pull-requests/6) from `master` to `master` (assigned reviewers: [sougo](http://139.59.64.214:7990/users/sougo), [zura](http://139.59.64.214:7990/users/zura) and [shimura](http://139.59.64.214:7990/users/shimura)):\n\n~~~ quote\nAdd a simple text file for further testing purposes.\n~~~"""
self.check_webhook(
"pull_request_opened_with_multiple_reviewers", expected_topic, expected_message
)
def test_pr_modified(self) -> None:
expected_topic = "sandbox / PR #1 Branch1"
- expected_message = """[hypro999](http://139.59.64.214:7990/users/hypro999) modified [PR #1](http://139.59.64.214:7990/projects/SBOX/repos/sandbox/pull-requests/1) from `branch1` to `master` (assigned to [shimura](http://139.59.64.214:7990/users/shimura) for review):\n\n~~~ quote\n* Add file2.txt\n* Add file3.txt\nBoth of these files would be important additions to the project!\n~~~"""
+ expected_message = """[hypro999](http://139.59.64.214:7990/users/hypro999) modified [PR #1](http://139.59.64.214:7990/projects/SBOX/repos/sandbox/pull-requests/1) from `branch1` to `master` (assigned reviewers: [shimura](http://139.59.64.214:7990/users/shimura)):\n\n~~~ quote\n* Add file2.txt\n* Add file3.txt\nBoth of these files would be important additions to the project!\n~~~"""
self.check_webhook("pull_request_modified", expected_topic, expected_message)
def test_pr_modified_with_include_title(self) -> None:
expected_topic = "custom_topic"
- expected_message = """[hypro999](http://139.59.64.214:7990/users/hypro999) modified [PR #1 Branch1](http://139.59.64.214:7990/projects/SBOX/repos/sandbox/pull-requests/1) from `branch1` to `master` (assigned to [shimura](http://139.59.64.214:7990/users/shimura) for review):\n\n~~~ quote\n* Add file2.txt\n* Add file3.txt\nBoth of these files would be important additions to the project!\n~~~"""
+ expected_message = """[hypro999](http://139.59.64.214:7990/users/hypro999) modified [PR #1 Branch1](http://139.59.64.214:7990/projects/SBOX/repos/sandbox/pull-requests/1) from `branch1` to `master` (assigned reviewers: [shimura](http://139.59.64.214:7990/users/shimura)):\n\n~~~ quote\n* Add file2.txt\n* Add file3.txt\nBoth of these files would be important additions to the project!\n~~~"""
self.url = self.build_webhook_url(topic="custom_topic")
self.check_webhook("pull_request_modified", expected_topic, expected_message)
diff --git a/zerver/webhooks/bitbucket3/view.py b/zerver/webhooks/bitbucket3/view.py
index ca04bec8015..d6812075e19 100644
--- a/zerver/webhooks/bitbucket3/view.py
+++ b/zerver/webhooks/bitbucket3/view.py
@@ -14,7 +14,6 @@ from zerver.lib.webhooks.common import (
validate_extract_webhook_http_header,
)
from zerver.lib.webhooks.git import (
- CONTENT_MESSAGE_TEMPLATE,
TOPIC_WITH_BRANCH_TEMPLATE,
TOPIC_WITH_PR_OR_ISSUE_INFO_TEMPLATE,
get_commits_comment_action_message,
@@ -251,32 +250,8 @@ def get_pr_opened_or_modified_body(
) -> str:
pr = payload["pullRequest"]
description = pr.get("description").tame(check_none_or(check_string))
- assignees_string = get_assignees_string(pr)
- if assignees_string:
- # Then use the custom message template for this particular integration so that we can
- # specify the reviewers at the end of the message (but before the description/message).
- parameters = {
- "user_name": get_user_name(payload),
- "action": action,
- "url": pr["links"]["self"][0]["href"].tame(check_string),
- "number": pr["id"].tame(check_int),
- "source": pr["fromRef"]["displayId"].tame(check_string),
- "destination": pr["toRef"]["displayId"].tame(check_string),
- "message": description,
- "assignees": assignees_string,
- "title": pr["title"].tame(check_string) if include_title else None,
- }
- if include_title:
- body = PULL_REQUEST_OPENED_OR_MODIFIED_TEMPLATE_WITH_REVIEWERS_WITH_TITLE.format(
- **parameters,
- )
- else:
- body = PULL_REQUEST_OPENED_OR_MODIFIED_TEMPLATE_WITH_REVIEWERS.format(**parameters)
- punctuation = ":" if description else "."
- body = f"{body}{punctuation}"
- if description:
- body += "\n" + CONTENT_MESSAGE_TEMPLATE.format(message=description)
- return body
+ reviewers_string = get_assignees_string(pr)
+
return get_pull_request_event_message(
user_name=get_user_name(payload),
action=action,
@@ -285,7 +260,7 @@ def get_pr_opened_or_modified_body(
target_branch=pr["fromRef"]["displayId"].tame(check_string),
base_branch=pr["toRef"]["displayId"].tame(check_string),
message=description,
- assignee=assignees_string if assignees_string else None,
+ reviewer=reviewers_string if reviewers_string else None,
title=pr["title"].tame(check_string) if include_title else None,
)