Skip to content

Commit

Permalink
fix: pagerduty_plugin - Updates method for accessing current on-call …
Browse files Browse the repository at this point in the history
…email (#4645)
  • Loading branch information
Meandmybadself authored Apr 22, 2024
1 parent e0953f2 commit a862b4f
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions src/dispatch/plugins/dispatch_pagerduty/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,30 +86,32 @@ def get_oncall_email(client: APISession, service_id: str) -> str:
escalation_policy = get_escalation_policy(
client=client, escalation_policy_id=escalation_policy_id
)
filter_name = (
f"{escalation_policy['escalation_rules'][0]['targets'][0]['type'].split('_')[0]}_ids[]"
)
filter_value = escalation_policy["escalation_rules"][0]["targets"][0]["id"]

oncalls = list(
client.iter_all(
"oncalls", # method
{
filter_name: [filter_value],
"escalation_policy_ids[]": [escalation_policy_id],
}, # params
)
)

if oncalls:
user_id = list(oncalls)[0]["user"]["id"]
else:
raise Exception(
f"No users could be found for this pagerduty escalation policy ({escalation_policy_id}). Is there a schedule associated?"
)
user = get_user(client=client, user_id=user_id)
# Iterate over all escalation rules and targets to find the oncall user
for rule in escalation_policy["escalation_rules"]:
for target in rule["targets"]:
filter_name = f"{target['type'].split('_')[0]}_ids[]"
filter_value = target["id"]

oncalls = list(
client.iter_all(
"oncalls", # method
{
filter_name: [filter_value],
"escalation_policy_ids[]": [escalation_policy_id],
}, # params
)
)

if oncalls:
user_id = list(oncalls)[0]["user"]["id"]
user = get_user(client=client, user_id=user_id)
return user["email"]

return user["email"]
# If we reach this point, we couldn't find the oncall user
raise Exception(
f"No users could be found for this pagerduty escalation policy ({escalation_policy_id}). Is there a schedule associated???"
)


def page_oncall(
Expand Down

0 comments on commit a862b4f

Please sign in to comment.