Skip to content

Conversation

@mifu67
Copy link
Contributor

@mifu67 mifu67 commented Oct 24, 2025

If there is no org member associated with this user and organization, then we should exit gracefully.

@mifu67 mifu67 requested review from a team as code owners October 24, 2025 22:31
@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Oct 24, 2025
Comment on lines +49 to +56
try:
return OrganizationMember.objects.get(
user_id=int(target_identifier),
organization=dcga.condition_group.organization,
)
except OrganizationMember.DoesNotExist:
# user is no longer a member of the organization
pass
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: human_desc() receives None for target from MetricAlertRegistryHandler.target() and attempts to access attributes, causing an AttributeError.
Severity: CRITICAL | Confidence: 0.98

🔍 Detailed Analysis

When MetricAlertRegistryHandler.target() returns None due to a caught OrganizationMember.DoesNotExist exception, the WorkflowEngineActionSerializer passes this None value to human_desc(). If the action is an email type (AlertRuleTriggerAction.Type.EMAIL.value) targeting a user or team (AlertRuleTriggerAction.TargetType.USER.value or AlertRuleTriggerAction.TargetType.TEAM.value), human_desc() attempts to call target.get_email() or target.slug on the None object. This occurs when an email action is configured for a user or team member who is subsequently deleted, leading to an AttributeError.

💡 Suggested Fix

Modify human_desc() to check if target is None before attempting to access target.get_email() or target.slug when action_type is email and target_type is user or team.

🤖 Prompt for AI Agent
Fix this bug. In
src/sentry/notifications/notification_action/group_type_notification_registry/handlers/metric_alert_registry_handler.py
at lines 49-56: When `MetricAlertRegistryHandler.target()` returns `None` due to a
caught `OrganizationMember.DoesNotExist` exception, the `WorkflowEngineActionSerializer`
passes this `None` value to `human_desc()`. If the action is an email type
(`AlertRuleTriggerAction.Type.EMAIL.value`) targeting a user or team
(`AlertRuleTriggerAction.TargetType.USER.value` or
`AlertRuleTriggerAction.TargetType.TEAM.value`), `human_desc()` attempts to call
`target.get_email()` or `target.slug` on the `None` object. This occurs when an email
action is configured for a user or team member who is subsequently deleted, leading to
an `AttributeError`.

Did we get this right? 👍 / 👎 to inform future reviews.

@mifu67 mifu67 requested a review from iamrajjoshi October 24, 2025 22:34
Copy link
Collaborator

@iamrajjoshi iamrajjoshi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we return None here, what happens downstream? I think it should be fine and we will go to the fallback if it exists?

@mifu67
Copy link
Contributor Author

mifu67 commented Oct 24, 2025

If we return None here, what happens downstream? I think it should be fine and we will go to the fallback if it exists?

I assumed it would be like what happens if we return None for team. Will double check!

@codecov
Copy link

codecov bot commented Oct 24, 2025

Codecov Report

❌ Patch coverage is 40.00000% with 3 lines in your changes missing coverage. Please review.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
...registry/handlers/metric_alert_registry_handler.py 50.00% 2 Missing ⚠️
...endpoints/serializers/alert_rule_trigger_action.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           master   #102113      +/-   ##
===========================================
- Coverage   80.95%    80.79%   -0.17%     
===========================================
  Files        8741      8748       +7     
  Lines      388998    391008    +2010     
  Branches    24692     24692              
===========================================
+ Hits       314930    315906     +976     
- Misses      73713     74747    +1034     
  Partials      355       355              

cursor[bot]

This comment was marked as outdated.

elif target_type == AlertRuleTriggerAction.TargetType.TEAM.value:
return "Send an email to members of #" + target.slug
else:
return "Send a notification to [removed]"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: EMAIL Action Missing in Dictionary

The added else clause for EMAIL action type creates a logic gap. When action_target is truthy but target_type is neither USER nor TEAM, the function doesn't return anything from the EMAIL block and falls through to the final else statement (line 48), which tries to access action_type_to_string[action_type]. However, EMAIL is not in the action_type_to_string dictionary (lines 22-27), causing a KeyError. The else clause should be moved inside the if action_target: block to properly handle cases where action_target exists but target_type doesn't match expected values.

Fix in Cursor Fix in Web

@mifu67 mifu67 merged commit 3d2e486 into master Oct 28, 2025
69 checks passed
@mifu67 mifu67 deleted the mifu67/aci/handle-om-dne branch October 28, 2025 18:50
priscilawebdev pushed a commit that referenced this pull request Oct 29, 2025
#102113)

If there is no org member associated with this user and organization,
then we should exit gracefully.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants