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.

webhooks/newrelic: Remove datetime error handling.

Niloth Pc7f4538090a6
diff --git a/zerver/lib/webhooks/common.py b/zerver/lib/webhooks/common.py
index f3dc5b2af96..aa528f3e43f 100644
--- a/zerver/lib/webhooks/common.py
+++ b/zerver/lib/webhooks/common.py
@@ -4,7 +4,6 @@ import hmac
import importlib
from collections.abc import Callable
from dataclasses import dataclass
-from datetime import datetime
from enum import Enum
from typing import Annotated, Any, TypeAlias
from urllib.parse import unquote
@@ -30,7 +29,6 @@ from zerver.lib.exceptions import (
)
from zerver.lib.request import RequestNotes
from zerver.lib.send_email import FromAddress
-from zerver.lib.timestamp import timestamp_to_datetime
from zerver.lib.typed_endpoint import ApiParamConfig, typed_endpoint
from zerver.lib.validator import check_bool, check_string
from zerver.models import UserProfile
@@ -293,21 +291,6 @@ def get_http_headers_from_filename(http_header_key: str) -> Callable[[str], dict
return fixture_to_headers
-def unix_milliseconds_to_timestamp(milliseconds: Any, webhook: str) -> datetime:
- """If an integration requires time input in unix milliseconds, this helper
- checks to ensure correct type and will catch any errors related to type or
- value and raise a JsonableError.
- Returns a datetime representing the time."""
- try:
- # timestamps are in milliseconds so divide by 1000
- seconds = milliseconds / 1000
- return timestamp_to_datetime(seconds)
- except (ValueError, TypeError):
- raise JsonableError(
- _("The {webhook} webhook expects time in milliseconds.").format(webhook=webhook)
- )
-
-
def parse_multipart_string(body: str) -> dict[str, str]:
"""
Converts multipart/form-data string (fixture) to dict
diff --git a/zerver/webhooks/newrelic/fixtures/incident_malformed_timestamp.json b/zerver/webhooks/newrelic/fixtures/incident_malformed_timestamp.json
deleted file mode 100644
index bc66740d8ea..00000000000
--- a/zerver/webhooks/newrelic/fixtures/incident_malformed_timestamp.json
+++ /dev/null
@@ -1,12 +0,0 @@
-{
- "id": "95a9344a-2590-48ce-8d83-07e28b6d22c6",
- "issueUrl": "https://radar-api.service.newrelic.com/accounts/1/issues/0ea2df1c-adab-45d2-aae0-042b609d2322?notifier=SLACK",
- "title": "main_app-UBUNTU query result is > 2.0 for 1 minutes on 'High CPU'",
- "priority": "CRITICAL",
- "totalIncidents": 1,
- "state": "CLOSED",
- "createdAt": "1713766657asdasd383",
- "updatedAt": "1713766657383",
- "alertPolicyNames": ["Golden Signals"],
- "alertConditionNames": ["High CPU"]
-}
diff --git a/zerver/webhooks/newrelic/fixtures/incident_time_too_large.json b/zerver/webhooks/newrelic/fixtures/incident_time_too_large.json
deleted file mode 100644
index 520c820f6c0..00000000000
--- a/zerver/webhooks/newrelic/fixtures/incident_time_too_large.json
+++ /dev/null
@@ -1,17 +0,0 @@
-{
- "id": "95a9344a-2590-48ce-8d83-07e28b6d22c6",
- "issueUrl": "https://radar-api.service.newrelic.com/accounts/1/issues/0ea2df1c-adab-45d2-aae0-042b609d2322?notifier=SLACK",
- "title": "main_app-UBUNTU query result is > 2.0 for 1 minutes on 'High CPU'",
- "priority": "CRITICAL",
- "impactedEntities": ["main_app-UBUNTU"],
- "totalIncidents": 1,
- "state": "CLOSED",
- "trigger": "INCIDENT_CLOSED",
- "isCorrelated": false,
- "createdAt": 1713766590228,
- "updatedAt": "123123123129312381923",
- "sources": ["newrelic"],
- "alertPolicyNames": ["Golden Signals"],
- "alertConditionNames": ["High CPU"],
- "workflowName": "DBA Team workflow"
-}
diff --git a/zerver/webhooks/newrelic/tests.py b/zerver/webhooks/newrelic/tests.py
index 840f66b49dd..b531058673b 100644
--- a/zerver/webhooks/newrelic/tests.py
+++ b/zerver/webhooks/newrelic/tests.py
@@ -356,23 +356,3 @@ class NewRelicHookTests(WebhookTestCase):
expected_message,
content_type="application/json",
)
-
- def test_malformatted_time(self) -> None:
- with self.assertRaises(AssertionError) as e:
- self.check_webhook(
- "incident_malformed_timestamp",
- "",
- "",
- content_type="application/json",
- )
- self.assertIn("The newrelic webhook expects time in milliseconds.", e.exception.args[0])
-
- def test_time_too_large(self) -> None:
- with self.assertRaises(AssertionError) as e:
- self.check_webhook(
- "incident_time_too_large",
- "",
- "",
- content_type="application/json",
- )
- self.assertIn("The newrelic webhook expects time in milliseconds.", e.exception.args[0])
diff --git a/zerver/webhooks/newrelic/view.py b/zerver/webhooks/newrelic/view.py
index 7c0b9722e93..4a65834628b 100644
--- a/zerver/webhooks/newrelic/view.py
+++ b/zerver/webhooks/newrelic/view.py
@@ -5,6 +5,7 @@ from django.http import HttpRequest, HttpResponse
from zerver.decorator import webhook_view
from zerver.lib.response import json_success
+from zerver.lib.timestamp import timestamp_to_datetime
from zerver.lib.typed_endpoint import JsonBodyPayload, typed_endpoint
from zerver.lib.validator import (
WildValue,
@@ -16,7 +17,7 @@ from zerver.lib.validator import (
check_string_in,
check_union,
)
-from zerver.lib.webhooks.common import check_send_webhook_message, unix_milliseconds_to_timestamp
+from zerver.lib.webhooks.common import check_send_webhook_message
from zerver.models import UserProfile
MISSING_FIELDS_NOTIFICATION = """
@@ -74,12 +75,9 @@ EXPECTED_FIELDS = [
]
-def get_timestamp_string(payload: WildValue, event_type: str) -> str:
- # This function is intended to be used only for the "updatedAt"
- # and "createdAt" fields. Theoretically, neither field can be
- # None at any time.
- unix_time = payload[event_type].tame(check_union([check_int, check_string]))
- timestamp = str(unix_milliseconds_to_timestamp(unix_time, "newrelic"))
+def get_timestamp_string(payload: WildValue, field: str) -> str:
+ unix_time = payload[field].tame(check_int)
+ timestamp = timestamp_to_datetime(unix_time / 1000)
return f"<time: {timestamp} >"