Skip to content

Commit 49f1e8f

Browse files
committed
Fix rule filterMatch mapping and action typing
1 parent 096141e commit 49f1e8f

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

src/sentry/api/endpoints/project_rules.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,8 +775,12 @@ def format_request_data(
775775
if target_type is not None:
776776
action["config"]["target_type"] = ActionTarget.get_name(target_type)
777777

778+
filter_match = data.get("filterMatch", "any-short")
779+
if filter_match == "any":
780+
filter_match = DataConditionGroup.Type.ANY_SHORT_CIRCUIT.value
781+
778782
action_filters = {
779-
"logicType": data.get("filterMatch", "any-short"),
783+
"logicType": filter_match,
780784
"conditions": translated_filter_list,
781785
"actions": translated_actions,
782786
}

src/sentry/workflow_engine/migration_helpers/rule_action.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
import logging
2-
from typing import Any, cast
2+
from typing import Any, TypedDict
33

4-
from sentry.workflow_engine.endpoints.validators.base.action import ActionData
54
from sentry.workflow_engine.models.action import Action
65
from sentry.workflow_engine.typings.notification_action import issue_alert_action_translator_mapping
76

87
logger = logging.getLogger(__name__)
98

109

10+
class NotificationActionData(TypedDict):
11+
type: str
12+
data: dict[str, Any]
13+
integration_id: int | None
14+
config: dict[str, str | int | None]
15+
16+
1117
def translate_rule_data_actions_to_notification_actions(
1218
actions: list[dict[str, Any]], skip_failures: bool
13-
) -> list[ActionData]:
19+
) -> list[NotificationActionData]:
1420
"""
1521
Builds notification actions from action field in Rule's data blob.
1622
Will only create actions that are valid, and log any errors.
@@ -20,7 +26,7 @@ def translate_rule_data_actions_to_notification_actions(
2026
:return: list of notification actions (Action)
2127
"""
2228

23-
notification_actions: list[ActionData] = []
29+
notification_actions: list[NotificationActionData] = []
2430

2531
for action in actions:
2632
# Fetch the registry ID
@@ -76,7 +82,7 @@ def translate_rule_data_actions_to_notification_actions(
7682
"config": translator.action_config,
7783
}
7884

79-
notification_actions.append(cast(ActionData, notification_action_data))
85+
notification_actions.append(notification_action_data)
8086
except Exception as e:
8187
if not skip_failures:
8288
raise

tests/sentry/api/endpoints/test_project_rules.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from rest_framework import status
1313
from slack_sdk.web import SlackResponse
1414

15-
from sentry.api.endpoints.project_rules import get_max_alerts
15+
from sentry.api.endpoints.project_rules import format_request_data, get_max_alerts
1616
from sentry.constants import ObjectStatus
1717
from sentry.incidents.endpoints.serializers.utils import (
1818
get_fake_id_from_object_id,
@@ -395,6 +395,24 @@ def run_test(
395395
assert RuleActivity.objects.filter(rule=rule, type=RuleActivityType.CREATED.value).exists()
396396
return response
397397

398+
def test_format_request_data_maps_any_filter_match_to_any_short(self) -> None:
399+
payload = {
400+
"name": "my rule",
401+
"status": "active",
402+
"frequency": 30,
403+
"conditions": [],
404+
"filters": [],
405+
"actions": [],
406+
"filterMatch": "any",
407+
}
408+
409+
workflow_payload = format_request_data(payload)
410+
411+
assert (
412+
workflow_payload["actionFilters"][0]["logicType"]
413+
== DataConditionGroup.Type.ANY_SHORT_CIRCUIT.value
414+
)
415+
398416
def test_simple(self) -> None:
399417
self.run_test(actions=self.notify_issue_owners_action, conditions=self.first_seen_condition)
400418

0 commit comments

Comments
 (0)