2017-12-27 07:15:27 +01:00
|
|
|
# Webhooks for external integrations.
|
2020-01-14 22:06:24 +01:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2021-07-16 22:11:10 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2023-10-06 00:17:52 +02:00
|
|
|
from zerver.lib.exceptions import AnomalousWebhookPayloadError
|
2017-12-27 07:15:27 +01:00
|
|
|
from zerver.lib.response import json_success
|
2023-09-27 19:01:31 +02:00
|
|
|
from zerver.lib.typed_endpoint import JsonBodyPayload, typed_endpoint
|
2023-08-12 09:34:31 +02:00
|
|
|
from zerver.lib.validator import WildValue, check_string
|
2018-03-16 22:53:50 +01:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
2019-02-02 23:53:55 +01:00
|
|
|
from zerver.models import UserProfile
|
2017-12-27 07:15:27 +01:00
|
|
|
|
2019-04-12 05:55:01 +02:00
|
|
|
INCIDENT_TEMPLATE = """
|
|
|
|
**{name}**:
|
|
|
|
* State: **{state}**
|
|
|
|
* Description: {content}
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
COMPONENT_TEMPLATE = "**{name}** has changed status from **{old_status}** to **{new_status}**."
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
TOPIC_TEMPLATE = "{name}: {description}"
|
2017-12-27 07:15:27 +01:00
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
ALL_EVENT_TYPES = ["incident", "component"]
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-23 16:19:57 +02:00
|
|
|
def get_incident_events_body(payload: WildValue) -> str:
|
2017-12-27 07:15:27 +01:00
|
|
|
return INCIDENT_TEMPLATE.format(
|
2022-06-23 16:19:57 +02:00
|
|
|
name=payload["incident"]["name"].tame(check_string),
|
|
|
|
state=payload["incident"]["status"].tame(check_string),
|
|
|
|
content=payload["incident"]["incident_updates"][0]["body"].tame(check_string),
|
2017-12-27 07:15:27 +01:00
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-23 16:19:57 +02:00
|
|
|
def get_components_update_body(payload: WildValue) -> str:
|
2017-12-27 07:15:27 +01:00
|
|
|
return COMPONENT_TEMPLATE.format(
|
2022-06-23 16:19:57 +02:00
|
|
|
name=payload["component"]["name"].tame(check_string),
|
|
|
|
old_status=payload["component_update"]["old_status"].tame(check_string),
|
|
|
|
new_status=payload["component_update"]["new_status"].tame(check_string),
|
2017-12-27 07:15:27 +01:00
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-23 16:19:57 +02:00
|
|
|
def get_incident_topic(payload: WildValue) -> str:
|
2017-12-27 07:15:27 +01:00
|
|
|
return TOPIC_TEMPLATE.format(
|
2022-06-23 16:19:57 +02:00
|
|
|
name=payload["incident"]["name"].tame(check_string),
|
|
|
|
description=payload["page"]["status_description"].tame(check_string),
|
2017-12-27 07:15:27 +01:00
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-23 16:19:57 +02:00
|
|
|
def get_component_topic(payload: WildValue) -> str:
|
2017-12-27 07:15:27 +01:00
|
|
|
return TOPIC_TEMPLATE.format(
|
2022-06-23 16:19:57 +02:00
|
|
|
name=payload["component"]["name"].tame(check_string),
|
|
|
|
description=payload["page"]["status_description"].tame(check_string),
|
2017-12-27 07:15:27 +01:00
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
@webhook_view("Statuspage", all_event_types=ALL_EVENT_TYPES)
|
2023-08-12 09:34:31 +02:00
|
|
|
@typed_endpoint
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_statuspage_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2023-08-12 09:34:31 +02:00
|
|
|
*,
|
2023-09-27 19:01:31 +02:00
|
|
|
payload: JsonBodyPayload[WildValue],
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2021-03-30 19:45:06 +02:00
|
|
|
if "incident" in payload:
|
2021-07-16 11:40:46 +02:00
|
|
|
event = "incident"
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = get_incident_topic(payload)
|
2017-12-27 07:15:27 +01:00
|
|
|
body = get_incident_events_body(payload)
|
2021-03-30 19:45:06 +02:00
|
|
|
elif "component" in payload:
|
2021-07-16 11:40:46 +02:00
|
|
|
event = "component"
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = get_component_topic(payload)
|
2017-12-27 07:15:27 +01:00
|
|
|
body = get_components_update_body(payload)
|
2021-03-30 19:45:06 +02:00
|
|
|
else:
|
2023-10-06 00:17:52 +02:00
|
|
|
raise AnomalousWebhookPayloadError
|
2017-12-27 07:15:27 +01:00
|
|
|
|
2024-01-17 15:53:30 +01:00
|
|
|
check_send_webhook_message(request, user_profile, topic_name, body, event)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|