Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/sentry/api/endpoints/project_rule_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ def execute_future_on_test_event_workflow_engine(

event_data = WorkflowEventData(
event=test_event,
workflow_id=workflow.id,
)

for action_blob in actions:
Expand All @@ -168,6 +167,8 @@ def execute_future_on_test_event_workflow_engine(
[action_blob], skip_failures=False
)[0]
action.id = -1
# Annotate the action with the workflow id
setattr(action, "workflow_id", workflow.id)
except Exception as e:
action_exceptions.append(str(e))
sentry_sdk.capture_exception(e)
Expand Down
10 changes: 6 additions & 4 deletions src/sentry/notifications/notification_action/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,22 +135,24 @@ def create_rule_instance_from_action(
"actions": [cls.build_rule_action_blob(action, detector.project.organization.id)],
}

workflow_id = getattr(action, "workflow_id", None)

label = detector.name
# We need to pass the legacy rule id when the workflow-engine-ui-links feature flag is disabled
# This is so we can build the old link to the rule
if not features.has(
"organizations:workflow-engine-ui-links", detector.project.organization
):
if job.workflow_id is None:
if workflow_id is None:
raise ValueError("Workflow ID is required when triggering an action")

# If test event, just set the legacy rule id to -1
if job.workflow_id == -1:
if workflow_id == -1:
data["actions"][0]["legacy_rule_id"] = -1
else:
try:
alert_rule_workflow = AlertRuleWorkflow.objects.get(
workflow_id=job.workflow_id,
workflow_id=workflow_id,
)
except AlertRuleWorkflow.DoesNotExist:
raise ValueError(
Expand All @@ -176,7 +178,7 @@ def create_rule_instance_from_action(

# In the new UI, we need this for to build the link to the new rule in the notification action
else:
data["actions"][0]["workflow_id"] = job.workflow_id
data["actions"][0]["workflow_id"] = workflow_id

rule = Rule(
id=action.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ def test_fire_actions(actions: list[dict[str, Any]], project: Project):
workflow_id = -1
workflow_event_data = WorkflowEventData(
event=test_event,
workflow_id=workflow_id,
)

detector = Detector(
Expand All @@ -128,6 +127,9 @@ def test_fire_actions(actions: list[dict[str, Any]], project: Project):
config=action_data.get("config", {}),
)

# Annotate the action with the workflow id
setattr(action, "workflow_id", workflow_id)

# Test fire the action and collect any exceptions
exceptions = test_fire_action(action, workflow_event_data, detector)
if exceptions:
Expand Down
1 change: 0 additions & 1 deletion src/sentry/workflow_engine/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ class WorkflowEventData:
group_state: GroupState | None = None
has_reappeared: bool | None = None
has_escalated: bool | None = None
workflow_id: int | None = None
workflow_env: Environment | None = None


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ def setUp(self):
data={"tags": "environment,user,my_tag"},
)
self.group, self.event, self.group_event = self.create_group_event()
self.event_data = WorkflowEventData(
event=self.group_event, workflow_env=self.environment, workflow_id=self.workflow.id
)
self.event_data = WorkflowEventData(event=self.group_event, workflow_env=self.environment)

self.action.workflow_id = self.workflow.id

class TestHandler(BaseIssueAlertHandler):
@classmethod
Expand All @@ -112,15 +112,29 @@ def get_additional_fields(cls, action: Action, mapping: ActionFieldMapping):

def test_create_rule_instance_from_action_missing_workflow_id_raises_value_error(self):
job = WorkflowEventData(event=self.group_event, workflow_env=self.environment)
action = self.create_action(
type=Action.Type.DISCORD,
integration_id="1234567890",
config={"target_identifier": "channel456", "target_type": ActionTarget.SPECIFIC},
data={"tags": "environment,user,my_tag"},
)

with pytest.raises(ValueError):
self.handler.create_rule_instance_from_action(self.action, self.detector, job)
self.handler.create_rule_instance_from_action(action, self.detector, job)

def test_create_rule_instance_from_action_missing_rule_raises_value_error(self):
job = WorkflowEventData(event=self.group_event, workflow_env=self.environment)
alert_rule = self.create_alert_rule(projects=[self.project], organization=self.organization)
self.create_alert_rule_workflow(workflow=self.workflow, alert_rule_id=alert_rule.id)
action = self.create_action(
type=Action.Type.DISCORD,
integration_id="1234567890",
config={"target_identifier": "channel456", "target_type": ActionTarget.SPECIFIC},
data={"tags": "environment,user,my_tag"},
)

with pytest.raises(ValueError):
self.handler.create_rule_instance_from_action(self.action, self.detector, job)
self.handler.create_rule_instance_from_action(action, self.detector, job)

def test_create_rule_instance_from_action(self):
"""Test that create_rule_instance_from_action creates a Rule with correct attributes"""
Expand Down Expand Up @@ -180,9 +194,7 @@ def test_create_rule_instance_from_action_with_workflow_engine_ui_feature_flag(s
def test_create_rule_instance_from_action_no_environment(self):
"""Test that create_rule_instance_from_action creates a Rule with correct attributes"""
self.create_workflow()
job = WorkflowEventData(
event=self.group_event, workflow_env=None, workflow_id=self.workflow.id
)
job = WorkflowEventData(event=self.group_event, workflow_env=None)
rule = self.handler.create_rule_instance_from_action(self.action, self.detector, job)

assert isinstance(rule, Rule)
Expand Down Expand Up @@ -210,9 +222,7 @@ def test_create_rule_instance_from_action_no_environment_with_workflow_engine_ui
):
"""Test that create_rule_instance_from_action creates a Rule with correct attributes"""
self.create_workflow()
job = WorkflowEventData(
event=self.group_event, workflow_env=None, workflow_id=self.workflow.id
)
job = WorkflowEventData(event=self.group_event, workflow_env=None)
rule = self.handler.create_rule_instance_from_action(self.action, self.detector, job)

assert isinstance(rule, Rule)
Expand Down
Loading