From f9d033a03255947e82f10c14c4a7e244bfdc5ac7 Mon Sep 17 00:00:00 2001 From: Tal Borenstein Date: Tue, 12 Nov 2024 14:04:33 +0200 Subject: [PATCH] fix(pagerduty): backwards compatibility --- .../pagerduty_provider/pagerduty_provider.py | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/keep/providers/pagerduty_provider/pagerduty_provider.py b/keep/providers/pagerduty_provider/pagerduty_provider.py index a149320d2..30eab1190 100644 --- a/keep/providers/pagerduty_provider/pagerduty_provider.py +++ b/keep/providers/pagerduty_provider/pagerduty_provider.py @@ -504,6 +504,11 @@ def _query(self, incident_id: str = None): def _format_alert( event: dict, provider_instance: "BaseProvider" = None ) -> AlertDto: + # If somebody connected the provider before we refactored it + old_format_event = event.get("event", {}) + if old_format_event is not None and isinstance(old_format_event, dict): + return PagerdutyProvider._format_alert_old(old_format_event) + status = PagerdutyProvider.ALERT_STATUS_MAP.get(event.get("status", "firing")) severity = PagerdutyProvider.ALERT_SEVERITIES_MAP.get( event.get("severity", "info") @@ -526,6 +531,59 @@ def _format_alert( fingerprint=fingerprint, ) + def _format_alert_old(event: dict) -> AlertDto: + actual_event = event.get("event", {}) + data = actual_event.get("data", {}) + url = data.pop("self", data.pop("html_url", None)) + # format status and severity to Keep format + status = PagerdutyProvider.STATUS_MAP.get(data.pop("status", "firing")) + priority_summary = (data.get("priority", {}) or {}).get("summary") + priority = PagerdutyProvider.SEVERITIES_MAP.get(priority_summary, "P4") + last_received = data.pop( + "created_at", datetime.datetime.now(tz=datetime.timezone.utc).isoformat() + ) + name = data.pop("title") + service = data.pop("service", {}).get("summary", "unknown") + environment = next( + iter( + [ + x + for x in data.pop("custom_fields", []) + if x.get("name") == "environment" + ] + ), + {}, + ).get("value", "unknown") + + last_status_change_by = data.get("last_status_change_by", {}).get("summary") + acknowledgers = [x.get("summary") for x in data.get("acknowledgers", [])] + conference_bridge = data.get("conference_bridge", {}) + if isinstance(conference_bridge, dict): + conference_bridge = conference_bridge.get("summary") + urgency = data.get("urgency") + + # Additional metadata + metadata = { + "urgency": urgency, + "acknowledgers": acknowledgers, + "last_updated_by": last_status_change_by, + "conference_bridge": conference_bridge, + "impacted_services": service, + } + + return AlertDto( + **data, + url=url, + status=status, + lastReceived=last_received, + name=name, + severity=priority, + environment=environment, + source=["pagerduty"], + service=service, + labels=metadata, + ) + def __get_all_incidents_or_alerts(self, incident_id: str = None): self.logger.info( "Getting incidents or alerts", extra={"incident_id": incident_id}