Skip to content

Commit d649b5b

Browse files
committed
populate legacy_rule_id and/or workflow_id in action dispatch
1 parent e9b7f3b commit d649b5b

File tree

7 files changed

+37
-103
lines changed

7 files changed

+37
-103
lines changed

src/sentry/notifications/notification_action/types.py

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
from django.core.exceptions import ValidationError
88

9-
from sentry import features
109
from sentry.constants import ObjectStatus
1110
from sentry.exceptions import InvalidIdentity
1211
from sentry.incidents.grouptype import MetricIssueEvidenceData
@@ -193,49 +192,39 @@ def create_rule_instance_from_action(
193192
workflow_id = getattr(action, "workflow_id", None)
194193

195194
label = detector.name
196-
# We need to pass the legacy rule id when the workflow-engine-ui-links feature flag is disabled
197-
# This is so we can build the old link to the rule
198-
if not features.has(
199-
"organizations:workflow-engine-ui-links", detector.project.organization
200-
):
201-
if workflow_id is None:
202-
raise ValueError("Workflow ID is required when triggering an action")
203-
204-
# If test event, just set the legacy rule id to -1
205-
if workflow_id == TEST_NOTIFICATION_ID:
206-
data["actions"][0]["legacy_rule_id"] = TEST_NOTIFICATION_ID
207-
208-
else:
209-
try:
210-
alert_rule_workflow = AlertRuleWorkflow.objects.get(
211-
workflow_id=workflow_id,
212-
)
213-
except AlertRuleWorkflow.DoesNotExist:
214-
raise ValueError(
215-
"AlertRuleWorkflow not found when querying for AlertRuleWorkflow"
216-
)
217-
218-
if alert_rule_workflow.rule_id is None:
219-
raise ValueError("Rule not found when querying for AlertRuleWorkflow")
220-
221-
data["actions"][0]["legacy_rule_id"] = alert_rule_workflow.rule_id
222-
223-
# Get the legacy rule label
224-
try:
225-
rule = Rule.objects.get(id=alert_rule_workflow.rule_id)
226-
label = rule.label
227-
except Rule.DoesNotExist:
228-
logger.exception(
229-
"Rule not found when querying for AlertRuleWorkflow",
230-
extra={"rule_id": alert_rule_workflow.rule_id},
231-
)
232-
# We shouldn't fail badly here since we can still send the notification, so just set it to the rule id
233-
label = f"Rule {alert_rule_workflow.rule_id}"
234-
235-
# In the new UI, we need this for to build the link to the new rule in the notification action
195+
# Build link to the rule if it exists, otherwise build link to the workflow.
196+
# FE will handle redirection if necessary from rule -> workflow
197+
198+
# If test event, just set the legacy rule id to -1
199+
if workflow_id == TEST_NOTIFICATION_ID:
200+
data["actions"][0]["legacy_rule_id"] = TEST_NOTIFICATION_ID
236201
else:
237202
data["actions"][0]["workflow_id"] = workflow_id
238203

204+
# attempt to find legacy_rule_id from the alert rule workflow
205+
rule_id = None
206+
try:
207+
alert_rule_workflow = AlertRuleWorkflow.objects.get(
208+
workflow_id=workflow_id,
209+
)
210+
211+
rule = Rule.objects.get(id=alert_rule_workflow.rule_id)
212+
label = rule.label
213+
rule_id = rule.id
214+
except AlertRuleWorkflow.DoesNotExist:
215+
pass
216+
except Rule.DoesNotExist:
217+
logger.exception(
218+
"Rule not found when querying for AlertRuleWorkflow",
219+
extra={"rule_id": alert_rule_workflow.rule_id},
220+
)
221+
222+
if rule_id:
223+
data["actions"][0]["legacy_rule_id"] = rule_id
224+
225+
if workflow_id is None and rule_id is None:
226+
raise ValueError("Workflow ID or rule ID is required to fire notification")
227+
239228
if workflow_id == TEST_NOTIFICATION_ID and action.type == Action.Type.EMAIL:
240229
# mail action needs to have skipDigests set to True
241230
data["actions"][0]["skipDigests"] = True

tests/sentry/integrations/opsgenie/test_client.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
assert_slo_metric,
1616
)
1717
from sentry.testutils.cases import APITestCase
18-
from sentry.testutils.helpers import with_feature
1918
from sentry.testutils.helpers.options import override_options
2019
from sentry.testutils.skips import requires_snuba
2120

