Skip to content

Commit b274ba5

Browse files
committed
set workflow in job when evaluating action filters
1 parent 7c4818a commit b274ba5

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

src/sentry/workflow_engine/processors/workflow.py

+19-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
DataConditionGroup,
1717
Detector,
1818
Workflow,
19+
WorkflowDataConditionGroup,
1920
)
2021
from sentry.workflow_engine.processors.action import filter_recently_fired_workflow_actions
2122
from sentry.workflow_engine.processors.data_condition_group import process_data_condition_group
@@ -73,6 +74,8 @@ def evaluate_workflow_triggers(workflows: set[Workflow], job: WorkflowJob) -> se
7374
if evaluation:
7475
triggered_workflows.add(workflow)
7576

77+
job.pop("workflow", None)
78+
7679
return triggered_workflows
7780

7881

@@ -82,14 +85,25 @@ def evaluate_workflows_action_filters(
8285
) -> BaseQuerySet[Action]:
8386
filtered_action_groups: set[DataConditionGroup] = set()
8487

85-
# gets the list of the workflow ids, and then get the workflow_data_condition_groups for those workflows
86-
workflow_ids = {workflow.id for workflow in workflows}
88+
# Gets the list of the workflow ids, and then get the workflow_data_condition_groups for those workflows
89+
workflow_ids_to_workflows = {workflow.id: workflow for workflow in workflows}
8790

8891
action_conditions = DataConditionGroup.objects.filter(
89-
workflowdataconditiongroup__workflow_id__in=workflow_ids
92+
workflowdataconditiongroup__workflow_id__in=list(workflow_ids_to_workflows.keys())
9093
).distinct()
9194

95+
workflow_to_dcg = dict(
96+
WorkflowDataConditionGroup.objects.filter(
97+
condition_group_id__in=action_conditions
98+
).values_list("condition_group_id", "workflow")
99+
)
100+
92101
for action_condition in action_conditions:
102+
# Populate the workflow in the job for the action_condition evaluation
103+
workflow_id = workflow_to_dcg.get(action_condition.id)
104+
if workflow_id:
105+
job["workflow"] = workflow_ids_to_workflows[workflow_id]
106+
93107
(evaluation, result), remaining_conditions = process_data_condition_group(
94108
action_condition.id, job
95109
)
@@ -109,6 +123,8 @@ def evaluate_workflows_action_filters(
109123
if evaluation:
110124
filtered_action_groups.add(action_condition)
111125

126+
job.pop("workflow", None)
127+
112128
return filter_recently_fired_workflow_actions(filtered_action_groups, job["event"].group)
113129

114130

tests/sentry/workflow_engine/processors/test_workflow.py

+48
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,54 @@ def test_error_event__logger(self, mock_logger):
117117
},
118118
)
119119

120+
@patch("sentry.workflow_engine.processors.workflow.filter_recently_fired_workflow_actions")
121+
def test_populate_workflow_for_filters(self, mock_filter):
122+
# this should not pass because the environment is not None
123+
self.error_workflow.update(environment=self.group_event.get_environment())
124+
error_workflow_filters = self.create_data_condition_group(
125+
logic_type=DataConditionGroup.Type.ANY_SHORT_CIRCUIT
126+
)
127+
self.create_data_condition(
128+
condition_group=error_workflow_filters,
129+
type=Condition.FIRST_SEEN_EVENT,
130+
comparison=True,
131+
condition_result=True,
132+
)
133+
self.create_workflow_data_condition_group(
134+
workflow=self.error_workflow, condition_group=error_workflow_filters
135+
)
136+
137+
workflow_triggers = self.create_data_condition_group(
138+
logic_type=DataConditionGroup.Type.ANY_SHORT_CIRCUIT
139+
)
140+
workflow_filters = self.create_data_condition_group(
141+
logic_type=DataConditionGroup.Type.ANY_SHORT_CIRCUIT
142+
)
143+
# this should pass because the environment is None
144+
self.create_data_condition(
145+
condition_group=workflow_filters,
146+
type=Condition.FIRST_SEEN_EVENT,
147+
comparison=True,
148+
condition_result=True,
149+
)
150+
workflow = self.create_workflow(
151+
name="testy",
152+
when_condition_group=workflow_triggers,
153+
)
154+
self.create_detector_workflow(
155+
detector=self.error_detector,
156+
workflow=workflow,
157+
)
158+
self.create_workflow_data_condition_group(
159+
workflow=workflow, condition_group=workflow_filters
160+
)
161+
162+
self.job["group_state"]["is_new"] = True
163+
164+
process_workflows(self.job)
165+
166+
mock_filter.assert_called_with({workflow_filters}, self.group)
167+
120168
def test_same_environment_only(self):
121169
# only processes workflows with the same env or no env specified
122170
self.error_workflow.update(environment=None)

0 commit comments

Comments
 (0)