Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
webhooks/sentry: Use datetime_to_global_time and ISO 8601 syntax.
Niloth Pdc7b5e0c51d5
diff --git a/zerver/webhooks/sentry/tests.py b/zerver/webhooks/sentry/tests.pyindex 9da41ab7173..8e156bbf8a4 100644--- a/zerver/webhooks/sentry/tests.py+++ b/zerver/webhooks/sentry/tests.py@@ -12,7 +12,7 @@ class SentryHookTests(WebhookTestCase):**New exception:** [*url.Error: Get "bad_url": unsupported protocol scheme ""](https://sentry.io/organizations/hypro999-personal-organization/issues/1637164584/events/80777a9cc30e4d0eb8904333d5c298b0/)```quote**level:** error-**timestamp:** <time:2020-04-29 11:23:45>+**timestamp:** <time:2020-04-29T11:23:45+00:00>**filename:** trigger-exception.go```@@ -38,7 +38,7 @@ Traceback:**New exception:** [Error: Sample error from node.](https://sentry.io/organizations/hypro999-personal-organization/issues/1638852747/events/f9cb0f2afff74a5aa92e766fb7ac3fe3/)```quote**level:** error-**timestamp:** <time:2020-04-30 06:19:33>+**timestamp:** <time:2020-04-30T06:19:33+00:00>**filename:** /home/hemanth/Desktop/sentry/trigger-exception.js```@@ -64,7 +64,7 @@ Traceback:**New exception:** [Exception: Custom exception!](https://sentry.io/organizations/hypro999-personal-organization/issues/1635244907/events/599349254a1447a99774b5310711c1a8/)```quote**level:** error-**timestamp:** <time:2020-04-28 13:56:05>+**timestamp:** <time:2020-04-28T13:56:05+00:00>**filename:** trigger-exception.py```@@ -88,7 +88,7 @@ Traceback:**New exception:** [ZeroDivisionError: divided by 0](https://sentry.io/organizations/nitk-46/issues/4213933362/events/49b528e13e45497ab9adc3173fd2ed34/)```quote**level:** error-**timestamp:** <time:2023-05-29 10:12:33>+**timestamp:** <time:2023-05-29T10:12:33+00:00>**filename:** app/controllers/articles_controller.rb```@@ -117,7 +117,7 @@ Traceback:**New exception:** [TypeError: Cannot read properties of null (reading 'insertBefore')](https://sentry.io/organizations/nitk-46/issues/4214010673/events/292f78454e774e62999506f759ad791d/)```quote**level:** error-**timestamp:** <time:2023-05-29 11:08:30>+**timestamp:** <time:2023-05-29T11:08:30+00:00>**filename:** /node_modules/.vite/deps/chunk-G4DFXOZZ.js```"""self.check_webhook("event_for_exception_vue", expected_topic_name, expected_message)@@ -128,7 +128,7 @@ Traceback:**New exception:** [ValueError: new sentry error.](https://sentry.io/organizations/bar-foundation/issues/1972208801/events/c916dccfd58e41dcabaebef0091f0736/)```quote**level:** error-**timestamp:** <time:2020-10-21 23:25:11>+**timestamp:** <time:2020-10-21T23:25:11+00:00>**filename:** trigger-exception.py```@@ -153,7 +153,7 @@ Traceback:**New exception:** [TypeError: can't access property "bar", x.foo is undefined](https://sentry.io/organizations/foo-bar-org/issues/1982047746/events/f3bf5fc4e354451db9e885a69b2a2b51/)```quote**level:** error-**timestamp:** <time:2020-10-26 16:39:54>+**timestamp:** <time:2020-10-26T16:39:54+00:00>**filename:** None```"""self.check_webhook(@@ -166,7 +166,7 @@ Traceback:**New exception:** [Error: Something external broke.](https://sentry.io/organizations/hypro999-personal-organization/issues/1731239773/events/355c3b2a142046629dd410db2fdda003/)```quote**level:** error-**timestamp:** <time:2020-06-17 14:42:54>+**timestamp:** <time:2020-06-17T14:42:54+00:00>**filename:** /mnt/data/Documents/Stuff%20for%20Zulip/Repos/sentry/js/external.js```"""self.check_webhook("event_for_exception_js", expected_topic_name, expected_message)@@ -177,7 +177,7 @@ Traceback:**New message event:** [A test message event from golang.](https://sentry.io/organizations/hypro999-personal-organization/issues/1638844654/events/01ecb45633bc4f5ca940ada671124c8f/)```quote**level:** info-**timestamp:** <time:2020-04-30 06:14:13>+**timestamp:** <time:2020-04-30T06:14:13+00:00>```"""self.check_webhook("event_for_message_golang", expected_topic_name, expected_message)@@ -187,7 +187,7 @@ Traceback:**New message event:** [Test event from node.](https://sentry.io/organizations/hypro999-personal-organization/issues/1638840427/events/6886bb1fe7ce4497b7836f6083d5fd34/)```quote**level:** info-**timestamp:** <time:2020-04-30 06:09:56>+**timestamp:** <time:2020-04-30T06:09:56+00:00>```"""self.check_webhook("event_for_message_node", expected_topic_name, expected_message)@@ -197,7 +197,7 @@ Traceback:**New message event:** [A simple message-based issue.](https://sentry.io/organizations/hypro999-personal-organization/issues/1635261062/events/8da63b42375e4d3b803c377fefb062f8/)```quote**level:** info-**timestamp:** <time:2020-04-28 14:05:04>+**timestamp:** <time:2020-04-28T14:05:04+00:00>```"""self.check_webhook("event_for_message_python", expected_topic_name, expected_message)@@ -217,7 +217,7 @@ Traceback:**New issue created:** Exception: Custom exception!```quote**level:** error-**timestamp:** <time:2020-04-28 13:56:05>+**timestamp:** <time:2020-04-28T13:56:05+00:00>**assignee:** No one```"""self.check_webhook("issue_created_for_exception", expected_topic_name, expected_message)@@ -228,7 +228,7 @@ Traceback:**New issue created:** A simple message-based issue.```quote**level:** info-**timestamp:** <time:2020-04-28 14:05:04>+**timestamp:** <time:2020-04-28T14:05:04+00:00>**assignee:** No one```"""self.check_webhook("issue_created_for_message", expected_topic_name, expected_message)@@ -259,7 +259,7 @@ This is an example python exception**New message event:** [This is an example Python exception](https://sentry.io/organizations/nitk-46/issues/4218258981/events/b6eff1a49b1f4132850b1238d968da70/)```quote**level:** error-**timestamp:** <time:2023-05-31 11:06:16>+**timestamp:** <time:2023-05-31T11:06:16+00:00>```"""self.check_webhook("sample_event_through_alert", expected_topic_name, expected_message)@@ -269,7 +269,7 @@ This is an example python exception**New message event:** [This is an example Python exception](https://nitk-46.sentry.io/issues/4218258981/events/4dc4fc2858aa450eb658be9e5b8ad149/)```quote**level:** error-**timestamp:** <time:2023-07-09 20:41:24>+**timestamp:** <time:2023-07-09T20:41:24+00:00>```"""self.check_webhook("sample_event_through_plugin", expected_topic_name, expected_message)diff --git a/zerver/webhooks/sentry/view.py b/zerver/webhooks/sentry/view.pyindex 327fcd98f39..2df9c97d305 100644--- a/zerver/webhooks/sentry/view.py+++ b/zerver/webhooks/sentry/view.py@@ -3,11 +3,13 @@ from datetime import datetime, timezonefrom typing import Anyfrom urllib.parse import urljoin+import dateutil.parserfrom django.http import HttpRequest, HttpResponsefrom zerver.decorator import webhook_viewfrom zerver.lib.exceptions import UnsupportedWebhookEventTypeErrorfrom zerver.lib.response import json_success+from zerver.lib.timestamp import datetime_to_global_timefrom zerver.lib.typed_endpoint import JsonBodyPayload, typed_endpointfrom zerver.lib.webhooks.common import check_send_webhook_messagefrom zerver.models import UserProfile@@ -24,7 +26,7 @@ MESSAGE_EVENT_TEMPLATE = """**New message event:** [{title}]({web_link})```quote**level:** {level}-**timestamp:** <time:{datetime}>+**timestamp:** {global_time}```"""@@ -32,7 +34,7 @@ EXCEPTION_EVENT_TEMPLATE = """**New exception:** [{title}]({web_link})```quote**level:** {level}-**timestamp:** <time:{datetime}>+**timestamp:** {global_time}**filename:** {filename}```"""@@ -53,7 +55,7 @@ ISSUE_CREATED_MESSAGE_TEMPLATE = """**New issue created:** {title}```quote**level:** {level}-**timestamp:** <time:{datetime}>+**timestamp:** {global_time}**assignee:** {assignee}```"""@@ -81,6 +83,11 @@ syntax_highlight_as_map = {}+def get_global_time(dt_str: str) -> str:+ dt = dateutil.parser.parse(dt_str)+ return datetime_to_global_time(dt)++def is_sample_event(event: dict[str, Any]) -> bool:# This is just a heuristic to detect the sample event, this should# not be used for making important behavior decisions.@@ -120,7 +127,7 @@ def handle_event_payload(event: dict[str, Any]) -> tuple[str, str]:"title": topic_name,"level": event["level"],"web_link": event["web_url"],- "datetime": event["datetime"].split(".")[0].replace("T", " "),+ "global_time": get_global_time(event["datetime"]),}if "exception" in event:@@ -186,7 +193,7 @@ def handle_issue_payload() -> tuple[str, str]:"""Handle either an issue type event."""topic_name = issue["title"]- datetime = issue["lastSeen"].split(".")[0].replace("T", " ")+ global_time = get_global_time(issue["lastSeen"])if issue["assignedTo"]:if issue["assignedTo"]["type"] == "team":@@ -200,7 +207,7 @@ def handle_issue_payload(context = {"title": topic_name,"level": issue["level"],- "datetime": datetime,+ "global_time": global_time,"assignee": assignee,}body = ISSUE_CREATED_MESSAGE_TEMPLATE.format(**context)