-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
feat(workflow_engine): Add create methods to validators #89769
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a18c520
bd8d93f
b93dc39
bd3cf02
15481b9
3685e58
61033da
f8e1873
b2ef2c9
34c5db2
1fbb53c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,104 @@ | ||
from typing import Any | ||
|
||
from django.db import router, transaction | ||
from rest_framework import serializers | ||
|
||
# from sentry import audit_log | ||
from sentry.api.serializers.rest_framework import CamelSnakeSerializer | ||
from sentry.workflow_engine.endpoints.validators.base import BaseDataConditionGroupValidator | ||
|
||
# from sentry.utils.audit import create_audit_entry | ||
from sentry.workflow_engine.endpoints.validators.base import ( | ||
BaseActionValidator, | ||
BaseDataConditionGroupValidator, | ||
) | ||
from sentry.workflow_engine.endpoints.validators.utils import validate_json_schema | ||
from sentry.workflow_engine.models import Workflow | ||
from sentry.workflow_engine.models import ( | ||
DataConditionGroupAction, | ||
Workflow, | ||
WorkflowDataConditionGroup, | ||
) | ||
|
||
DataConditionGroupData = dict[str, Any] | ||
ActionData = list[dict[str, Any]] | ||
|
||
|
||
class WorkflowValidator(CamelSnakeSerializer): | ||
name = serializers.CharField(required=True, max_length=256) | ||
enabled = serializers.BooleanField(required=False, default=True) | ||
config = serializers.JSONField(required=False) | ||
triggers = BaseDataConditionGroupValidator(required=False) | ||
action_filters = serializers.ListField(child=BaseDataConditionGroupValidator(), required=False) | ||
action_filters = serializers.ListField(required=False) | ||
|
||
# TODO - Need to improve the following fields (validate them in db) | ||
organization_id = serializers.IntegerField(required=True) | ||
environment_id = serializers.IntegerField(required=False) | ||
|
||
def _split_action_and_condition_group( | ||
self, action_filter: dict[str, Any] | ||
) -> tuple[ActionData, DataConditionGroupData]: | ||
try: | ||
actions = action_filter["actions"] | ||
except KeyError: | ||
raise serializers.ValidationError("Missing actions key in action filter") | ||
|
||
return actions, action_filter | ||
|
||
def validate_config(self, value): | ||
schema = Workflow.config_schema | ||
return validate_json_schema(value, schema) | ||
|
||
def validate_action_filters(self, value): | ||
for action_filter in value: | ||
actions, condition_group = self._split_action_and_condition_group(action_filter) | ||
BaseDataConditionGroupValidator(data=condition_group).is_valid(raise_exception=True) | ||
|
||
for action in actions: | ||
BaseActionValidator(data=action).is_valid(raise_exception=True) | ||
|
||
return value | ||
|
||
def create(self, validated_value: dict[str, Any]) -> Workflow: | ||
condition_group_validator = BaseDataConditionGroupValidator() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should the Workflow validator automatically convert There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i don't feel like it should auto-convert that. It probably could based on the we should not convert |
||
action_validator = BaseActionValidator() | ||
|
||
with transaction.atomic(router.db_for_write(Workflow)): | ||
when_condition_group = condition_group_validator.create(validated_value["triggers"]) | ||
|
||
workflow = Workflow.objects.create( | ||
name=validated_value["name"], | ||
enabled=validated_value["enabled"], | ||
config=validated_value["config"], | ||
organization_id=validated_value["organization_id"], | ||
environment_id=validated_value.get("environment_id"), | ||
when_condition_group=when_condition_group, | ||
) | ||
|
||
# TODO -- can we bulk create: actions, dcga's and the workflow dcg? | ||
# Create actions and action filters, then associate them to the workflow | ||
for action_filter in validated_value["action_filters"]: | ||
actions, condition_group = self._split_action_and_condition_group(action_filter) | ||
new_condition_group = condition_group_validator.create(condition_group) | ||
|
||
# Connect the condition group to the workflow | ||
WorkflowDataConditionGroup.objects.create( | ||
condition_group=new_condition_group, | ||
workflow=workflow, | ||
) | ||
|
||
for action in actions: | ||
new_action = action_validator.create(action) | ||
DataConditionGroupAction.objects.create( | ||
action=new_action, | ||
condition_group=new_condition_group, | ||
) | ||
Comment on lines
+83
to
+93
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. super nit: can maybe bulk create the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll add a todo to come back and optimize this / check if there's a better way to bulk create. My expectation would be that there's only one or two of these per workflow, so it's probably okay for a bit. |
||
|
||
# TODO - Fix after adding contexts | ||
# create_audit_entry( | ||
# request=self.context["request"], | ||
# organization=self.context["organization_id"], | ||
# target_object=workflow.id, | ||
# event=audit_log.get_event_id("WORKFLOW_ADD"), | ||
# data=workflow.get_audit_log_data(), | ||
# ) | ||
|
||
return workflow |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i suppose the
DataCondition
will error if this isn't set and we try to create the object, sincecondition_group_id
is required. do we make it optional because we only create these inside ofDataConditionGroupValidator
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes - that way the API doesn't need to set the condition_group_id on every request -- also it won't have the DCG id when we initially make a workflow - so if we re-use those validators it'll fail there as well.