Skip to content

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

Merged
merged 11 commits into from
Apr 16, 2025

Conversation

saponifi3d
Copy link
Contributor

Description

Add the .create method to data condition, data condition group, and workflow validators. This allows us to validate and create objects safely. This will also safely create items in an atomic transaction -- bubbling up to the workflow layer.

@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Apr 16, 2025
@saponifi3d saponifi3d marked this pull request as ready for review April 16, 2025 16:57
@saponifi3d saponifi3d requested a review from a team as a code owner April 16, 2025 16:57
Copy link

codecov bot commented Apr 16, 2025

Codecov Report

Attention: Patch coverage is 98.71795% with 2 lines in your changes missing coverage. Please review.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
...kflow_engine/endpoints/validators/base/workflow.py 94.11% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##           master   #89769    +/-   ##
========================================
  Coverage   85.47%   85.47%            
========================================
  Files       10194    10194            
  Lines      574925   575066   +141     
  Branches    22643    22643            
========================================
+ Hits       491432   491559   +127     
- Misses      82898    82912    +14     
  Partials      595      595            

@@ -26,7 +26,7 @@ class AbstractDataConditionValidator(
type = serializers.ChoiceField(choices=[(t.value, t.value) for t in Condition])
comparison = serializers.JSONField(required=True)
condition_result = serializers.JSONField(required=True)
condition_group_id = serializers.IntegerField(required=True)
condition_group_id = serializers.IntegerField(required=False)
Copy link
Member

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, since condition_group_id is required. do we make it optional because we only create these inside of DataConditionGroupValidator?

Copy link
Contributor Author

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.

Comment on lines 95 to 101
create_audit_entry(
request=self.context["request"],
organization=validated_value["organization_id"],
target_object=workflow.id,
event=audit_log.get_event_id("WORKFLOW_ADD"),
data=workflow.get_audit_log_data(),
)
Copy link
Member

Choose a reason for hiding this comment

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

does this need to be unindented? it's currently in the for loop going through the action_filters

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 (this is why i don't like whitespace based languages 🤣)

return value

def create(self, validated_value: dict[str, Any]) -> Workflow:
condition_group_validator = BaseDataConditionGroupValidator()
Copy link
Member

Choose a reason for hiding this comment

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

should the Workflow validator automatically convert DataConditionGroup.logic_type of ANY into ANY_SHORT_CIRCUIT? there's some discussion here about how to handle it in the FE #89233 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 type but it would add some artificial constraints that i'm not sure i agree with.

we should not convert any to any_short_circuit though as they are specifically used in different places for different reasons.

@saponifi3d saponifi3d requested a review from cathteng April 16, 2025 19:25
Copy link
Member

@cathteng cathteng left a comment

Choose a reason for hiding this comment

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

ty c:

Comment on lines +82 to +92
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,
)
Copy link
Member

Choose a reason for hiding this comment

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

super nit: can maybe bulk create the WorkflowDataConditionGroups and DataConditionGroupActions but idk how much time that would actually save

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

@saponifi3d saponifi3d enabled auto-merge (squash) April 16, 2025 19:43
@saponifi3d saponifi3d merged commit 6c5b27f into master Apr 16, 2025
63 checks passed
@saponifi3d saponifi3d deleted the jcallender/aci/creation-in-validators branch April 16, 2025 20:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Scope: Backend Automatically applied to PRs that change backend components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants