Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
newrelic: Added owner field and cleaned up code.
Max Zawisa0e40cc72af51
diff --git a/zerver/lib/webhooks/common.py b/zerver/lib/webhooks/common.pyindex 0c4e34d9da6..585887bcdad 100644--- a/zerver/lib/webhooks/common.py+++ b/zerver/lib/webhooks/common.py@@ -14,6 +14,7 @@ from zerver.lib.actions import (from zerver.lib.exceptions import ErrorCode, JsonableError, StreamDoesNotExistErrorfrom zerver.lib.request import REQ, has_request_variablesfrom zerver.lib.send_email import FromAddress+from zerver.lib.timestamp import timestamp_to_datetimefrom zerver.models import UserProfileMISSING_EVENT_HEADER_MESSAGE = """@@ -167,8 +168,7 @@ def unix_milliseconds_to_timestamp(milliseconds: Any, webhook: str) -> datetime:Returns a datetime representing the time."""try:# timestamps are in milliseconds so divide by 1000- return datetime.fromtimestamp((milliseconds) / 1000)- except ValueError:- raise JsonableError(_("The {} webhook expects time in milleseconds.").format(webhook))- except TypeError:+ seconds = milliseconds / 1000+ return timestamp_to_datetime(seconds)+ except (ValueError, TypeError):raise JsonableError(_("The {} webhook expects time in milleseconds.").format(webhook))diff --git a/zerver/webhooks/newrelic/fixtures/incident_acknowledged.json b/zerver/webhooks/newrelic/fixtures/incident_acknowledged.jsonindex 21a0ba6ebf6..b29fae57d88 100644--- a/zerver/webhooks/newrelic/fixtures/incident_acknowledged.json+++ b/zerver/webhooks/newrelic/fixtures/incident_acknowledged.json@@ -6,5 +6,6 @@"condition_name": "Server Down","timestamp": 1605133931151,"current_state": "acknowledged",+ "owner": "Alice","incident_url": "https://alerts.newrelic.com/accounts/2941966/incidents/1234"}diff --git a/zerver/webhooks/newrelic/fixtures/incident_closed.json b/zerver/webhooks/newrelic/fixtures/incident_closed.jsonindex 30d2c0ff8e3..ac57a18c637 100644--- a/zerver/webhooks/newrelic/fixtures/incident_closed.json+++ b/zerver/webhooks/newrelic/fixtures/incident_closed.json@@ -6,5 +6,6 @@"condition_name": "Server Down","timestamp": 1605133931151,"current_state": "closed",+ "owner": "","incident_url": "https://alerts.newrelic.com/accounts/2941966/incidents/1234"}diff --git a/zerver/webhooks/newrelic/fixtures/incident_malformatted_time.json b/zerver/webhooks/newrelic/fixtures/incident_malformatted_time.jsonindex 746892df619..9daf9c276d9 100644--- a/zerver/webhooks/newrelic/fixtures/incident_malformatted_time.json+++ b/zerver/webhooks/newrelic/fixtures/incident_malformatted_time.json@@ -6,5 +6,6 @@"condition_name": "Server Down","timestamp": "1969-12-31 23:59:55","current_state": "open",+ "owner": "","incident_url": "https://alerts.newrelic.com/accounts/2941966/incidents/1234"}diff --git a/zerver/webhooks/newrelic/fixtures/incident_missing_current_state.json b/zerver/webhooks/newrelic/fixtures/incident_missing_current_state.jsonindex 545d302a84d..af8d9eba5ec 100644--- a/zerver/webhooks/newrelic/fixtures/incident_missing_current_state.json+++ b/zerver/webhooks/newrelic/fixtures/incident_missing_current_state.json@@ -5,5 +5,6 @@"policy_name": "Test policy name","condition_name": "Server Down","timestamp": 1605133931151,+ "owner": "Alice","incident_url": "https://alerts.newrelic.com/accounts/2941966/incidents/1234"}diff --git a/zerver/webhooks/newrelic/fixtures/incident_missing_timestamp.json b/zerver/webhooks/newrelic/fixtures/incident_missing_timestamp.jsonindex f9bece876c3..35cbd81af24 100644--- a/zerver/webhooks/newrelic/fixtures/incident_missing_timestamp.json+++ b/zerver/webhooks/newrelic/fixtures/incident_missing_timestamp.json@@ -5,5 +5,6 @@"policy_name": "Test policy name","condition_name": "Server Down","current_state": "open",+ "owner": "","incident_url": "https://alerts.newrelic.com/accounts/2941966/incidents/1234"}diff --git a/zerver/webhooks/newrelic/fixtures/incident_opened.json b/zerver/webhooks/newrelic/fixtures/incident_opened.jsonindex c9e32b7be6c..097fcf508a8 100644--- a/zerver/webhooks/newrelic/fixtures/incident_opened.json+++ b/zerver/webhooks/newrelic/fixtures/incident_opened.json@@ -6,5 +6,6 @@"condition_name": "Server Down","timestamp": 1605133931151,"current_state": "open",+ "owner": "","incident_url": "https://alerts.newrelic.com/accounts/2941966/incidents/1234"}diff --git a/zerver/webhooks/newrelic/fixtures/incident_state_not_recognized.json b/zerver/webhooks/newrelic/fixtures/incident_state_not_recognized.jsonindex 559a7f2ccd3..0d6674fe7dc 100644--- a/zerver/webhooks/newrelic/fixtures/incident_state_not_recognized.json+++ b/zerver/webhooks/newrelic/fixtures/incident_state_not_recognized.json@@ -6,5 +6,6 @@"condition_name": "Server Down","timestamp": 1605133931151,"current_state": "hello world",+ "owner": "Alice","incident_url": "https://alerts.newrelic.com/accounts/2941966/incidents/1234"}diff --git a/zerver/webhooks/newrelic/fixtures/incident_time_too_large.json b/zerver/webhooks/newrelic/fixtures/incident_time_too_large.jsonindex fbff28b9eeb..ea715265fb8 100644--- a/zerver/webhooks/newrelic/fixtures/incident_time_too_large.json+++ b/zerver/webhooks/newrelic/fixtures/incident_time_too_large.json@@ -6,5 +6,6 @@"condition_name": "Server Down","timestamp": 160513393115100000,"current_state": "open",+ "owner": "","incident_url": "https://alerts.newrelic.com/accounts/2941966/incidents/1234"}diff --git a/zerver/webhooks/newrelic/tests.py b/zerver/webhooks/newrelic/tests.pyindex 13290c9c8aa..9936c91d13c 100644--- a/zerver/webhooks/newrelic/tests.py+++ b/zerver/webhooks/newrelic/tests.py@@ -9,7 +9,7 @@ class NewRelicHookTests(WebhookTestCase):def test_open(self) -> None:expected_topic = "Test policy name (1234)"expected_message = """-[Incident](https://alerts.newrelic.com/accounts/2941966/incidents/1234) **opened** for condition: **Server Down** at <time:2020-11-11 22:32:11.151000>+[Incident](https://alerts.newrelic.com/accounts/2941966/incidents/1234) **opened** for condition: **Server Down** at <time:2020-11-11 22:32:11.151000+00:00>``` quoteViolation description test.```@@ -38,7 +38,7 @@ Violation description test.def test_acknowledged(self) -> None:expected_topic = "Test policy name (1234)"expected_message = """-[Incident](https://alerts.newrelic.com/accounts/2941966/incidents/1234) **acknowledged** for condition: **Server Down**+[Incident](https://alerts.newrelic.com/accounts/2941966/incidents/1234) **acknowledged** by **Alice** for condition: **Server Down**""".strip()self.check_webhook(@@ -61,7 +61,7 @@ Violation description test.def test_missing_fields(self) -> None:expected_topic = "Unknown Policy (Unknown ID)"expected_message = """-[Incident](https://alerts.newrelic.com) **opened** for condition: **Unknown condition** at <time:2020-11-11 22:32:11.151000>+[Incident](https://alerts.newrelic.com) **opened** for condition: **Unknown condition** at <time:2020-11-11 22:32:11.151000+00:00>``` quoteNo details.```diff --git a/zerver/webhooks/newrelic/view.py b/zerver/webhooks/newrelic/view.pyindex 188c9651a1b..fa1e8cb5657 100644--- a/zerver/webhooks/newrelic/view.py+++ b/zerver/webhooks/newrelic/view.py@@ -17,7 +17,7 @@ OPEN_TEMPLATE = """```""".strip()-DEFAULT_TEMPLATE = """[Incident]({incident_url}) **{status}** for condition: **{condition_name}**""".strip()+DEFAULT_TEMPLATE = """[Incident]({incident_url}) **{status}** {owner}for condition: **{condition_name}**""".strip()TOPIC_TEMPLATE = """{policy_name} ({incident_id})""".strip()@@ -36,6 +36,7 @@ def api_newrelic_webhook("incident_acknowledge_url": payload.get('incident_acknowledge_url', 'https://alerts.newrelic.com'),"status": payload.get('current_state', 'None'),"iso_timestamp": '',+ "owner": payload.get('owner', ''),}unix_time = payload.get('timestamp', None)@@ -44,6 +45,10 @@ def api_newrelic_webhook(info['iso_timestamp'] = unix_milliseconds_to_timestamp(unix_time, "newrelic")+ # Add formatting to the owner field if owner is present+ if info['owner'] != '':+ info['owner'] = 'by **{}** '.format(info['owner'])+# These are the three promised current_state valuesif 'open' in info['status']:content = OPEN_TEMPLATE.format(**info)