2017-12-27 07:15:27 +01:00
|
|
|
# Webhooks for external integrations.
|
|
|
|
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
|
2017-12-27 07:15:27 +01:00
|
|
|
from zerver.decorator import REQ, has_request_variables, api_key_only_webhook_view
|
2019-02-02 23:53:55 +01:00
|
|
|
from zerver.models import UserProfile
|
2017-12-27 07:15:27 +01:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2018-05-10 19:34:01 +02:00
|
|
|
from typing import Dict, Any
|
2017-12-27 07:15:27 +01:00
|
|
|
|
|
|
|
INCIDENT_TEMPLATE = u'**{name}** \n * State: **{state}** \n * Description: {content}'
|
|
|
|
COMPONENT_TEMPLATE = u'**{name}** has changed status from **{old_status}** to **{new_status}**'
|
|
|
|
TOPIC_TEMPLATE = u'{name}: {description}'
|
|
|
|
|
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(
|
|
|
|
name = payload["incident"]["name"],
|
|
|
|
state = payload["incident"]["status"],
|
|
|
|
content = payload["incident"]["incident_updates"][0]["body"],
|
|
|
|
)
|
|
|
|
|
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(
|
|
|
|
name = payload["component"]["name"],
|
|
|
|
old_status = payload["component_update"]["old_status"],
|
|
|
|
new_status = payload["component_update"]["new_status"],
|
|
|
|
)
|
|
|
|
|
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(
|
|
|
|
name = payload["incident"]["name"],
|
|
|
|
description = payload["page"]["status_description"],
|
|
|
|
)
|
|
|
|
|
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(
|
|
|
|
name = payload["component"]["name"],
|
|
|
|
description = payload["page"]["status_description"],
|
|
|
|
)
|
|
|
|
|
|
|
|
@api_key_only_webhook_view('Statuspage')
|
|
|
|
@has_request_variables
|
|
|
|
def api_statuspage_webhook(request: HttpRequest, user_profile: UserProfile,
|
2018-03-16 22:53:50 +01:00
|
|
|
payload: Dict[str, Any]=REQ(argument_type='body')) -> HttpResponse:
|
2017-12-27 07:15:27 +01:00
|
|
|
|
|
|
|
status = payload["page"]["status_indicator"]
|
|
|
|
|
|
|
|
if status == "none":
|
|
|
|
topic = get_incident_topic(payload)
|
|
|
|
body = get_incident_events_body(payload)
|
|
|
|
else:
|
|
|
|
topic = get_component_topic(payload)
|
|
|
|
body = get_components_update_body(payload)
|
|
|
|
|
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()
|