Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -605,9 +605,9 @@ def check_can_create_alert(self, request: Request, organization: Organization) -
"organizations:workflow-engine-metric-detector-limit", organization, actor=request.user
):
alert_limit = quotas.backend.get_metric_detector_limit(organization.id)
alert_count = AlertRule.objects.fetch_for_organization(
organization=organization
).count()
alert_count = AlertRule.objects.fetch_for_organization(organization=organization)
# filter out alert rules without any projects
Copy link
Member

Choose a reason for hiding this comment

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

might want to leave a comment that this is an unusual buggy path that we don't expect to normally happen

alert_count = alert_count.filter(projects__isnull=False).distinct().count()

if alert_limit >= 0 and alert_count >= alert_limit:
raise ValidationError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1612,6 +1612,13 @@ def test_performance_alert(self, record_analytics: MagicMock) -> None:
@with_feature("organizations:workflow-engine-metric-detector-limit")
@patch("sentry.quotas.backend.get_metric_detector_limit")
def test_metric_alert_limit(self, mock_get_limit: MagicMock) -> None:
# create orphaned metric alert
project_to_delete = self.create_project(organization=self.organization)
alert_rule = self.create_alert_rule(
organization=self.organization, projects=[project_to_delete]
)
project_to_delete.delete()

# Set limit to 2 alert rules
mock_get_limit.return_value = 2

Expand Down Expand Up @@ -1646,6 +1653,13 @@ def test_metric_alert_limit(self, mock_get_limit: MagicMock) -> None:
@with_feature("organizations:incidents")
@with_feature("organizations:workflow-engine-metric-detector-limit")
def test_metric_alert_limit_unlimited_plan(self) -> None:
# create orphaned metric alert
project_to_delete = self.create_project(organization=self.organization)
alert_rule = self.create_alert_rule(
organization=self.organization, projects=[project_to_delete]
)
project_to_delete.delete()

# Create many alert rules
for _ in range(5):
self.create_alert_rule(organization=self.organization)
Expand Down
Loading