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
4 changes: 3 additions & 1 deletion src/sentry/api/bases/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ def convert_args(
if use_workflow_engine:
try:
arw = AlertRuleWorkflow.objects.get(
rule_id=rule_id, workflow__organization=project.organization
rule_id=rule_id,
workflow__organization=project.organization,
workflow__status=ObjectStatus.ACTIVE,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any concerns around indexes / query times with filtering by status?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in this case. we're already filtering on workflow attributes here. This does remind me that we could be selecting workflow here and saving a fetch, but that's a separate issue.

)
kwargs["rule"] = arw.workflow
except AlertRuleWorkflow.DoesNotExist:
Expand Down
4 changes: 4 additions & 0 deletions src/sentry/api/endpoints/project_rule_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,10 @@ def delete(self, request: Request, project: Project, rule: Rule | Workflow) -> R
rule=rule, user_id=request.user.id, type=RuleActivityType.DELETED.value
)
scheduled = CellScheduledDeletion.schedule(rule, days=0, actor=request.user)
# The Rule's scheduled deletion should take care of the workflow, but
# we mark it pending immediately so we don't return it while the deletion is in progress.
for workflow in Workflow.objects.filter(alertruleworkflow__rule_id=rule.id):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For another PR; thoughts on us modifying the manager to handle this for us? we are doing that pattern in Detectors here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah, I'd actually forgotten I was going to do this to the association tables.
I still intend to do that (actually, have a PR going), but in this case, the Workflow is logically deleted, even if you're not going through the association table, so we can't rely on the association table to hide it, we need to mark it pending.

workflow.update(status=ObjectStatus.PENDING_DELETION)

self.create_audit_entry(
request=request,
Expand Down
10 changes: 10 additions & 0 deletions tests/sentry/api/endpoints/test_project_rule_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,16 @@ def test_workflow_engine_granular_flag_single_written_rule(self) -> None:
assert response.data["conditions"][0]["name"]
assert response.data["filters"][0]["name"]

@with_feature("organizations:workflow-engine-issue-alert-endpoints-get")
def test_deleted_dual_written_rule_returns_404(self) -> None:
rule = self.create_project_rule(self.project)
# DELETE schedules deletion but we intentionally do NOT run it
self.get_success_response(
self.organization.slug, rule.project.slug, rule.id, method="delete", status_code=202
)
# GET should 404 even though the scheduled deletion hasn't executed
self.get_error_response(self.organization.slug, rule.project.slug, rule.id, status_code=404)

def test_non_existing_rule(self) -> None:
self.get_error_response(self.organization.slug, self.project.slug, 12345, status_code=404)

Expand Down
Loading