@@ -187,7 +186,6 @@ def test_send_notification_with_workflow_engine_trigger_actions(
187186

188187
@responses.activate
189188
@patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
190-
@with_feature("organizations:workflow-engine-ui-links")
191189
def test_send_notification_with_workflow_engine_ui_links(self, mock_record: MagicMock) -> None:
192190
resp_data = {
193191
"result": "Request will be processed",

tests/sentry/integrations/slack/actions/notification/test_slack_notify_service_action.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from sentry.silo.base import SiloMode
1515
from sentry.testutils.asserts import assert_failure_metric
1616
from sentry.testutils.cases import RuleTestCase
17-
from sentry.testutils.helpers.features import with_feature
1817
from sentry.testutils.helpers.options import override_options
1918
from sentry.testutils.silo import assume_test_silo_mode
2019
from sentry.types.rules import RuleFuture
@@ -372,7 +371,6 @@ def test_after_noa_test_action(
372371
assert thread_ts_success.args[0] == EventLifecycleOutcome.SUCCESS
373372

374373
@override_options({"workflow_engine.issue_alert.group.type_id.ga": [1]})
375-
@with_feature("organizations:workflow-engine-ui-links")
376374
@patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
377375
@patch("sentry.integrations.slack.sdk_client.SlackSdkClient.chat_postMessage")
378376
@patch("slack_sdk.web.client.WebClient._perform_urllib_http_request")

tests/sentry/integrations/slack/notifications/test_issue_alert.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
from sentry.silo.base import SiloMode
3131
from sentry.tasks.digests import deliver_digest
3232
from sentry.testutils.cases import PerformanceIssueTestCase, SlackActivityNotificationTest
33-
from sentry.testutils.helpers.features import with_feature
3433
from sentry.testutils.helpers.notifications import TEST_ISSUE_OCCURRENCE, TEST_PERF_ISSUE_OCCURRENCE
3534
from sentry.testutils.helpers.options import override_options
3635
from sentry.testutils.silo import assume_test_silo_mode
@@ -276,7 +275,6 @@ def test_generic_issue_alert_user_block_workflow_engine_dual_write(
276275
return_value=TEST_ISSUE_OCCURRENCE,
277276
new_callable=mock.PropertyMock,
278277
)
279-
@with_feature("organizations:workflow-engine-ui-links")
280278
def test_generic_issue_alert_user_block_workflow_engine_ui_links(
281279
self, occurrence: MagicMock
282280
) -> None:

tests/sentry/mail/test_actions.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ def setUp(self):
170170
)
171171

172172
@with_feature("organizations:workflow-engine-single-process-workflows")
173-
@with_feature("organizations:workflow-engine-ui-links")
174173
@override_options({"workflow_engine.issue_alert.group.type_id.rollout": [1]})
175174
def test_full_integration(self) -> None:
176175
action_config = {
@@ -197,7 +196,6 @@ def test_full_integration(self) -> None:
197196
assert sent.to == [self.user.email]
198197
assert "uh oh" in sent.subject
199198

200-
@with_feature("organizations:workflow-engine-ui-links")
201199
@with_feature("organizations:workflow-engine-single-process-workflows")
202200
@override_options({"workflow_engine.issue_alert.group.type_id.rollout": [1]})
203201
def test_full_integration_all_members_fallthrough(self) -> None:
@@ -223,7 +221,6 @@ def test_full_integration_all_members_fallthrough(self) -> None:
223221
assert sent.to == [self.user.email]
224222
assert "uh oh" in sent.subject
225223

226-
@with_feature("organizations:workflow-engine-ui-links")
227224
@with_feature("organizations:workflow-engine-single-process-workflows")
228225
@override_options({"workflow_engine.issue_alert.group.type_id.rollout": [1]})
229226
def test_full_integration_noone_fallthrough(self) -> None:
@@ -246,7 +243,6 @@ def test_full_integration_noone_fallthrough(self) -> None:
246243

247244
assert len(mail.outbox) == 0
248245

249-
@with_feature("organizations:workflow-engine-ui-links")
250246
@with_feature("organizations:workflow-engine-single-process-workflows")
251247
@override_options({"workflow_engine.issue_alert.group.type_id.rollout": [1]})
252248
def test_full_integration_fallthrough_not_provided(self) -> None:
@@ -270,7 +266,6 @@ def test_full_integration_fallthrough_not_provided(self) -> None:
270266
assert sent.to == [self.user.email]
271267
assert "uh oh" in sent.subject
272268

273-
@with_feature("organizations:workflow-engine-ui-links")
274269
@with_feature("organizations:workflow-engine-single-process-workflows")
275270
@override_options({"workflow_engine.issue_alert.group.type_id.rollout": [1]})
276271
def test_hack_mail_workflow(self) -> None:

tests/sentry/notifications/notification_action/test_issue_alert_registry_handlers.py

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
JIRA_SERVER_ACTION_DATA_BLOBS,
3535
WEBHOOK_ACTION_DATA_BLOBS,
3636
)
37-
from sentry.testutils.helpers.features import with_feature
3837
from sentry.workflow_engine.models import Action
3938
from sentry.workflow_engine.types import ActionInvocation, WorkflowEventData
4039
from sentry.workflow_engine.typings.notification_action import (
@@ -113,26 +112,12 @@ def get_additional_fields(cls, action: Action, mapping: ActionFieldMapping):
113112
with pytest.raises(ValueError):
114113
handler.create_rule_instance_from_action(self.action, self.detector, self.event_data)
115114

116-
def test_create_rule_instance_from_action_missing_workflow_id_raises_value_error(self) -> None:
117-
job = WorkflowEventData(
118-
event=self.group_event, workflow_env=self.environment, group=self.group
119-
)
120-
action = self.create_action(
121-
type=Action.Type.DISCORD,
122-
integration_id="1234567890",
123-
config={"target_identifier": "channel456", "target_type": ActionTarget.SPECIFIC},
124-
data={"tags": "environment,user,my_tag"},
125-
)
126-
127-
with pytest.raises(ValueError):
128-
self.handler.create_rule_instance_from_action(action, self.detector, job)
129-
130-
def test_create_rule_instance_from_action_missing_rule_raises_value_error(self) -> None:
115+
def test_create_rule_instance_from_action_missing_rule_workflow_id_raises_value_error(
116+
self,
117+
) -> None:
131118
job = WorkflowEventData(
132119
event=self.group_event, workflow_env=self.environment, group=self.group
133120
)
134-
alert_rule = self.create_alert_rule(projects=[self.project], organization=self.organization)
135-
self.create_alert_rule_workflow(workflow=self.workflow, alert_rule_id=alert_rule.id)
136121
action = self.create_action(
137122
type=Action.Type.DISCORD,
138123
integration_id="1234567890",
@@ -164,15 +149,16 @@ def test_create_rule_instance_from_action(self) -> None:
164149
"channel_id": "channel456",
165150
"tags": "environment,user,my_tag",
166151
"legacy_rule_id": self.rule.id,
152+
"workflow_id": self.workflow.id,
167153
}
168154
],
169155
}
170156
assert rule.status == ObjectStatus.ACTIVE
171157
assert rule.source == RuleSource.ISSUE
172158

173-
@with_feature("organizations:workflow-engine-ui-links")
174-
def test_create_rule_instance_from_action_with_workflow_engine_ui_feature_flag(self) -> None:
159+
def test_create_rule_instance_from_action_with_workflow_only(self) -> None:
175160
"""Test that create_rule_instance_from_action creates a Rule with correct attributes"""
161+
self.rule.delete()
176162
rule = self.handler.create_rule_instance_from_action(
177163
self.action, self.detector, self.event_data
178164
)
@@ -217,36 +203,9 @@ def test_create_rule_instance_from_action_no_environment(self) -> None:
217203
"channel_id": "channel456",
218204
"tags": "environment,user,my_tag",
219205
"legacy_rule_id": self.rule.id,
220-
}
221-
],
222-
}
223-
assert rule.status == ObjectStatus.ACTIVE
224-
assert rule.source == RuleSource.ISSUE
225-
226-
@with_feature("organizations:workflow-engine-ui-links")
227-
def test_create_rule_instance_from_action_no_environment_with_workflow_engine_ui_feature_flag(
228-
self,
229-
):
230-
"""Test that create_rule_instance_from_action creates a Rule with correct attributes"""
231-
self.create_workflow()
232-
job = WorkflowEventData(event=self.group_event, workflow_env=None, group=self.group)
233-
rule = self.handler.create_rule_instance_from_action(self.action, self.detector, job)
234-
235-
assert isinstance(rule, Rule)
236-
assert rule.id == self.action.id
237-
assert rule.project == self.detector.project
238-
assert rule.environment_id is None
239-
assert rule.label == self.detector.name
240-
assert rule.data == {
241-
"actions": [
242-
{
243-
"id": "sentry.integrations.discord.notify_action.DiscordNotifyServiceAction",
244-
"server": "1234567890",
245-
"channel_id": "channel456",
246-
"tags": "environment,user,my_tag",
247206
"workflow_id": self.workflow.id,
248207
}
249-
]
208+
],
250209
}
251210
assert rule.status == ObjectStatus.ACTIVE
252211
assert rule.source == RuleSource.ISSUE

