-
-
Couldn't load subscription status.
- Fork 4.5k
chore(aci): handle OrganizationMember.DoesNotExist when getting target #102113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this 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?
I assumed it would be like what happens if we return None for team. Will double check! |
Codecov Report❌ Patch coverage is
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 |
| elif target_type == AlertRuleTriggerAction.TargetType.TEAM.value: | ||
| return "Send an email to members of #" + target.slug | ||
| else: | ||
| return "Send a notification to [removed]" |
There was a problem hiding this comment.
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.
#102113) If there is no org member associated with this user and organization, then we should exit gracefully.
If there is no org member associated with this user and organization, then we should exit gracefully.