Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
jira: Match Zulip users by email for silent mentions.
The Dance2834414f8d74
diff --git a/zerver/webhooks/jira/fixtures/issue_updated__status.json b/zerver/webhooks/jira/fixtures/issue_updated__status.jsonindex 9eb7fa4b006..62c574ec679 100644--- a/zerver/webhooks/jira/fixtures/issue_updated__status.json+++ b/zerver/webhooks/jira/fixtures/issue_updated__status.json@@ -7,7 +7,7 @@"name": "admin","accountId": "99:b8d8a054-2e12-4839-bd5f-5f2b7c5f5e3a","key": "admin",- "emailAddress": "[email protected]",+ "emailAddress": "[email protected]","avatarUrls": {"48x48": "https://secure.gravatar.com/avatar/d832c4f48fb6bc323411fbbcd25079ad?d=mm&s=48","24x24": "https://secure.gravatar.com/avatar/d832c4f48fb6bc323411fbbcd25079ad?d=mm&s=24",diff --git a/zerver/webhooks/jira/tests.py b/zerver/webhooks/jira/tests.pyindex e2806052783..38d081f95dc 100644--- a/zerver/webhooks/jira/tests.py+++ b/zerver/webhooks/jira/tests.py@@ -15,7 +15,7 @@ class JiraHookTests(WebhookTestCase):content_type="application/json",)expected_content = """-Leo Franchi created [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15):+@_**Othello, the Moor of Venice|12** created [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15):* **Priority**: Major* **Assignee**: no one@@ -30,7 +30,7 @@ Leo Franchi created [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/def test_created(self) -> None:expected_topic_name = "BUG-15: New bug with hook"expected_message = """-Leo Franchi created [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15):+@_**Othello, the Moor of Venice|12** created [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15):* **Priority**: Major* **Assignee**: no one@@ -60,7 +60,7 @@ Leo Franchi created [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/def test_created_with_unicode(self) -> None:expected_topic_name = "BUG-15: New bug with à hook"expected_message = """-Leo Franchià created [BUG-15: New bug with à hook](http://lfranchi.com:8080/browse/BUG-15):+@_**Othello, the Moor of Venice|12** created [BUG-15: New bug with à hook](http://lfranchi.com:8080/browse/BUG-15):* **Priority**: Major* **Assignee**: no one@@ -79,14 +79,14 @@ Leonardo Franchi [Administrator] created [TEST-4: Test Created Assignee](https:/def test_deleted(self) -> None:expected_topic_name = "BUG-15: New bug with hook"- expected_message = "Leo Franchi deleted [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15)."+ expected_message = "@_**Othello, the Moor of Venice|12** deleted [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15)."self.check_webhook("issue_deleted", expected_topic_name, expected_message)def test_reassigned(self) -> None:expected_topic_name = "BUG-15: New bug with hook"- expected_message = """Leo Franchi updated [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15) (assigned to **Othello, the Moor of Venice**):+ expected_message = """@_**Othello, the Moor of Venice|12** updated [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15) (assigned to @_**Othello, the Moor of Venice|12**):-* Changed assignee to **Othello, the Moor of Venice**"""+* Changed assignee to @_**Othello, the Moor of Venice|12**"""self.check_webhook("issue_updated__reassigned", expected_topic_name, expected_message)def test_priority_updated(self) -> None:@@ -98,7 +98,7 @@ Leonardo Franchi [Administrator] created [TEST-4: Test Created Assignee](https:/def test_status_changed(self) -> None:expected_topic_name = "TEST-1: Fix That"- expected_message = """Leonardo Franchi [Administrator] updated [TEST-1: Fix That](https://zulipp.atlassian.net/browse/TEST-1):+ expected_message = """@_**Othello, the Moor of Venice|12** updated [TEST-1: Fix That](https://zulipp.atlassian.net/browse/TEST-1):* Changed status from **To Do** to **In Progress**"""self.check_webhook("issue_updated__status", expected_topic_name, expected_message)diff --git a/zerver/webhooks/jira/view.py b/zerver/webhooks/jira/view.pyindex 3ba01f9b1aa..384656f8b82 100644--- a/zerver/webhooks/jira/view.py+++ b/zerver/webhooks/jira/view.py@@ -132,15 +132,33 @@ def get_issue_string(def get_assignee_mention(assignee_email: str, realm: Realm) -> str:if assignee_email != "":try:- assignee_name = get_user_by_delivery_email(assignee_email, realm).full_name+ zulip_user = get_user_by_delivery_email(assignee_email, realm)+ return silent_mention_syntax_for_user(zulip_user)except UserProfile.DoesNotExist:assignee_name = assignee_emailreturn f"**{assignee_name}**"return ""-def get_issue_author(payload: WildValue) -> str:- return get_in(payload, ["user", "displayName"]).tame(check_string)+def get_user_mention(realm: Realm, user_payload: WildValue) -> str:+ """Return a silent mention for a matched Zulip user, or the display name."""++ # The presence of emailAddress in the payload depends on Jira's User email+ # visibility settings.+ if "emailAddress" in user_payload:+ email = user_payload["emailAddress"].tame(check_string)+ try:+ zulip_user = get_user_by_delivery_email(email, realm)+ return silent_mention_syntax_for_user(zulip_user)+ except UserProfile.DoesNotExist:+ pass++ return user_payload["displayName"].tame(check_string)+++def get_issue_author(payload: WildValue, realm: Realm) -> str:+ user_payload = payload.get("user")+ return get_user_mention(realm, user_payload)def get_issue_id(payload: WildValue) -> str:@@ -201,7 +219,9 @@ def handle_updated_issue_event(payload: WildValue, user_profile: UserProfile) ->else:assignee_blurb = ""- content = f"{get_issue_author(payload)} updated {issue}{assignee_blurb}:\n\n"+ content = (+ f"{get_issue_author(payload, user_profile.realm)} updated {issue}{assignee_blurb}:\n\n"+ )changelog = payload.get("changelog")if changelog:@@ -234,7 +254,7 @@ def handle_created_issue_event(payload: WildValue, user_profile: UserProfile) ->""".strip()return template.format(- author=get_issue_author(payload),+ author=get_issue_author(payload, user_profile.realm),issue_string=get_issue_string(payload, with_title=True),priority=get_in(payload, ["issue", "fields", "priority", "name"]).tame(check_string),assignee=get_in(payload, ["issue", "fields", "assignee", "displayName"], "no one").tame(@@ -248,7 +268,7 @@ def handle_deleted_issue_event(payload: WildValue, user_profile: UserProfile) ->title = get_issue_title(payload)punctuation = "." if title[-1] not in string.punctuation else ""return template.format(- author=get_issue_author(payload),+ author=get_issue_author(payload, user_profile.realm),issue_string=get_issue_string(payload, with_title=True),punctuation=punctuation,)@@ -264,10 +284,15 @@ def normalize_comment(comment: str, realm: Realm) -> str:return normalized_comment+def get_comment_author(comment_payload: WildValue, realm: Realm) -> str:+ author_payload = comment_payload.get("author")+ return get_user_mention(realm, author_payload)++def handle_comment_created_event(payload: WildValue, user_profile: UserProfile) -> str:return "{author} commented on {issue_string}\\n``` quote\n{comment}\n```\n".format(- author=payload["comment"]["author"]["displayName"].tame(check_string),+ author=get_comment_author(payload["comment"], user_profile.realm),issue_string=get_issue_string(payload, with_title=True),comment=normalize_comment(payload["comment"]["body"].tame(check_string), user_profile.realm@@ -278,7 +303,7 @@ def handle_comment_created_event(payload: WildValue, user_profile: UserProfile)def handle_comment_updated_event(payload: WildValue, user_profile: UserProfile) -> str:return "{author} updated their comment on {issue_string}\\n``` quote\n{comment}\n```\n".format(- author=payload["comment"]["author"]["displayName"].tame(check_string),+ author=get_comment_author(payload["comment"], user_profile.realm),issue_string=get_issue_string(payload, with_title=True),comment=normalize_comment(payload["comment"]["body"].tame(check_string), user_profile.realm@@ -289,7 +314,7 @@ def handle_comment_updated_event(payload: WildValue, user_profile: UserProfile)def handle_comment_deleted_event(payload: WildValue, user_profile: UserProfile) -> str:return "{author} deleted their comment on {issue_string}\\n``` quote\n~~{comment}~~\n```\n".format(- author=payload["comment"]["author"]["displayName"].tame(check_string),+ author=get_comment_author(payload["comment"], user_profile.realm),issue_string=get_issue_string(payload, with_title=True),comment=normalize_comment(payload["comment"]["body"].tame(check_string), user_profile.realm