2022-07-22 12:05:59 +00:00
|
|
|
import json
|
2022-12-01 16:31:04 +00:00
|
|
|
from typing import Any, List, Tuple
|
|
|
|
|
|
|
|
|
|
from fastapi import Request
|
2022-07-22 12:05:59 +00:00
|
|
|
|
2022-11-30 16:05:39 +00:00
|
|
|
from ops_bot.common import COLOR_ALARM, COLOR_UNKNOWN
|
2022-12-01 16:31:04 +00:00
|
|
|
from ops_bot.config import RoutingKey
|
2022-11-30 16:05:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def urgency_color(urgency: str) -> str:
|
|
|
|
|
if urgency == "high":
|
|
|
|
|
return COLOR_ALARM
|
|
|
|
|
else:
|
|
|
|
|
return COLOR_UNKNOWN
|
2022-07-22 12:05:59 +00:00
|
|
|
|
|
|
|
|
|
2022-12-01 16:31:04 +00:00
|
|
|
async def parse_pagerduty_event(
|
|
|
|
|
route: RoutingKey,
|
|
|
|
|
payload: Any,
|
|
|
|
|
request: Request,
|
|
|
|
|
) -> List[Tuple[str, str]]:
|
2022-07-22 12:05:59 +00:00
|
|
|
"""
|
|
|
|
|
Parses a pagerduty webhook v3 event into a human readable message.
|
|
|
|
|
Returns a tuple where the first item is plain text, and the second item is matrix html formatted text
|
|
|
|
|
"""
|
|
|
|
|
event = payload["event"]
|
|
|
|
|
# evt_id = event["id"]
|
|
|
|
|
# event_type = event["event_type"]
|
|
|
|
|
# resource_type = event["resource_type"]
|
|
|
|
|
# occurred_at = event["occurred_at"]
|
|
|
|
|
data = event["data"]
|
|
|
|
|
data_type = data["type"]
|
|
|
|
|
|
|
|
|
|
if data_type == "incident":
|
|
|
|
|
url = data["html_url"]
|
|
|
|
|
status: str = data["status"]
|
|
|
|
|
title: str = data["title"]
|
|
|
|
|
service_name: str = data["service"]["summary"]
|
|
|
|
|
urgency: str = data.get("urgency", "high")
|
|
|
|
|
plain = f"{status}: on {service_name}: {title} {url}"
|
2022-07-22 14:32:49 +00:00
|
|
|
header_str = f"{status.upper()}[{urgency}]"
|
|
|
|
|
if status == "resolved":
|
|
|
|
|
color = "#33cc33" # green
|
|
|
|
|
else:
|
|
|
|
|
color = urgency_color(urgency)
|
|
|
|
|
formatted = f"<strong><font color={color}>{header_str}</font></strong> on {service_name}: [{title}]({url})"
|
2022-12-01 16:31:04 +00:00
|
|
|
return [(plain, formatted)]
|
2022-07-22 12:05:59 +00:00
|
|
|
|
|
|
|
|
payload_str = json.dumps(payload, sort_keys=True, indent=2)
|
2022-12-01 16:31:04 +00:00
|
|
|
return [
|
|
|
|
|
(
|
|
|
|
|
"unhandled",
|
|
|
|
|
f"""**unhandled pager duty event** (this may or may not be a critical problem, please look carefully)
|
2022-07-22 12:05:59 +00:00
|
|
|
<pre><code class="language-json">{payload_str}</code></pre>
|
|
|
|
|
""",
|
2022-12-01 16:31:04 +00:00
|
|
|
)
|
|
|
|
|
]
|