Skip to content

Commit

Permalink
feat(aci): add frequency as optional key in workflow config schema (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
cathteng authored Jan 2, 2025
1 parent 31b9b6a commit 3800216
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 25 deletions.
7 changes: 6 additions & 1 deletion src/sentry/testutils/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -2113,13 +2113,18 @@ def create_dashboard_widget_query(
def create_workflow(
name: str | None = None,
organization: Organization | None = None,
config: dict[str, Any] | None = None,
**kwargs,
) -> Workflow:
if organization is None:
organization = Factories.create_organization()
if name is None:
name = petname.generate(2, " ", letters=10).title()
return Workflow.objects.create(organization=organization, name=name, **kwargs)
if config is None:
config = {}
return Workflow.objects.create(
organization=organization, name=name, config=config, **kwargs
)

@staticmethod
@assume_test_silo_mode(SiloMode.REGION)
Expand Down
1 change: 1 addition & 0 deletions src/sentry/workflow_engine/migration_helpers/alert_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def create_workflow(
when_condition_group=data_condition_group,
enabled=True,
created_by_id=user.id if user else None,
config={},
)


Expand Down
15 changes: 13 additions & 2 deletions src/sentry/workflow_engine/models/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,19 @@ class Workflow(DefaultFieldsModel, OwnerModel, JSONConfigBase):

@property
def config_schema(self) -> dict[str, Any]:
# TODO: fill in
return {}
return {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Workflow Schema",
"type": "object",
"properties": {
"frequency": {
"description": "How often the workflow should fire for a Group (minutes)",
"type": "integer",
"minimum": 0,
},
},
"additionalProperties": False,
}

__repr__ = sane_repr("name", "organization_id")

Expand Down
36 changes: 14 additions & 22 deletions tests/sentry/workflow_engine/models/test_json_config_base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from dataclasses import dataclass
from unittest.mock import PropertyMock, patch

import pytest
from jsonschema import ValidationError
Expand All @@ -19,17 +18,6 @@ def setUp(self):
"location": "Cityville",
"interests": ["Travel", "Technology"],
}

@dataclass(frozen=True)
class TestGroupType(GroupType):
type_id = 1
slug = "test"
description = "Test"
category = GroupCategory.ERROR.value
detector_config_schema = self.example_schema

@pytest.fixture(autouse=True)
def initialize_configs(self):
self.example_schema = {
"$id": "https://example.com/user-profile.schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
Expand All @@ -45,15 +33,14 @@ def initialize_configs(self):
"interests": {"type": "array", "items": {"type": "string"}},
},
}
with (
patch(
"sentry.workflow_engine.models.Workflow.config_schema",
return_value=self.example_schema,
new_callable=PropertyMock,
),
):
# Run test case
yield

@dataclass(frozen=True)
class TestGroupType(GroupType):
type_id = 1
slug = "test"
description = "Test"
category = GroupCategory.ERROR.value
detector_config_schema = self.example_schema


class TestDetectorConfig(TestJsonConfigBase):
Expand All @@ -75,8 +62,13 @@ def test_workflow_mismatched_schema(self):
self.create_workflow(
organization=self.organization, name="test_workflow", config={"hi": "there"}
)
with pytest.raises(ValidationError):
self.create_workflow(
organization=self.organization, name="test_workflow", config={"frequency": "-1"}
)

def test_workflow_correct_schema(self):
self.create_workflow(organization=self.organization, name="test_workflow", config={})
self.create_workflow(
organization=self.organization, name="test_workflow", config=self.correct_config
organization=self.organization, name="test_workflow2", config={"frequency": 5}
)

0 comments on commit 3800216

Please sign in to comment.