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
13 changes: 12 additions & 1 deletion src/sentry/rules/conditions/event_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def clean(self):
msg = forms.ValidationError("comparisonInterval is required when comparing by percent")
self.add_error("comparisonInterval", msg)
return
return cleaned_data


class BaseEventFrequencyCondition(EventCondition):
Expand Down Expand Up @@ -217,7 +218,17 @@ class EventFrequencyPercentForm(EventFrequencyForm):
)
]
)
value = forms.FloatField(widget=forms.TextInput(), min_value=0, max_value=100)
value = forms.FloatField(widget=forms.TextInput(), min_value=0)

def clean(self):
cleaned_data = super().clean()
if cleaned_data["comparisonType"] == COMPARISON_TYPE_COUNT and cleaned_data["value"] > 100:
self.add_error(
"value", forms.ValidationError("Ensure this value is less than or equal to 100")
)
return

return cleaned_data


class EventFrequencyPercentCondition(BaseEventFrequencyCondition):
Expand Down
38 changes: 38 additions & 0 deletions tests/sentry/api/endpoints/test_project_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,44 @@ def test_missing_name(self):
status_code=400,
)

def test_frequency_percent_validation(self):
self.login_as(user=self.user)
condition = {
"id": "sentry.rules.conditions.event_frequency.EventFrequencyPercentCondition",
"interval": "1h",
"value": 101,
"comparisonType": "count",
}
response = self.get_error_response(
self.organization.slug,
self.project.slug,
name="test",
frequency=30,
owner=self.user.actor.get_actor_identifier(),
actionMatch="any",
filterMatch="any",
conditions=[condition],
status_code=400,
)
assert (
str(response.data["conditions"][0]) == "Ensure this value is less than or equal to 100"
)

# Upper bound shouldn't be enforced when we're doing a comparison alert
condition["comparisonType"] = "percent"
condition["comparisonInterval"] = "1d"
self.get_success_response(
self.organization.slug,
self.project.slug,
name="test",
frequency=30,
owner=self.user.actor.get_actor_identifier(),
actionMatch="any",
filterMatch="any",
conditions=[condition],
status_code=200,
)

def test_match_values(self):
filters = [
{
Expand Down