2022-04-02 11:18:21 +02:00
|
|
|
from typing import Dict, List
|
2021-04-13 02:22:41 +02:00
|
|
|
|
|
|
|
import dateutil.parser
|
2022-04-02 11:18:21 +02:00
|
|
|
from django.core.exceptions import ValidationError
|
2021-04-13 02:22:41 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2022-01-13 22:59:43 +01:00
|
|
|
from django.utils.translation import gettext as _
|
2021-04-13 02:22:41 +02:00
|
|
|
|
2022-04-14 23:50:10 +02:00
|
|
|
from zerver.actions.message_send import send_rate_limited_pm_notification_to_bot_owner
|
2021-07-16 22:11:10 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2021-06-30 18:35:50 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
2021-07-16 22:11:10 +02:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2021-06-30 18:35:50 +02:00
|
|
|
from zerver.lib.response import json_success
|
2021-04-13 02:22:41 +02:00
|
|
|
from zerver.lib.send_email import FromAddress
|
2022-04-02 11:18:21 +02:00
|
|
|
from zerver.lib.validator import WildValue, check_string, to_wild_value
|
2021-04-13 02:22:41 +02:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message, get_setup_webhook_message
|
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
|
|
|
MISCONFIGURED_PAYLOAD_ERROR_MESSAGE = """
|
|
|
|
Hi there! Your bot {bot_name} just received a Freshstatus payload that is missing
|
|
|
|
some data that Zulip requires. This usually indicates a configuration issue
|
|
|
|
in your Freshstatus webhook settings. Please make sure that you provide all the required parameters
|
|
|
|
when configuring the Freshstatus webhook. Contact {support_email} if you
|
|
|
|
need further help!
|
|
|
|
"""
|
|
|
|
|
|
|
|
FRESHSTATUS_TOPIC_TEMPLATE = "{title}".strip()
|
|
|
|
FRESHSTATUS_TOPIC_TEMPLATE_TEST = "Freshstatus"
|
|
|
|
|
|
|
|
FRESHSTATUS_MESSAGE_TEMPLATE_INCIDENT_OPEN = """
|
|
|
|
The following incident has been opened: **{title}**
|
|
|
|
**Description:** {description}
|
|
|
|
**Start Time:** {start_time}
|
|
|
|
**Affected Services:**
|
|
|
|
{affected_services}
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
FRESHSTATUS_MESSAGE_TEMPLATE_INCIDENT_CLOSED = """
|
|
|
|
The following incident has been closed: **{title}**
|
|
|
|
**Note:** {message}
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
FRESHSTATUS_MESSAGE_TEMPLATE_INCIDENT_NOTE_CREATED = """
|
|
|
|
The following note has been added to the incident: **{title}**
|
|
|
|
**Note:** {message}
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
FRESHSTATUS_MESSAGE_TEMPLATE_SCHEDULED_MAINTENANCE_PLANNED = """
|
|
|
|
The following scheduled maintenance has been opened: **{title}**
|
|
|
|
**Description:** {description}
|
|
|
|
**Scheduled Start Time:** {scheduled_start_time}
|
|
|
|
**Scheduled End Time:** {scheduled_end_time}
|
|
|
|
**Affected Services:**
|
|
|
|
{affected_services}
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
FRESHSTATUS_MESSAGE_TEMPLATE_SCHEDULED_MAINTENANCE_CLOSED = """
|
|
|
|
The following scheduled maintenance has been closed: **{title}**
|
|
|
|
**Note:** {message}
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
FRESHSTATUS_MESSAGE_TEMPLATE_SCHEDULED_MAINTENANCE_NOTE_CREATED = """
|
|
|
|
The following note has been added to the scheduled maintenance: **{title}**
|
|
|
|
**Note:** {message}
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
FRESHSTATUS_MESSAGE_EVENT_NOT_SUPPORTED = "The event ({event_type}) is not supported yet."
|
|
|
|
|
|
|
|
FRESHSTATUS_SERVICES_ROW_TEMPLATE = "* {service_name}\n"
|
|
|
|
FRESHSTATUS_SERVICES_OTHERS_ROW_TEMPLATE = "[and {services_number} more service(s)]"
|
|
|
|
FRESHSTATUS_SERVICES_LIMIT = 5
|
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
ALL_EVENT_TYPES = [
|
|
|
|
"MAINTENANCE_NOTE_CREATE",
|
|
|
|
"INCIDENT_NOTE_CREATE",
|
|
|
|
"INCIDENT_OPEN",
|
|
|
|
"MAINTENANCE_PLANNED",
|
|
|
|
"INCIDENT_REOPEN",
|
|
|
|
]
|
2021-04-13 02:22:41 +02:00
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
|
|
|
|
@webhook_view("Freshstatus", all_event_types=ALL_EVENT_TYPES)
|
2021-04-13 02:22:41 +02:00
|
|
|
@has_request_variables
|
|
|
|
def api_freshstatus_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2022-04-02 11:18:21 +02:00
|
|
|
payload: WildValue = REQ(argument_type="body", converter=to_wild_value),
|
2021-04-13 02:22:41 +02:00
|
|
|
) -> HttpResponse:
|
|
|
|
try:
|
|
|
|
body = get_body_for_http_request(payload)
|
2023-07-12 13:37:08 +02:00
|
|
|
topic = get_topic_for_http_request(payload)
|
2022-04-02 11:18:21 +02:00
|
|
|
except ValidationError:
|
2021-04-13 02:22:41 +02:00
|
|
|
message = MISCONFIGURED_PAYLOAD_ERROR_MESSAGE.format(
|
|
|
|
bot_name=user_profile.full_name,
|
|
|
|
support_email=FromAddress.SUPPORT,
|
|
|
|
).strip()
|
|
|
|
send_rate_limited_pm_notification_to_bot_owner(user_profile, user_profile.realm, message)
|
|
|
|
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("Invalid payload"))
|
2021-04-13 02:22:41 +02:00
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
check_send_webhook_message(
|
2023-07-12 13:37:08 +02:00
|
|
|
request, user_profile, topic, body, payload["event_data"]["event_type"].tame(check_string)
|
2021-07-16 11:40:46 +02:00
|
|
|
)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2021-04-13 02:22:41 +02:00
|
|
|
|
|
|
|
|
2022-04-02 11:18:21 +02:00
|
|
|
def get_services_content(services_data: List[Dict[str, str]]) -> str:
|
2021-04-13 02:22:41 +02:00
|
|
|
services_content = ""
|
|
|
|
for service in services_data[:FRESHSTATUS_SERVICES_LIMIT]:
|
|
|
|
services_content += FRESHSTATUS_SERVICES_ROW_TEMPLATE.format(
|
|
|
|
service_name=service.get("service_name")
|
|
|
|
)
|
|
|
|
|
|
|
|
if len(services_data) > FRESHSTATUS_SERVICES_LIMIT:
|
|
|
|
services_content += FRESHSTATUS_SERVICES_OTHERS_ROW_TEMPLATE.format(
|
|
|
|
services_number=len(services_data) - FRESHSTATUS_SERVICES_LIMIT,
|
|
|
|
)
|
|
|
|
return services_content.rstrip()
|
|
|
|
|
|
|
|
|
2023-07-12 13:37:08 +02:00
|
|
|
def get_topic_for_http_request(payload: WildValue) -> str:
|
2021-04-13 02:22:41 +02:00
|
|
|
event_data = payload["event_data"]
|
2022-04-02 11:18:21 +02:00
|
|
|
if (
|
|
|
|
event_data["event_type"].tame(check_string) == "INCIDENT_OPEN"
|
|
|
|
and payload["id"].tame(check_string) == "1"
|
|
|
|
):
|
2021-04-13 02:22:41 +02:00
|
|
|
return FRESHSTATUS_TOPIC_TEMPLATE_TEST
|
|
|
|
else:
|
2022-04-02 11:18:21 +02:00
|
|
|
return FRESHSTATUS_TOPIC_TEMPLATE.format(title=payload["title"].tame(check_string))
|
2021-04-13 02:22:41 +02:00
|
|
|
|
|
|
|
|
2022-04-02 11:18:21 +02:00
|
|
|
def get_body_for_maintenance_planned_event(payload: WildValue) -> str:
|
2023-07-31 22:52:35 +02:00
|
|
|
services_data = [
|
|
|
|
{"service_name": service}
|
|
|
|
for service in payload["affected_services"].tame(check_string).split(",")
|
|
|
|
]
|
2021-04-13 02:22:41 +02:00
|
|
|
data = {
|
2022-04-02 11:18:21 +02:00
|
|
|
"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"),
|
2021-04-13 02:22:41 +02:00
|
|
|
"affected_services": get_services_content(services_data),
|
|
|
|
}
|
|
|
|
return FRESHSTATUS_MESSAGE_TEMPLATE_SCHEDULED_MAINTENANCE_PLANNED.format(**data)
|
|
|
|
|
|
|
|
|
2022-04-02 11:18:21 +02:00
|
|
|
def get_body_for_incident_open_event(payload: WildValue) -> str:
|
2023-07-31 22:52:35 +02:00
|
|
|
services_data = [
|
|
|
|
{"service_name": service}
|
|
|
|
for service in payload["affected_services"].tame(check_string).split(",")
|
|
|
|
]
|
2021-04-13 02:22:41 +02:00
|
|
|
data = {
|
2022-04-02 11:18:21 +02:00
|
|
|
"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"
|
|
|
|
),
|
2021-04-13 02:22:41 +02:00
|
|
|
"affected_services": get_services_content(services_data),
|
|
|
|
}
|
|
|
|
return FRESHSTATUS_MESSAGE_TEMPLATE_INCIDENT_OPEN.format(**data)
|
|
|
|
|
|
|
|
|
2022-04-02 11:18:21 +02:00
|
|
|
def get_body_for_http_request(payload: WildValue) -> str:
|
2021-04-13 02:22:41 +02:00
|
|
|
event_data = payload["event_data"]
|
2022-04-02 11:18:21 +02:00
|
|
|
event_type = event_data["event_type"].tame(check_string)
|
|
|
|
if event_type == "INCIDENT_OPEN" and payload["id"].tame(check_string) == "1":
|
2021-04-13 02:22:41 +02:00
|
|
|
return get_setup_webhook_message("Freshstatus")
|
2022-04-02 11:18:21 +02:00
|
|
|
elif event_type == "INCIDENT_OPEN":
|
2021-04-13 02:22:41 +02:00
|
|
|
return get_body_for_incident_open_event(payload)
|
2022-04-02 11:18:21 +02:00
|
|
|
elif event_type == "INCIDENT_NOTE_CREATE":
|
|
|
|
incident_status = payload["incident_status"].tame(check_string)
|
|
|
|
title = payload["title"].tame(check_string)
|
|
|
|
message = payload["message"].tame(check_string)
|
|
|
|
if incident_status == "Closed":
|
|
|
|
return FRESHSTATUS_MESSAGE_TEMPLATE_INCIDENT_CLOSED.format(title=title, message=message)
|
|
|
|
elif incident_status == "Open":
|
|
|
|
return FRESHSTATUS_MESSAGE_TEMPLATE_INCIDENT_NOTE_CREATED.format(
|
|
|
|
title=title, message=message
|
|
|
|
)
|
|
|
|
elif event_type == "MAINTENANCE_PLANNED":
|
2021-04-13 02:22:41 +02:00
|
|
|
return get_body_for_maintenance_planned_event(payload)
|
2022-04-02 11:18:21 +02:00
|
|
|
elif event_type == "MAINTENANCE_NOTE_CREATE":
|
|
|
|
title = payload["title"].tame(check_string)
|
|
|
|
message = payload["message"].tame(check_string)
|
|
|
|
if payload["incident_status"].tame(check_string) == "Closed":
|
|
|
|
return FRESHSTATUS_MESSAGE_TEMPLATE_SCHEDULED_MAINTENANCE_CLOSED.format(
|
|
|
|
title=title, message=message
|
|
|
|
)
|
2021-04-13 02:22:41 +02:00
|
|
|
else:
|
2022-04-02 11:18:21 +02:00
|
|
|
return FRESHSTATUS_MESSAGE_TEMPLATE_SCHEDULED_MAINTENANCE_NOTE_CREATED.format(
|
|
|
|
title=title, message=message
|
|
|
|
)
|
2021-04-13 02:22:41 +02:00
|
|
|
|
2022-04-02 11:18:21 +02:00
|
|
|
return FRESHSTATUS_MESSAGE_EVENT_NOT_SUPPORTED.format(event_type=event_type)
|