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/freshstatus: Use datetime_to_global_time and ISO 8601 syntax.

Niloth P877737954a62
diff --git a/zerver/webhooks/freshstatus/tests.py b/zerver/webhooks/freshstatus/tests.py
index 62c73c4eb9f..a117931cd63 100644
--- a/zerver/webhooks/freshstatus/tests.py
+++ b/zerver/webhooks/freshstatus/tests.py
@@ -17,7 +17,7 @@ class FreshstatusHookTests(WebhookTestCase):
expected_message = """
The following incident has been opened: **Degradation of Multiple Servers**
**Description:** This issue is being investigated.
-**Start Time:** <time:2021-04-12 16:29 UTC>
+**Start Time:** <time:2021-04-12T16:29:00+00:00>
**Affected Services:**
* Database Server
* Web Server
@@ -35,7 +35,7 @@ The following incident has been opened: **Degradation of Multiple Servers**
expected_message = """
The following incident has been opened: **Degradation of Multiple Servers**
**Description:** This issue is being investigated.
-**Start Time:** <time:2021-04-12 16:29 UTC>
+**Start Time:** <time:2021-04-12T16:29:00+00:00>
**Affected Services:**
* Database Server
* Web Server
@@ -58,7 +58,7 @@ The following incident has been opened: **Degradation of Multiple Servers**
expected_message = """
The following incident has been opened: **Degradation of Database Server**
**Description:** This issue is being investigated.
-**Start Time:** <time:2021-04-12 16:29 UTC>
+**Start Time:** <time:2021-04-12T16:29:00+00:00>
**Affected Services:**
* Database Server
""".strip()
@@ -96,8 +96,8 @@ The following incident has been closed: **Degradation of Database Server**
expected_message = """
The following scheduled maintenance has been opened: **Expect some services downtime due to server maintenance**
**Description:** As part of the upgrade routine, we will be carrying out server maintenance work for this Service. This work will affect the Service to be unavailable during the maintenance window. We apologize for any inconvenience this may cause. Please do not hesitate to contact our support team at [email protected] if you have any questions regarding this server upgrading exercise.
-**Scheduled Start Time:** <time:2021-04-12 17:08 UTC>
-**Scheduled End Time:** <time:2021-04-12 18:08 UTC>
+**Scheduled Start Time:** <time:2021-04-12T17:08:00+00:00>
+**Scheduled End Time:** <time:2021-04-12T18:08:00+00:00>
**Affected Services:**
* Sample Service
""".strip()
@@ -113,8 +113,8 @@ The following scheduled maintenance has been opened: **Expect some services down
expected_message = """
The following scheduled maintenance has been opened: **Expect some services downtime due to server maintenance**
**Description:** As part of the upgrade routine, we will be carrying out server maintenance work for this Service. This work will affect the Service to be unavailable during the maintenance window. We apologize for any inconvenience this may cause. Please do not hesitate to contact our support team at [email protected] if you have any questions regarding this server upgrading exercise.
-**Scheduled Start Time:** <time:2021-04-12 17:08 UTC>
-**Scheduled End Time:** <time:2021-04-12 18:08 UTC>
+**Scheduled Start Time:** <time:2021-04-12T17:08:00+00:00>
+**Scheduled End Time:** <time:2021-04-12T18:08:00+00:00>
**Affected Services:**
* Sample Service
* Sample Service 2
@@ -133,8 +133,8 @@ The following scheduled maintenance has been opened: **Expect some services down
expected_message = """
The following scheduled maintenance has been opened: **Expect some services downtime due to server maintenance**
**Description:** As part of the upgrade routine, we will be carrying out server maintenance work for this Service. This work will affect the Service to be unavailable during the maintenance window. We apologize for any inconvenience this may cause. Please do not hesitate to contact our support team at [email protected] if you have any questions regarding this server upgrading exercise.
-**Scheduled Start Time:** <time:2021-04-12 17:08 UTC>
-**Scheduled End Time:** <time:2021-04-12 18:08 UTC>
+**Scheduled Start Time:** <time:2021-04-12T17:08:00+00:00>
+**Scheduled End Time:** <time:2021-04-12T18:08:00+00:00>
**Affected Services:**
* Sample Service
* Sample Service 2
diff --git a/zerver/webhooks/freshstatus/view.py b/zerver/webhooks/freshstatus/view.py
index dedfbc14861..fe2c9b89f94 100644
--- a/zerver/webhooks/freshstatus/view.py
+++ b/zerver/webhooks/freshstatus/view.py
@@ -1,4 +1,5 @@
-import dateutil.parser
+from datetime import datetime
+
from django.core.exceptions import ValidationError
from django.http import HttpRequest, HttpResponse
from django.utils.translation import gettext as _
@@ -8,6 +9,7 @@ from zerver.decorator import webhook_view
from zerver.lib.exceptions import JsonableError
from zerver.lib.response import json_success
from zerver.lib.send_email import FromAddress
+from zerver.lib.timestamp import datetime_to_global_time
from zerver.lib.typed_endpoint import JsonBodyPayload, typed_endpoint
from zerver.lib.validator import WildValue, check_string
from zerver.lib.webhooks.common import check_send_webhook_message, get_setup_webhook_message
@@ -27,7 +29,7 @@ FRESHSTATUS_TOPIC_TEMPLATE_TEST = "Freshstatus"
FRESHSTATUS_MESSAGE_TEMPLATE_INCIDENT_OPEN = """
The following incident has been opened: **{title}**
**Description:** {description}
-**Start Time:** <time:{start_time}>
+**Start Time:** {start_time}
**Affected Services:**
{affected_services}
""".strip()
@@ -45,8 +47,8 @@ The following note has been added to the incident: **{title}**
FRESHSTATUS_MESSAGE_TEMPLATE_SCHEDULED_MAINTENANCE_PLANNED = """
The following scheduled maintenance has been opened: **{title}**
**Description:** {description}
-**Scheduled Start Time:** <time:{scheduled_start_time}>
-**Scheduled End Time:** <time:{scheduled_end_time}>
+**Scheduled Start Time:** {scheduled_start_time}
+**Scheduled End Time:** {scheduled_end_time}
**Affected Services:**
{affected_services}
""".strip()
@@ -76,6 +78,11 @@ ALL_EVENT_TYPES = [
]
+def get_global_time(dt_str: str) -> str:
+ dt = datetime.strptime(dt_str, "%Y-%m-%d %H:%M:%S%z")
+ return datetime_to_global_time(dt)
+
+
@webhook_view("Freshstatus", all_event_types=ALL_EVENT_TYPES)
@typed_endpoint
def api_freshstatus_webhook(
@@ -139,12 +146,8 @@ def get_body_for_maintenance_planned_event(payload: WildValue) -> str:
data = {
"title": payload["title"].tame(check_string),
"description": payload["description"].tame(check_string),
- "scheduled_start_time": dateutil.parser.parse(
- payload["scheduled_start_time"].tame(check_string)
- ).strftime("%Y-%m-%d %H:%M %Z"),
- "scheduled_end_time": dateutil.parser.parse(
- payload["scheduled_end_time"].tame(check_string)
- ).strftime("%Y-%m-%d %H:%M %Z"),
+ "scheduled_start_time": get_global_time(payload["scheduled_start_time"].tame(check_string)),
+ "scheduled_end_time": get_global_time(payload["scheduled_end_time"].tame(check_string)),
"affected_services": get_services_content(services_data),
}
return FRESHSTATUS_MESSAGE_TEMPLATE_SCHEDULED_MAINTENANCE_PLANNED.format(**data)
@@ -158,9 +161,7 @@ def get_body_for_incident_open_event(payload: WildValue) -> str:
data = {
"title": payload["title"].tame(check_string),
"description": payload["description"].tame(check_string),
- "start_time": dateutil.parser.parse(payload["start_time"].tame(check_string)).strftime(
- "%Y-%m-%d %H:%M %Z"
- ),
+ "start_time": get_global_time(payload["start_time"].tame(check_string)),
"affected_services": get_services_content(services_data),
}
return FRESHSTATUS_MESSAGE_TEMPLATE_INCIDENT_OPEN.format(**data)