2022-07-27 23:33:49 +02:00
|
|
|
from email.headerregistry import Address
|
2022-07-27 03:46:55 +02:00
|
|
|
from typing import Dict, Union
|
2017-11-16 00:43:10 +01:00
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2020-08-20 00:32:15 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2020-08-19 22:26:38 +02:00
|
|
|
from zerver.lib.exceptions import UnsupportedWebhookEventType
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2017-11-16 00:43:10 +01:00
|
|
|
from zerver.lib.response import json_success
|
2022-07-27 03:46:55 +02:00
|
|
|
from zerver.lib.validator import WildValue, check_int, check_none_or, check_string, to_wild_value
|
2020-08-19 22:14:40 +02:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
2019-02-02 23:53:55 +01:00
|
|
|
from zerver.models import UserProfile
|
2016-06-06 00:33:59 +02:00
|
|
|
|
2022-07-27 03:46:55 +02:00
|
|
|
FormatDictType = Dict[str, Union[str, int]]
|
|
|
|
|
2016-03-13 15:28:44 +01:00
|
|
|
PAGER_DUTY_EVENT_NAMES = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"incident.trigger": "triggered",
|
|
|
|
"incident.acknowledge": "acknowledged",
|
|
|
|
"incident.unacknowledge": "unacknowledged",
|
|
|
|
"incident.resolve": "resolved",
|
|
|
|
"incident.assign": "assigned",
|
|
|
|
"incident.escalate": "escalated",
|
|
|
|
"incident.delegate": "delineated",
|
2016-03-13 15:28:44 +01:00
|
|
|
}
|
|
|
|
|
2019-01-12 23:26:27 +01:00
|
|
|
PAGER_DUTY_EVENT_NAMES_V2 = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"incident.trigger": "triggered",
|
|
|
|
"incident.acknowledge": "acknowledged",
|
|
|
|
"incident.resolve": "resolved",
|
|
|
|
"incident.assign": "assigned",
|
2019-01-12 23:26:27 +01:00
|
|
|
}
|
|
|
|
|
2021-06-04 17:26:13 +02:00
|
|
|
PAGER_DUTY_EVENT_NAMES_V3 = {
|
|
|
|
"incident.triggered": "triggered",
|
|
|
|
"incident.acknowledged": "acknowledged",
|
|
|
|
"incident.unacknowledged": "unacknowledged",
|
|
|
|
"incident.resolved": "resolved",
|
|
|
|
"incident.reassigned": "reassigned",
|
|
|
|
}
|
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
ALL_EVENT_TYPES = [
|
|
|
|
"resolved",
|
|
|
|
"assigned",
|
|
|
|
"unacknowledged",
|
|
|
|
"acknowledged",
|
|
|
|
"triggered",
|
|
|
|
"reassigned",
|
|
|
|
]
|
|
|
|
|
2021-06-04 16:47:08 +02:00
|
|
|
AGENT_TEMPLATE = "[{username}]({url})"
|
2019-01-13 00:41:51 +01:00
|
|
|
|
|
|
|
INCIDENT_WITH_SERVICE_AND_ASSIGNEE = (
|
2021-06-04 17:26:13 +02:00
|
|
|
"Incident [{incident_num_title}]({incident_url}) {action} by [{service_name}]"
|
2021-06-04 16:47:08 +02:00
|
|
|
"({service_url}) (assigned to {assignee_info}).\n\n{trigger_message}"
|
2019-01-13 00:41:51 +01:00
|
|
|
)
|
|
|
|
|
2021-06-04 16:47:08 +02:00
|
|
|
TRIGGER_MESSAGE = "``` quote\n{message}\n```"
|
|
|
|
|
2021-06-04 17:26:13 +02:00
|
|
|
NUM_TITLE = "{incident_title} (#{incident_num})"
|
|
|
|
|
2019-01-13 00:41:51 +01:00
|
|
|
INCIDENT_WITH_ASSIGNEE = """
|
2021-06-04 17:26:13 +02:00
|
|
|
Incident [{incident_num_title}]({incident_url}) {action} by {assignee_info}.
|
2019-01-13 00:41:51 +01:00
|
|
|
|
|
|
|
{trigger_message}
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
INCIDENT_ASSIGNED = """
|
2021-06-04 17:26:13 +02:00
|
|
|
Incident [{incident_num_title}]({incident_url}) {action} to {assignee_info}.
|
2019-01-13 00:41:51 +01:00
|
|
|
|
|
|
|
{trigger_message}
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
INCIDENT_RESOLVED_WITH_AGENT = """
|
2021-06-04 17:26:13 +02:00
|
|
|
Incident [{incident_num_title}]({incident_url}) resolved by {agent_info}.
|
2016-03-13 15:28:44 +01:00
|
|
|
|
2019-01-13 00:41:51 +01:00
|
|
|
{trigger_message}
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
INCIDENT_RESOLVED = """
|
2021-06-04 17:26:13 +02:00
|
|
|
Incident [{incident_num_title}]({incident_url}) resolved.
|
2019-01-13 00:41:51 +01:00
|
|
|
|
|
|
|
{trigger_message}
|
|
|
|
""".strip()
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-07-27 03:46:55 +02:00
|
|
|
def build_pagerduty_formatdict(message: WildValue) -> FormatDictType:
|
|
|
|
format_dict: FormatDictType = {}
|
|
|
|
format_dict["action"] = PAGER_DUTY_EVENT_NAMES[message["type"].tame(check_string)]
|
2016-03-13 15:28:44 +01:00
|
|
|
|
2022-07-27 03:46:55 +02:00
|
|
|
format_dict["incident_id"] = message["data"]["incident"]["id"].tame(check_string)
|
|
|
|
format_dict["incident_num_title"] = message["data"]["incident"]["incident_number"].tame(
|
|
|
|
check_int
|
|
|
|
)
|
|
|
|
format_dict["incident_url"] = message["data"]["incident"]["html_url"].tame(check_string)
|
2016-03-13 15:28:44 +01:00
|
|
|
|
2022-07-27 03:46:55 +02:00
|
|
|
format_dict["service_name"] = message["data"]["incident"]["service"]["name"].tame(check_string)
|
|
|
|
format_dict["service_url"] = message["data"]["incident"]["service"]["html_url"].tame(
|
|
|
|
check_string
|
|
|
|
)
|
2016-03-13 15:28:44 +01:00
|
|
|
|
2022-07-27 03:46:55 +02:00
|
|
|
if message["data"]["incident"].get("assigned_to_user"):
|
2021-02-12 08:20:45 +01:00
|
|
|
assigned_to_user = message["data"]["incident"]["assigned_to_user"]
|
2021-06-04 16:47:08 +02:00
|
|
|
format_dict["assignee_info"] = AGENT_TEMPLATE.format(
|
2022-07-27 03:46:55 +02:00
|
|
|
username=Address(addr_spec=assigned_to_user["email"].tame(check_string)).username,
|
|
|
|
url=assigned_to_user["html_url"].tame(check_string),
|
2019-01-13 00:41:51 +01:00
|
|
|
)
|
2016-03-13 15:28:44 +01:00
|
|
|
else:
|
2021-02-12 08:20:45 +01:00
|
|
|
format_dict["assignee_info"] = "nobody"
|
2016-03-13 15:28:44 +01:00
|
|
|
|
2022-07-27 03:46:55 +02:00
|
|
|
if message["data"]["incident"].get("resolved_by_user"):
|
2021-02-12 08:20:45 +01:00
|
|
|
resolved_by_user = message["data"]["incident"]["resolved_by_user"]
|
2021-06-04 16:47:08 +02:00
|
|
|
format_dict["agent_info"] = AGENT_TEMPLATE.format(
|
2022-07-27 03:46:55 +02:00
|
|
|
username=Address(addr_spec=resolved_by_user["email"].tame(check_string)).username,
|
|
|
|
url=resolved_by_user["html_url"].tame(check_string),
|
2019-01-13 00:41:51 +01:00
|
|
|
)
|
2016-03-13 15:28:44 +01:00
|
|
|
|
|
|
|
trigger_message = []
|
2022-07-27 03:46:55 +02:00
|
|
|
trigger_summary_data = message["data"]["incident"].get("trigger_summary_data")
|
|
|
|
if trigger_summary_data:
|
|
|
|
trigger_subject = trigger_summary_data.get("subject", "").tame(check_string)
|
2019-02-22 00:30:33 +01:00
|
|
|
if trigger_subject:
|
|
|
|
trigger_message.append(trigger_subject)
|
|
|
|
|
2022-07-27 03:46:55 +02:00
|
|
|
trigger_description = trigger_summary_data.get("description", "").tame(check_string)
|
2019-02-22 00:30:33 +01:00
|
|
|
if trigger_description:
|
|
|
|
trigger_message.append(trigger_description)
|
|
|
|
|
2021-06-04 16:47:08 +02:00
|
|
|
format_dict["trigger_message"] = TRIGGER_MESSAGE.format(message="\n".join(trigger_message))
|
2016-03-13 15:28:44 +01:00
|
|
|
return format_dict
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-07-27 03:46:55 +02:00
|
|
|
def build_pagerduty_formatdict_v2(message: WildValue) -> FormatDictType:
|
|
|
|
format_dict: FormatDictType = {}
|
|
|
|
format_dict["action"] = PAGER_DUTY_EVENT_NAMES_V2[message["event"].tame(check_string)]
|
2019-01-12 23:26:27 +01:00
|
|
|
|
2022-07-27 03:46:55 +02:00
|
|
|
format_dict["incident_id"] = message["incident"]["id"].tame(check_string)
|
|
|
|
format_dict["incident_num_title"] = message["incident"]["incident_number"].tame(check_int)
|
|
|
|
format_dict["incident_url"] = message["incident"]["html_url"].tame(check_string)
|
2019-01-12 23:26:27 +01:00
|
|
|
|
2022-07-27 03:46:55 +02:00
|
|
|
format_dict["service_name"] = message["incident"]["service"]["name"].tame(check_string)
|
|
|
|
format_dict["service_url"] = message["incident"]["service"]["html_url"].tame(check_string)
|
2019-01-12 23:26:27 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
assignments = message["incident"]["assignments"]
|
2019-01-12 23:26:27 +01:00
|
|
|
if assignments:
|
2021-02-12 08:20:45 +01:00
|
|
|
assignee = assignments[0]["assignee"]
|
2021-06-04 16:47:08 +02:00
|
|
|
format_dict["assignee_info"] = AGENT_TEMPLATE.format(
|
2022-07-27 03:46:55 +02:00
|
|
|
username=assignee["summary"].tame(check_string),
|
|
|
|
url=assignee["html_url"].tame(check_string),
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2019-01-12 23:26:27 +01:00
|
|
|
else:
|
2021-02-12 08:20:45 +01:00
|
|
|
format_dict["assignee_info"] = "nobody"
|
2019-01-12 23:26:27 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
last_status_change_by = message["incident"].get("last_status_change_by")
|
2019-01-12 23:26:27 +01:00
|
|
|
if last_status_change_by is not None:
|
2021-06-04 16:47:08 +02:00
|
|
|
format_dict["agent_info"] = AGENT_TEMPLATE.format(
|
2022-07-27 03:46:55 +02:00
|
|
|
username=last_status_change_by["summary"].tame(check_string),
|
|
|
|
url=last_status_change_by["html_url"].tame(check_string),
|
2019-01-13 00:41:51 +01:00
|
|
|
)
|
2019-01-12 23:26:27 +01:00
|
|
|
|
2022-07-27 03:46:55 +02:00
|
|
|
trigger_description = message["incident"].get("description").tame(check_none_or(check_string))
|
2019-01-12 23:26:27 +01:00
|
|
|
if trigger_description is not None:
|
2021-06-04 16:47:08 +02:00
|
|
|
format_dict["trigger_message"] = TRIGGER_MESSAGE.format(message=trigger_description)
|
2019-01-12 23:26:27 +01:00
|
|
|
return format_dict
|
2016-03-13 15:28:44 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-07-27 03:46:55 +02:00
|
|
|
def build_pagerduty_formatdict_v3(event: WildValue) -> FormatDictType:
|
|
|
|
format_dict: FormatDictType = {}
|
|
|
|
format_dict["action"] = PAGER_DUTY_EVENT_NAMES_V3[event["event_type"].tame(check_string)]
|
2021-06-04 17:26:13 +02:00
|
|
|
|
2022-07-27 03:46:55 +02:00
|
|
|
format_dict["incident_id"] = event["data"]["id"].tame(check_string)
|
|
|
|
format_dict["incident_url"] = event["data"]["html_url"].tame(check_string)
|
2021-06-04 17:26:13 +02:00
|
|
|
format_dict["incident_num_title"] = NUM_TITLE.format(
|
2022-07-27 03:46:55 +02:00
|
|
|
incident_num=event["data"]["number"].tame(check_int),
|
|
|
|
incident_title=event["data"]["title"].tame(check_string),
|
2021-06-04 17:26:13 +02:00
|
|
|
)
|
|
|
|
|
2022-07-27 03:46:55 +02:00
|
|
|
format_dict["service_name"] = event["data"]["service"]["summary"].tame(check_string)
|
|
|
|
format_dict["service_url"] = event["data"]["service"]["html_url"].tame(check_string)
|
2021-06-04 17:26:13 +02:00
|
|
|
|
|
|
|
assignees = event["data"]["assignees"]
|
|
|
|
if assignees:
|
|
|
|
assignee = assignees[0]
|
|
|
|
format_dict["assignee_info"] = AGENT_TEMPLATE.format(
|
2022-07-27 03:46:55 +02:00
|
|
|
username=assignee["summary"].tame(check_string),
|
|
|
|
url=assignee["html_url"].tame(check_string),
|
2021-06-04 17:26:13 +02:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
format_dict["assignee_info"] = "nobody"
|
|
|
|
|
|
|
|
agent = event.get("agent")
|
|
|
|
if agent is not None:
|
|
|
|
format_dict["agent_info"] = AGENT_TEMPLATE.format(
|
2022-07-27 03:46:55 +02:00
|
|
|
username=agent["summary"].tame(check_string),
|
|
|
|
url=agent["html_url"].tame(check_string),
|
2021-06-04 17:26:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# V3 doesn't have trigger_message
|
|
|
|
format_dict["trigger_message"] = ""
|
|
|
|
|
|
|
|
return format_dict
|
|
|
|
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
def send_formated_pagerduty(
|
2022-07-27 03:46:55 +02:00
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
|
|
|
message_type: str,
|
|
|
|
format_dict: FormatDictType,
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> None:
|
2021-06-04 17:26:13 +02:00
|
|
|
if message_type in (
|
|
|
|
"incident.trigger",
|
|
|
|
"incident.triggered",
|
|
|
|
"incident.unacknowledge",
|
|
|
|
"incident.unacknowledged",
|
|
|
|
):
|
2019-01-13 00:41:51 +01:00
|
|
|
template = INCIDENT_WITH_SERVICE_AND_ASSIGNEE
|
2021-06-04 17:26:13 +02:00
|
|
|
elif (
|
|
|
|
message_type == "incident.resolve" or message_type == "incident.resolved"
|
|
|
|
) and format_dict.get("agent_info") is not None:
|
2019-01-13 00:41:51 +01:00
|
|
|
template = INCIDENT_RESOLVED_WITH_AGENT
|
2021-06-04 17:26:13 +02:00
|
|
|
elif (
|
|
|
|
message_type == "incident.resolve" or message_type == "incident.resolved"
|
|
|
|
) and format_dict.get("agent_info") is None:
|
2019-01-13 00:41:51 +01:00
|
|
|
template = INCIDENT_RESOLVED
|
2021-06-04 17:26:13 +02:00
|
|
|
elif message_type == "incident.assign" or message_type == "incident.reassigned":
|
2019-01-13 00:41:51 +01:00
|
|
|
template = INCIDENT_ASSIGNED
|
2016-03-13 15:28:44 +01:00
|
|
|
else:
|
2019-01-13 00:41:51 +01:00
|
|
|
template = INCIDENT_WITH_ASSIGNEE
|
2016-03-13 15:28:44 +01:00
|
|
|
|
2021-06-04 17:26:13 +02:00
|
|
|
subject = "Incident {incident_num_title}".format(**format_dict)
|
2016-03-13 15:28:44 +01:00
|
|
|
body = template.format(**format_dict)
|
2022-07-27 03:46:55 +02:00
|
|
|
assert isinstance(format_dict["action"], str)
|
2021-07-16 11:40:46 +02:00
|
|
|
check_send_webhook_message(request, user_profile, subject, body, format_dict["action"])
|
2016-03-13 15:28:44 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
@webhook_view("PagerDuty", all_event_types=ALL_EVENT_TYPES)
|
2016-03-13 15:28:44 +01:00
|
|
|
@has_request_variables
|
2018-03-16 22:53:50 +01:00
|
|
|
def api_pagerduty_webhook(
|
2021-02-12 08:19:30 +01:00
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2022-07-27 03:46:55 +02:00
|
|
|
payload: WildValue = REQ(argument_type="body", converter=to_wild_value),
|
2018-03-16 22:53:50 +01:00
|
|
|
) -> HttpResponse:
|
2021-06-04 17:26:13 +02:00
|
|
|
messages = payload.get("messages")
|
2022-07-27 03:46:55 +02:00
|
|
|
if messages:
|
2021-06-04 17:26:13 +02:00
|
|
|
for message in messages:
|
2022-07-27 03:46:55 +02:00
|
|
|
message_type = message.get("type").tame(check_none_or(check_string))
|
2019-01-12 23:26:27 +01:00
|
|
|
|
2021-06-04 17:26:13 +02:00
|
|
|
# If the message has no "type" key, then this payload came from a
|
|
|
|
# Pagerduty Webhook V2.
|
|
|
|
if message_type is None:
|
|
|
|
break
|
2016-03-13 15:28:44 +01:00
|
|
|
|
2021-06-04 17:26:13 +02:00
|
|
|
if message_type not in PAGER_DUTY_EVENT_NAMES:
|
|
|
|
raise UnsupportedWebhookEventType(message_type)
|
2019-01-13 00:55:04 +01:00
|
|
|
|
2021-06-04 17:26:13 +02:00
|
|
|
format_dict = build_pagerduty_formatdict(message)
|
|
|
|
send_formated_pagerduty(request, user_profile, message_type, format_dict)
|
2016-03-13 15:28:44 +01:00
|
|
|
|
2021-06-04 17:26:13 +02:00
|
|
|
for message in messages:
|
2022-07-27 03:46:55 +02:00
|
|
|
message_event = message.get("event").tame(check_none_or(check_string))
|
2019-01-12 23:26:27 +01:00
|
|
|
|
2021-06-04 17:26:13 +02:00
|
|
|
# If the message has no "event" key, then this payload came from a
|
|
|
|
# Pagerduty Webhook V1.
|
2022-07-27 03:46:55 +02:00
|
|
|
if message_event is None:
|
2021-06-04 17:26:13 +02:00
|
|
|
break
|
|
|
|
|
2022-07-27 03:46:55 +02:00
|
|
|
if message_event not in PAGER_DUTY_EVENT_NAMES_V2:
|
|
|
|
raise UnsupportedWebhookEventType(message_event)
|
2021-06-04 17:26:13 +02:00
|
|
|
|
|
|
|
format_dict = build_pagerduty_formatdict_v2(message)
|
2022-07-27 03:46:55 +02:00
|
|
|
send_formated_pagerduty(request, user_profile, message_event, format_dict)
|
2021-06-04 17:26:13 +02:00
|
|
|
else:
|
|
|
|
if "event" in payload:
|
|
|
|
# V3 has no "messages" field, and it has key "event" instead
|
|
|
|
event = payload["event"]
|
2022-07-27 03:46:55 +02:00
|
|
|
event_type = event.get("event_type").tame(check_none_or(check_string))
|
2019-01-12 23:26:27 +01:00
|
|
|
|
2021-06-04 17:26:13 +02:00
|
|
|
if event_type not in PAGER_DUTY_EVENT_NAMES_V3:
|
|
|
|
raise UnsupportedWebhookEventType(event_type)
|
2019-01-12 23:26:27 +01:00
|
|
|
|
2021-06-04 17:26:13 +02:00
|
|
|
format_dict = build_pagerduty_formatdict_v3(event)
|
|
|
|
send_formated_pagerduty(request, user_profile, event_type, format_dict)
|
2019-01-12 23:26:27 +01:00
|
|
|
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|