fix(pagerduty): apply several small PagerDuty migrator fixes (#5536)

This PR applies the diff described in
https://github.com/grafana/irm/issues/1863 to the PagerDuty migrator
tool.

Specifically, it:
- Fixes the target type check for user references in service filtering
- Ensures escalation policies are included in the PagerDuty API request
- Corrects the logging output for service migration errors
This commit is contained in:
Joey Orlando 2025-05-12 19:33:31 -04:00 committed by GitHub
parent bf7a834e17
commit 307afbe464
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 10 deletions

View file

@ -49,7 +49,7 @@ def filter_services(services: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
if service.get("escalation_policy"): if service.get("escalation_policy"):
for rule in service["escalation_policy"].get("escalation_rules", []): for rule in service["escalation_policy"].get("escalation_rules", []):
for target in rule.get("targets", []): for target in rule.get("targets", []):
if target["type"] == "user": if target["type"] == "user_reference":
service_users.add(target["id"]) service_users.add(target["id"])
if not any(user_id in service_users for user_id in PAGERDUTY_FILTER_USERS): if not any(user_id in service_users for user_id in PAGERDUTY_FILTER_USERS):
@ -163,6 +163,7 @@ def fetch_services(
if include_teams: if include_teams:
include_params.append("teams") include_params.append("teams")
include_params.append("escalation_policies")
params = {} params = {}
if include_params: if include_params:
params["include[]"] = include_params params["include[]"] = include_params
@ -570,7 +571,7 @@ def _migrate_technical_service(
if errors: if errors:
service.migration_errors = errors service.migration_errors = errors
service.preserved = False service.preserved = False
print(TAB + format_service(service, False)) print(TAB + format_service(service, True))
return None return None
if dry_run: if dry_run:
@ -589,7 +590,7 @@ def _migrate_technical_service(
except Exception as e: except Exception as e:
service.migration_errors = str(e) service.migration_errors = str(e)
service.preserved = False service.preserved = False
print(TAB + format_service(service, False)) print(TAB + format_service(service, True))
return None return None
@ -624,7 +625,7 @@ def _migrate_business_service(
if errors: if errors:
service.migration_errors = errors service.migration_errors = errors
service.preserved = False service.preserved = False
print(TAB + format_service(service, False)) print(TAB + format_service(service, True))
return None return None
if dry_run: if dry_run:
@ -643,7 +644,7 @@ def _migrate_business_service(
except Exception as e: except Exception as e:
service.migration_errors = str(e) service.migration_errors = str(e)
service.preserved = False service.preserved = False
print(TAB + format_service(service, False)) print(TAB + format_service(service, True))
return None return None

View file

@ -50,8 +50,8 @@ def sample_services():
"escalation_rules": [ "escalation_rules": [
{ {
"targets": [ "targets": [
{"type": "user", "id": "U123"}, {"type": "user_reference", "id": "U123"},
{"type": "user", "id": "U456"}, {"type": "user_reference", "id": "U456"},
] ]
} }
] ]
@ -63,7 +63,9 @@ def sample_services():
"type": "service", "type": "service",
"teams": [{"summary": "DevOps Team"}], "teams": [{"summary": "DevOps Team"}],
"escalation_policy": { "escalation_policy": {
"escalation_rules": [{"targets": [{"type": "user", "id": "U789"}]}] "escalation_rules": [
{"targets": [{"type": "user_reference", "id": "U789"}]}
]
}, },
}, },
{ {
@ -201,7 +203,8 @@ def test_fetch_services(mock_session):
# Verify API call # Verify API call
mock_session.list_all.assert_called_once_with( mock_session.list_all.assert_called_once_with(
"services", params={"include[]": ["integrations", "teams"]} "services",
params={"include[]": ["integrations", "teams", "escalation_policies"]},
) )
# Verify results # Verify results
@ -220,7 +223,9 @@ def test_fetch_services_without_includes(mock_session):
) )
# Verify API call with no includes # Verify API call with no includes
mock_session.list_all.assert_called_once_with("services", params={}) mock_session.list_all.assert_called_once_with(
"services", params={"include[]": ["escalation_policies"]}
)
# Verify results # Verify results
assert len(services) == 1 assert len(services) == 1