Skip to content

Commit

Permalink
feat(alerts): require trigger actions to save metric alerts (#78446)
Browse files Browse the repository at this point in the history
Prevent saving metric alerts if each trigger doesn't have an associated
action
  • Loading branch information
mifu67 authored Oct 2, 2024
1 parent 41decd5 commit dc3fc08
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ def create_metric_alert(
if not serializer.is_valid():
raise ValidationError(serializer.errors)

# if there are no triggers, then the serializer will raise an error
for trigger in data["triggers"]:
if not trigger.get("actions", []):
raise ValidationError(
"Each trigger must have an associated action for this alert to fire."
)

trigger_sentry_app_action_creators_for_incidents(serializer.validated_data)
if get_slack_actions_with_async_lookups(organization, request.user, request.data):
# need to kick off an async job for Slack
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -921,12 +921,15 @@ def test_critical_trigger_no_action(self):
}

with self.feature("organizations:incidents"):
resp = self.get_success_response(
self.organization.slug, status_code=201, **rule_one_trigger_only_critical_no_action
resp = self.get_error_response(
self.organization.slug, status_code=400, **rule_one_trigger_only_critical_no_action
)
assert "id" in resp.data
alert_rule = AlertRule.objects.get(id=resp.data["id"])
assert resp.data == serialize(alert_rule, self.user)
assert resp.data == [
ErrorDetail(
string="Each trigger must have an associated action for this alert to fire.",
code="invalid",
)
]

def test_invalid_projects(self):
with self.feature("organizations:incidents"):
Expand Down Expand Up @@ -1009,7 +1012,15 @@ def test_no_owner(self):
"name": "JustATestRule",
"resolveThreshold": 100,
"thresholdType": 1,
"triggers": [{"label": "critical", "alertThreshold": 75}],
"triggers": [
{
"label": "critical",
"alertThreshold": 75,
"actions": [
{"type": "email", "targetType": "team", "targetIdentifier": self.team.id}
],
}
],
}

with self.feature("organizations:incidents"):
Expand Down

0 comments on commit dc3fc08

Please sign in to comment.