41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
import json
|
|
from typing import Any, Tuple
|
|
|
|
|
|
def urgency_color(urgency: str) -> str:
|
|
if urgency == "high":
|
|
return "#dc3545"
|
|
else:
|
|
return "#17a2b8"
|
|
|
|
|
|
def parse_pagerduty_event(payload: Any) -> Tuple[str, str]:
|
|
"""
|
|
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}"
|
|
formatted = f"<strong><font color={urgency_color(urgency)}>{status.upper()}</font></strong> on {service_name}: [{title}]({url})"
|
|
return plain, formatted
|
|
|
|
payload_str = json.dumps(payload, sort_keys=True, indent=2)
|
|
return (
|
|
"unhandled",
|
|
f"""**unhandled pager duty event**
|
|
<pre><code class="language-json">{payload_str}</code></pre>
|
|
""",
|
|
)
|