Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
webhooks/jira: Add silent mentions via Atlassian account ID.
The Dancebc9ab18ef3d5
diff --git a/starlight_help/src/content/docs/custom-profile-fields.mdx b/starlight_help/src/content/docs/custom-profile-fields.mdxindex ac2b774fe6b..06f2eb25e8a 100644--- a/starlight_help/src/content/docs/custom-profile-fields.mdx+++ b/starlight_help/src/content/docs/custom-profile-fields.mdx@@ -158,7 +158,8 @@ Choose the profile field type that's most appropriate for the requested informat[configure](#use-a-custom-profile-field-in--mention-suggestions) this field tobe used for [@-mention](/help/mention-a-user-or-group) suggestions. Someintegrations ([Bitbucket](/integrations/bitbucket),- [GitHub](/integrations/github), [GitLab](/integrations/gitlab)) use [silent+ [GitHub](/integrations/github), [GitLab](/integrations/gitlab),+ [Jira](/integrations/jira)) use [silentmentions](/help/mention-a-user-or-group#silently-mention-a-user) to refer tousers if a matching external account is configured.* **Dropdown**: A dropdown with a list of predefined options (e.g.,diff --git a/zerver/webhooks/jira/doc.md b/zerver/webhooks/jira/doc.mdindex aff917a607c..e882ed81428 100644--- a/zerver/webhooks/jira/doc.md+++ b/zerver/webhooks/jira/doc.md@@ -2,6 +2,17 @@Get Zulip notifications for your Jira projects!+!!! tip ""++ If you also configure a [custom profile+ field](/help/custom-profile-fields) for Atlassian Cloud account IDs,+ this integration will refer to Jira users using+ [Zulip silent mentions](/help/mention-a-user-or-group#silently-mention-a-user),+ rather than their Jira display name. See [how to find Atlassian+ account IDs][atlassian-account-id].++[atlassian-account-id]: https://developer.atlassian.com/cloud/automation/resources/how-to-get-user-account-IDs/+{start_tabs}1. {!create-an-incoming-webhook.md!}diff --git a/zerver/webhooks/jira/tests.py b/zerver/webhooks/jira/tests.pyindex 38d081f95dc..3b46f4f0dbb 100644--- a/zerver/webhooks/jira/tests.py+++ b/zerver/webhooks/jira/tests.py@@ -1,6 +1,8 @@from unittest.mock import patch+from zerver.actions.custom_profile_fields import try_add_realm_default_custom_profile_fieldfrom zerver.lib.test_classes import WebhookTestCase+from zerver.models.realms import get_realmclass JiraHookTests(WebhookTestCase):@@ -91,7 +93,7 @@ Leonardo Franchi [Administrator] created [TEST-4: Test Created Assignee](https:/def test_priority_updated(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) (assigned to **[email protected]**):+ expected_message = """Leonardo Franchi [Administrator] updated [TEST-1: Fix That](https://zulipp.atlassian.net/browse/TEST-1) (assigned to Leonardo Franchi [Administrator]):* Changed priority from **Critical** to **Major**"""self.check_webhook("issue_updated__priority", expected_topic_name, expected_message)@@ -138,3 +140,27 @@ Leonardo Franchi [Administrator] created [TEST-4: Test Created Assignee](https:/"Unable to parse request: Did Jira generate this event?",e.exception.args[0],)++ def test_created_silent_mention_by_email_fallback(self) -> None:+ othello = self.example_user("othello")+ expected_topic_name = "BUG-15: New bug with hook"+ expected_message = f"""+@_**{othello.full_name}|{othello.id}** created [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15):++* **Priority**: Major+* **Assignee**: no one+""".strip()+ self.check_webhook("issue_created", expected_topic_name, expected_message)++ def test_comment_created_silent_mention_atlassian_account_id(self) -> None:+ realm = get_realm("zulip")+ atlassian_field = try_add_realm_default_custom_profile_field(realm, "atlassian")+ hamlet = self.example_user("hamlet")+ test_account_id = "5c76b994e1bcdf6294d0eb0f"+ self.set_user_custom_profile_data(+ hamlet, [{"id": atlassian_field.id, "value": test_account_id}]+ )++ expected_topic_name = "SP-1: Add support for newer format Jira issue comment events"+ expected_message = f"""@_**{hamlet.full_name}|{hamlet.id}** commented on [SP-1: Add support for newer format Jira issue comment events](https://f20171170.atlassian.net/browse/SP-1)\n``` quote\nThis is a comment that likes to **exercise** a lot of _different_ `conventions` that `jira uses`.\r\n\r\n~~~\n\r\nthis code is not highlighted, but monospaced\r\n\n~~~\r\n\r\n~~~\n\r\ndef python():\r\n print "likes to be formatted"\r\n\n~~~\r\n\r\n[http://www.google.com](http://www.google.com) is a bare link, and [Google](http://www.google.com) is given a title.\r\n\r\nThanks!\r\n\r\n~~~ quote\n\r\nSomeone said somewhere\r\n\n~~~!\n**Niloth**\n@_**{hamlet.full_name}|{hamlet.id}**\n```"""+ self.check_webhook("comment_created", expected_topic_name, expected_message)diff --git a/zerver/webhooks/jira/view.py b/zerver/webhooks/jira/view.pyindex 384656f8b82..2fe156e0e6e 100644--- a/zerver/webhooks/jira/view.py+++ b/zerver/webhooks/jira/view.py@@ -9,11 +9,15 @@ from django.http import HttpRequest, HttpResponsefrom zerver.decorator import webhook_viewfrom zerver.lib.exceptions import AnomalousWebhookPayloadError, UnsupportedWebhookEventTypeError+from zerver.lib.external_accounts import DEFAULT_EXTERNAL_ACCOUNTSfrom zerver.lib.mention import silent_mention_syntax_for_userfrom zerver.lib.response import json_successfrom zerver.lib.typed_endpoint import JsonBodyPayload, typed_endpointfrom zerver.lib.validator import WildValue, check_none_or, check_string-from zerver.lib.webhooks.common import check_send_webhook_message+from zerver.lib.webhooks.common import (+ check_send_webhook_message,+ guess_zulip_user_from_external_account,+)from zerver.models import Realm, UserProfilefrom zerver.models.users import get_user_by_delivery_email@@ -129,20 +133,23 @@ def get_issue_string(return text-def get_assignee_mention(assignee_email: str, realm: Realm) -> str:- if assignee_email != "":- try:- zulip_user = get_user_by_delivery_email(assignee_email, realm)- return silent_mention_syntax_for_user(zulip_user)- except UserProfile.DoesNotExist:- assignee_name = assignee_email- return f"**{assignee_name}**"- return ""--def get_user_mention(realm: Realm, user_payload: WildValue) -> str:"""Return a silent mention for a matched Zulip user, or the display name."""+ # Account IDs are specific to Atlassian Cloud products.+ # Jira Data Center do not include accountId in their webhook payloads+ if "accountId" in user_payload:+ account_id = user_payload["accountId"].tame(check_string)+ external_account_field_name = str(DEFAULT_EXTERNAL_ACCOUNTS["atlassian"].name)+ zulip_user = guess_zulip_user_from_external_account(+ realm,+ account_id,+ external_account_field_name,+ external_username_case_insensitive=True,+ )+ if zulip_user is not None:+ return silent_mention_syntax_for_user(zulip_user)+# The presence of emailAddress in the payload depends on Jira's User email# visibility settings.if "emailAddress" in user_payload:@@ -209,10 +216,12 @@ def handle_updated_issue_event(payload: WildValue, user_profile: UserProfile) ->issue_id = get_in(payload, ["issue", "key"]).tame(check_string)issue = get_issue_string(payload, issue_id, True)- assignee_email = get_in(payload, ["issue", "fields", "assignee", "emailAddress"], "").tame(- check_string- )- assignee_mention = get_assignee_mention(assignee_email, user_profile.realm)+ assignee = get_in(payload, ["issue", "fields", "assignee"])++ if assignee.value and isinstance(assignee.value, dict):+ assignee_mention = get_user_mention(user_profile.realm, assignee)+ else:+ assignee_mention = ""if assignee_mention != "":assignee_blurb = f" (assigned to {assignee_mention})"