From 6df86dab3ef442b3e153a69f8133c7911b519d14 Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Wed, 26 Feb 2020 13:10:25 -0800 Subject: [PATCH] jira: Handle comment_created events without issue details. I'm not sure what causes some Jira webhook events to not include the metadata that other events do, but it's definitely a format sent by real installations of Jira (likely a very old version, since this has fields missing from what modern Jira does) and we've seen it in production. The best we can do is encourage users to upgrade Jira for better data. --- .../comment_created_no_issue_details.json | 39 +++++++++++++++++++ zerver/webhooks/jira/tests.py | 6 +++ zerver/webhooks/jira/view.py | 21 +++++++++- 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 zerver/webhooks/jira/fixtures/comment_created_no_issue_details.json diff --git a/zerver/webhooks/jira/fixtures/comment_created_no_issue_details.json b/zerver/webhooks/jira/fixtures/comment_created_no_issue_details.json new file mode 100644 index 0000000000..570579b12a --- /dev/null +++ b/zerver/webhooks/jira/fixtures/comment_created_no_issue_details.json @@ -0,0 +1,39 @@ +{ + "timestamp": 1565964017153, + "webhookEvent": "comment_created", + "comment": { + "self": "https://f20171170.atlassian.net/rest/api/2/issue/10000/comment/12000", + "id": "12000", + "author": { + "self": "https://f20171170.atlassian.net/rest/api/2/user?accountId=5c76b994e1bcdf6294d0eb0f", + "name": "admin", + "key": "admin", + "avatarUrls": { + "48x48": "https://secure.gravatar.com/avatar/bc3f74559ceceb488f4189b7e7bdfec2?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FHA-1.png&size=48&s=48", + "24x24": "https://secure.gravatar.com/avatar/bc3f74559ceceb488f4189b7e7bdfec2?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FHA-1.png&size=24&s=24", + "16x16": "https://secure.gravatar.com/avatar/bc3f74559ceceb488f4189b7e7bdfec2?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FHA-1.png&size=16&s=16", + "32x32": "https://secure.gravatar.com/avatar/bc3f74559ceceb488f4189b7e7bdfec2?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FHA-1.png&size=32&s=32" + }, + "displayName": "Hemanth V. Alluri", + "active": true, + "timeZone": "Asia/Calcutta", + }, + "body": "Sounds like it’s pretty important. I’ll get this fixed ASAP\\!", + "updateAuthor": { + "self": "https://f20171170.atlassian.net/rest/api/2/user?accountId=5c76b994e1bcdf6294d0eb0f", + "name": "admin", + "key": "admin", + "avatarUrls": { + "48x48": "https://secure.gravatar.com/avatar/bc3f74559ceceb488f4189b7e7bdfec2?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FHA-1.png&size=48&s=48", + "24x24": "https://secure.gravatar.com/avatar/bc3f74559ceceb488f4189b7e7bdfec2?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FHA-1.png&size=24&s=24", + "16x16": "https://secure.gravatar.com/avatar/bc3f74559ceceb488f4189b7e7bdfec2?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FHA-1.png&size=16&s=16", + "32x32": "https://secure.gravatar.com/avatar/bc3f74559ceceb488f4189b7e7bdfec2?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FHA-1.png&size=32&s=32" + }, + "displayName": "Hemanth V. Alluri", + "active": true, + "timeZone": "Asia/Calcutta", + }, + "created": "2019-08-16T19:30:17.153+0530", + "updated": "2019-08-16T19:30:17.153+0530", + }, +} diff --git a/zerver/webhooks/jira/tests.py b/zerver/webhooks/jira/tests.py index 5585d39504..0d1803b902 100644 --- a/zerver/webhooks/jira/tests.py +++ b/zerver/webhooks/jira/tests.py @@ -196,6 +196,12 @@ Adding a comment. Oh, what a comment it is! expected_message = """Hemanth V. Alluri commented on issue: *"Add support for newer format Jira issue comment events"*\n``` quote\nSounds like it’s pretty important. I’ll get this fixed ASAP!\n```""" self.send_and_test_stream_message("comment_created", expected_topic, expected_message) + def test_comment_event_comment_created_no_issue_details(self) -> None: + expected_topic = "10000: Upgrade Jira to get the issue title here." + expected_message = """Hemanth V. Alluri commented on issue: *"Upgrade Jira to get the issue title here."*\n``` quote\nSounds like it’s pretty important. I’ll get this fixed ASAP!\n```""" + self.send_and_test_stream_message("comment_created_no_issue_details", + expected_topic, expected_message) + def test_comment_event_comment_edited(self) -> None: expected_topic = "SP-1: Add support for newer format Jira issue comment events" expected_message = """Hemanth V. Alluri updated their comment on issue: *"Add support for newer format Jira issue comment events"*\n``` quote\nThis is a very important issue! I’m on it!\n```""" diff --git a/zerver/webhooks/jira/view.py b/zerver/webhooks/jira/view.py index a7d7a1c90f..0cc2952e91 100644 --- a/zerver/webhooks/jira/view.py +++ b/zerver/webhooks/jira/view.py @@ -125,9 +125,27 @@ def get_issue_author(payload: Dict[str, Any]) -> str: return get_in(payload, ['user', 'displayName']) def get_issue_id(payload: Dict[str, Any]) -> str: + if 'issue' not in payload: + # Some ancient version of Jira or one of its extensions posts + # comment_created events without an "issue" element. For + # these, the best we can do is extract the Jira-intenral + # issue number and use that in the topic. + # + # Users who want better formatting can upgrade Jira. + return payload['comment']['self'].split('/')[-3] + return get_in(payload, ['issue', 'key']) def get_issue_title(payload: Dict[str, Any]) -> str: + if 'issue' not in payload: + # Some ancient version of Jira or one of its extensions posts + # comment_created events without an "issue" element. For + # these, the best we can do is extract the Jira-intenral + # issue number and use that in the topic. + # + # Users who want better formatting can upgrade Jira. + return 'Upgrade Jira to get the issue title here.' + return get_in(payload, ['issue', 'fields', 'summary']) def get_issue_subject(payload: Dict[str, Any]) -> str: @@ -253,10 +271,11 @@ def normalize_comment(comment: str) -> str: return normalized_comment def handle_comment_created_event(payload: Dict[str, Any], user_profile: UserProfile) -> str: + title = get_issue_title(payload) return "{author} commented on issue: *\"{title}\"\ *\n``` quote\n{comment}\n```\n".format( author = payload["comment"]["author"]["displayName"], - title = payload["issue"]["fields"]["summary"], + title = title, comment = normalize_comment(payload["comment"]["body"]) )