Skip to content

Commit fcc5f0c

Browse files
feat(alerts): Add alert type filter backend (#83784)
Supports a `alertType` query parameter (as a list) to filter down the alerts Supports - `rule` (classic issue alert) - `alert_rule` (metric alert) - `uptime` (uptime monitor) - `monitor` (cron monitor) --------- Co-authored-by: getsantry[bot] <66042841+getsantry[bot]@users.noreply.github.com>
1 parent a11c5d9 commit fcc5f0c

File tree

2 files changed

+75
-6
lines changed

2 files changed

+75
-6
lines changed

src/sentry/incidents/endpoints/organization_alert_rule_index.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,13 +322,20 @@ def get(self, request: Request, organization) -> Response:
322322
),
323323
)
324324

325-
intermediaries = [
326-
CombinedQuerysetIntermediary(alert_rules, sort_key),
327-
CombinedQuerysetIntermediary(issue_rules, rule_sort_key),
328-
CombinedQuerysetIntermediary(uptime_rules, sort_key),
329-
]
325+
type_filter = request.GET.getlist("alertType", [])
326+
327+
def has_type(type: str) -> bool:
328+
return not type_filter or type in type_filter
329+
330+
intermediaries: list[CombinedQuerysetIntermediary] = []
330331

331-
if features.has("organizations:insights-crons", organization):
332+
if has_type("alert_rule"):
333+
intermediaries.append(CombinedQuerysetIntermediary(alert_rules, sort_key))
334+
if has_type("rule"):
335+
intermediaries.append(CombinedQuerysetIntermediary(issue_rules, rule_sort_key))
336+
if has_type("uptime"):
337+
intermediaries.append(CombinedQuerysetIntermediary(uptime_rules, sort_key))
338+
if has_type("monitor") and features.has("organizations:insights-crons", organization):
332339
intermediaries.append(CombinedQuerysetIntermediary(crons_rules, sort_key))
333340

334341
response = self.paginate(

tests/sentry/incidents/endpoints/test_organization_combined_rule_index_endpoint.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,3 +1309,65 @@ def test_dataset_filter(self):
13091309
# without performance-view, we should only see events rules
13101310
res = self.get_success_response(self.organization.slug)
13111311
self.assert_alert_rule_serialized(events_rule, res.data[0], skip_dates=True)
1312+
1313+
def test_alert_type_filter(self):
1314+
# Setup one of each type of alert rule
1315+
metric_rule = self.create_alert_rule()
1316+
issue_rule = self.create_issue_alert_rule(
1317+
data={
1318+
"project": self.project,
1319+
"name": "Issue Rule Test",
1320+
"conditions": [],
1321+
"actions": [],
1322+
"actionMatch": "all",
1323+
"date_added": before_now(minutes=4),
1324+
}
1325+
)
1326+
uptime_rule = self.create_project_uptime_subscription(project=self.project)
1327+
cron_rule = self.create_monitor(project=self.project)
1328+
1329+
features = [
1330+
"organizations:incidents",
1331+
"organizations:performance-view",
1332+
"organizations:insights-crons",
1333+
]
1334+
1335+
# Everything comes back without the query parameter
1336+
with self.feature(features):
1337+
request_data = {"per_page": "10", "project": [self.project.id]}
1338+
response = self.client.get(
1339+
path=self.combined_rules_url,
1340+
data=request_data,
1341+
content_type="application/json",
1342+
)
1343+
assert response.status_code == 200, response.content
1344+
assert {r["id"] for r in response.data} == {
1345+
str(metric_rule.id),
1346+
str(issue_rule.id),
1347+
str(uptime_rule.id),
1348+
str(cron_rule.guid),
1349+
}
1350+
1351+
test_cases: list[tuple[list[str], set[str]]] = [
1352+
(["rule"], {str(issue_rule.id)}),
1353+
(["alert_rule"], {str(metric_rule.id)}),
1354+
(["monitor"], {str(cron_rule.guid)}),
1355+
(["uptime"], {str(uptime_rule.id)}),
1356+
(["rule", "alert_rule"], {str(issue_rule.id), str(metric_rule.id)}),
1357+
(["uptime", "monitor"], {str(uptime_rule.id), str(cron_rule.guid)}),
1358+
]
1359+
1360+
for alert_type, expected_ids in test_cases:
1361+
with self.feature(features):
1362+
request_data = {
1363+
"per_page": "10",
1364+
"project": [self.project.id],
1365+
"alertType": alert_type,
1366+
}
1367+
response = self.client.get(
1368+
path=self.combined_rules_url,
1369+
data=request_data,
1370+
content_type="application/json",
1371+
)
1372+
assert response.status_code == 200, response.content
1373+
assert {r["id"] for r in response.data} == expected_ids

0 commit comments

Comments
 (0)