Skip to content

Commit 4c70163

Browse files
authored
fix(aci): update workflow filter evaluation with workflow env (#87678)
1 parent 06408af commit 4c70163

File tree

2 files changed

+71
-11
lines changed

2 files changed

+71
-11
lines changed

src/sentry/workflow_engine/processors/workflow.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from dataclasses import asdict
2+
from dataclasses import asdict, replace
33
from enum import StrEnum
44

55
import sentry_sdk
@@ -84,27 +84,38 @@ def evaluate_workflows_action_filters(
8484
) -> BaseQuerySet[Action]:
8585
filtered_action_groups: set[DataConditionGroup] = set()
8686

87-
# gets the list of the workflow ids, and then get the workflow_data_condition_groups for those workflows
88-
workflow_ids = {workflow.id for workflow in workflows}
87+
# Gets the list of the workflow ids, and then get the workflow_data_condition_groups for those workflows
88+
workflow_ids_to_envs = {workflow.id: workflow.environment for workflow in workflows}
8989

90-
action_conditions = DataConditionGroup.objects.filter(
91-
workflowdataconditiongroup__workflow_id__in=workflow_ids
92-
).distinct()
90+
action_conditions = (
91+
DataConditionGroup.objects.filter(
92+
workflowdataconditiongroup__workflow_id__in=list(workflow_ids_to_envs.keys())
93+
)
94+
.prefetch_related("workflowdataconditiongroup_set")
95+
.distinct()
96+
)
9397

9498
for action_condition in action_conditions:
95-
# TODO(cathy): attach correct workflow to job
99+
workflow_job = job
100+
101+
workflow_data_condition_group = action_condition.workflowdataconditiongroup_set.first()
102+
103+
# Populate the workflow_env in the job for the action_condition evaluation
104+
if workflow_data_condition_group:
105+
workflow_job = replace(
106+
job, workflow_env=workflow_data_condition_group.workflow.environment
107+
)
96108

97109
(evaluation, result), remaining_conditions = process_data_condition_group(
98-
action_condition.id, job
110+
action_condition.id, workflow_job
99111
)
100112

101113
if remaining_conditions:
102114
# If there are remaining conditions for the action filter to evaluate,
103115
# then return the list of conditions to enqueue
104-
condition_group = action_condition.workflowdataconditiongroup_set.first()
105-
if condition_group:
116+
if workflow_data_condition_group:
106117
enqueue_workflow(
107-
condition_group.workflow,
118+
workflow_data_condition_group.workflow,
108119
remaining_conditions,
109120
job.event,
110121
WorkflowDataConditionGroupType.ACTION_FILTER,

tests/sentry/workflow_engine/processors/test_workflow.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,55 @@ def test_error_event__logger(self, mock_logger):
108108
},
109109
)
110110

111+
@patch("sentry.workflow_engine.processors.workflow.filter_recently_fired_workflow_actions")
112+
def test_populate_workflow_env_for_filters(self, mock_filter):
113+
# this should not pass because the environment is not None
114+
self.error_workflow.update(environment=self.group_event.get_environment())
115+
error_workflow_filters = self.create_data_condition_group(
116+
logic_type=DataConditionGroup.Type.ANY_SHORT_CIRCUIT
117+
)
118+
self.create_data_condition(
119+
condition_group=error_workflow_filters,
120+
type=Condition.FIRST_SEEN_EVENT,
121+
comparison=True,
122+
condition_result=True,
123+
)
124+
self.create_workflow_data_condition_group(
125+
workflow=self.error_workflow, condition_group=error_workflow_filters
126+
)
127+
128+
workflow_triggers = self.create_data_condition_group(
129+
logic_type=DataConditionGroup.Type.ANY_SHORT_CIRCUIT
130+
)
131+
workflow_filters = self.create_data_condition_group(
132+
logic_type=DataConditionGroup.Type.ANY_SHORT_CIRCUIT
133+
)
134+
# this should pass because the environment is None
135+
self.create_data_condition(
136+
condition_group=workflow_filters,
137+
type=Condition.FIRST_SEEN_EVENT,
138+
comparison=True,
139+
condition_result=True,
140+
)
141+
workflow = self.create_workflow(
142+
name="testy",
143+
when_condition_group=workflow_triggers,
144+
)
145+
self.create_detector_workflow(
146+
detector=self.error_detector,
147+
workflow=workflow,
148+
)
149+
self.create_workflow_data_condition_group(
150+
workflow=workflow, condition_group=workflow_filters
151+
)
152+
153+
assert self.job.group_state
154+
self.job.group_state["is_new"] = True
155+
156+
process_workflows(self.job)
157+
158+
mock_filter.assert_called_with({workflow_filters}, self.group)
159+
111160
def test_same_environment_only(self):
112161
env = self.create_environment(project=self.project)
113162
other_env = self.create_environment(project=self.project)

0 commit comments

Comments
 (0)