2017-12-27 07:15:27 +01:00
|
|
|
# Webhooks for external integrations.
|
2020-01-14 22:06:24 +01:00
|
|
|
from typing import Any, Dict
|
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2020-08-20 00:32:15 +02:00
|
|
|
from zerver.decorator import REQ, has_request_variables, webhook_view
|
2021-03-30 19:45:06 +02:00
|
|
|
from zerver.lib.exceptions import UnsupportedWebhookEventType
|
2017-12-27 07:15:27 +01:00
|
|
|
from zerver.lib.response import json_success
|
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-02-12 08:19:30 +01:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_incident_events_body(payload: Dict[str, Any]) -> str:
|
2017-12-27 07:15:27 +01:00
|
|
|
return INCIDENT_TEMPLATE.format(
|
2021-02-12 08:19:30 +01:00
|
|
|
name=payload["incident"]["name"],
|
|
|
|
state=payload["incident"]["status"],
|
|
|
|
content=payload["incident"]["incident_updates"][0]["body"],
|
2017-12-27 07:15:27 +01:00
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_components_update_body(payload: Dict[str, Any]) -> str:
|
2017-12-27 07:15:27 +01:00
|
|
|
return COMPONENT_TEMPLATE.format(
|
2021-02-12 08:19:30 +01:00
|
|
|
name=payload["component"]["name"],
|
|
|
|
old_status=payload["component_update"]["old_status"],
|
|
|
|
new_status=payload["component_update"]["new_status"],
|
2017-12-27 07:15:27 +01:00
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_incident_topic(payload: Dict[str, Any]) -> str:
|
2017-12-27 07:15:27 +01:00
|
|
|
return TOPIC_TEMPLATE.format(
|
2021-02-12 08:19:30 +01:00
|
|
|
name=payload["incident"]["name"],
|
|
|
|
description=payload["page"]["status_description"],
|
2017-12-27 07:15:27 +01:00
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_component_topic(payload: Dict[str, Any]) -> str:
|
2017-12-27 07:15:27 +01:00
|
|
|
return TOPIC_TEMPLATE.format(
|
2021-02-12 08:19:30 +01:00
|
|
|
name=payload["component"]["name"],
|
|
|
|
description=payload["page"]["status_description"],
|
2017-12-27 07:15:27 +01:00
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
@webhook_view("Statuspage")
|
2017-12-27 07:15:27 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_statuspage_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2021-02-12 08:20:45 +01:00
|
|
|
payload: Dict[str, Any] = REQ(argument_type="body"),
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2017-12-27 07:15:27 +01:00
|
|
|
|
2021-03-30 19:45:06 +02:00
|
|
|
if "incident" in payload:
|
2017-12-27 07:15:27 +01:00
|
|
|
topic = get_incident_topic(payload)
|
|
|
|
body = get_incident_events_body(payload)
|
2021-03-30 19:45:06 +02:00
|
|
|
elif "component" in payload:
|
2017-12-27 07:15:27 +01:00
|
|
|
topic = get_component_topic(payload)
|
|
|
|
body = get_components_update_body(payload)
|
2021-03-30 19:45:06 +02:00
|
|
|
else:
|
|
|
|
raise UnsupportedWebhookEventType("unknown-event")
|
2017-12-27 07:15:27 +01:00
|
|
|
|
2018-03-16 22:53:50 +01:00
|
|
|
check_send_webhook_message(request, user_profile, topic, body)
|
2017-12-27 07:15:27 +01:00
|
|
|
return json_success()
|