tests/sentry/rules/actions/test_notify_event_service.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ def test_applies_correctly_for_legacy_webhooks(self) -> None:
105105

106106
@responses.activate
107107
@with_feature("organizations:workflow-engine-single-process-workflows")
108-
@with_feature("organizations:workflow-engine-ui-links")
109108
@override_options({"workflow_engine.issue_alert.group.type_id.rollout": [1]})
110109
def test_applies_correctly_for_legacy_webhooks_aci(self):
111110
responses.add(responses.POST, "http://my-fake-webhook.io")
@@ -163,7 +162,6 @@ def test_applies_correctly_for_legacy_webhooks_aci(self):
163162

164163
@responses.activate
165164
@with_feature("organizations:workflow-engine-single-process-workflows")
166-
@with_feature("organizations:workflow-engine-ui-links")
167165
@override_options({"workflow_engine.issue_alert.group.type_id.rollout": [1]})
168166
def test_legacy_webhooks_uneven_dual_write_aci(self):
169167
"""
@@ -203,7 +201,6 @@ def test_legacy_webhooks_uneven_dual_write_aci(self):
203201

204202
@responses.activate
205203
@with_feature("organizations:workflow-engine-single-process-workflows")
206-
@with_feature("organizations:workflow-engine-ui-links")
207204
@override_options({"workflow_engine.issue_alert.group.type_id.rollout": [1]})
208205
@patch("sentry.plugins.sentry_webhooks.plugin.WebHooksPlugin.notify_users")
209206
def test_error_for_legacy_webhooks_dual_write_aci(self, mock_notify_users):

0 commit comments

Comments
 (0)