77from prefect .client .orchestration import PrefectClient
88from prefect .client .schemas .filters import DeploymentFilter , DeploymentFilterName
99
10- from .catalogue import triggers
11- from .constants import DEPRECATED_STATIC_TRIGGER_NAMES
10+ from infrahub .trigger .models import TriggerDefinition
11+
12+ # from .catalogue import triggers
1213from .models import TriggerType
1314
1415if TYPE_CHECKING :
1516 from uuid import UUID
1617
1718
18- @task (name = "trigger-setup" , task_run_name = "Setup triggers in task-manager" , cache_policy = NONE ) # type: ignore[arg-type]
19- async def setup_triggers (client : PrefectClient ) -> None :
19+ @task (name = "trigger-setup" , task_run_name = "Setup triggers of type {trigger_type.value}" , cache_policy = NONE ) # type: ignore[arg-type]
20+ async def setup_triggers (
21+ client : PrefectClient ,
22+ triggers : list [TriggerDefinition ],
23+ trigger_type : TriggerType = TriggerType .BUILTIN ,
24+ deprecated_triggers : list [str ] | None = None ,
25+ ) -> None :
2026 log = get_run_logger ()
2127
2228 # -------------------------------------------------------------
@@ -32,15 +38,15 @@ async def setup_triggers(client: PrefectClient) -> None:
3238 deployments_mapping : dict [str , UUID ] = {name : item .id for name , item in deployments .items ()}
3339 existing_automations = {item .name : item for item in await client .read_automations ()}
3440
35- builtin_automations = [
36- item .name for item in await client .read_automations () if item .name .startswith (TriggerType . BUILTIN .value )
41+ trigger_automations = [
42+ item .name for item in await client .read_automations () if item .name .startswith (trigger_type .value )
3743 ]
3844 trigger_names = [trigger .generate_name () for trigger in triggers ]
3945
40- _ , to_delete , _ = compare_lists ( list1 = builtin_automations , list2 = trigger_names )
46+ to_delete = set ( trigger_automations ) - set ( trigger_names )
4147
4248 # -------------------------------------------------------------
43- # Create or Update all builtin triggers
49+ # Create or Update all triggers
4450 # -------------------------------------------------------------
4551 for trigger in triggers :
4652 automation = AutomationCore (
@@ -61,7 +67,7 @@ async def setup_triggers(client: PrefectClient) -> None:
6167 log .info (f"{ trigger .name } Created" )
6268
6369 # -------------------------------------------------------------
64- # Delete Builtin Triggers that shouldn't be there
70+ # Delete Triggers that shouldn't be there
6571 # -------------------------------------------------------------
6672 for item_to_delete in to_delete :
6773 existing_automation = existing_automations .get (item_to_delete )
@@ -75,11 +81,12 @@ async def setup_triggers(client: PrefectClient) -> None:
7581 # -------------------------------------------------------------
7682 # Delete Deprecated triggers
7783 # -------------------------------------------------------------
78- for trigger_name in DEPRECATED_STATIC_TRIGGER_NAMES :
79- existing_automation = existing_automations .get (trigger_name )
84+ if deprecated_triggers :
85+ for trigger_name in deprecated_triggers :
86+ existing_automation = existing_automations .get (trigger_name )
8087
81- if not existing_automation :
82- continue
88+ if not existing_automation :
89+ continue
8390
84- await client .delete_automation (automation_id = existing_automation .id )
85- log .info (f"{ trigger_name } Deleted" )
91+ await client .delete_automation (automation_id = existing_automation .id )
92+ log .info (f"{ trigger_name } Deleted" )
0 commit comments