11from typing import TYPE_CHECKING
22
3- from infrahub_sdk .utils import compare_lists
43from prefect import get_run_logger , task
54from prefect .automations import AutomationCore
65from prefect .cache_policies import NONE
76from prefect .client .orchestration import PrefectClient
87from prefect .client .schemas .filters import DeploymentFilter , DeploymentFilterName
98
10- from .catalogue import triggers
11- from .constants import DEPRECATED_STATIC_TRIGGER_NAMES
9+ from infrahub .trigger .models import TriggerDefinition
10+
11+ # from .catalogue import triggers
1212from .models import TriggerType
1313
1414if TYPE_CHECKING :
1515 from uuid import UUID
1616
1717
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 :
18+ @task (name = "trigger-setup" , task_run_name = "Setup triggers of type {trigger_type.value}" , cache_policy = NONE ) # type: ignore[arg-type]
19+ async def setup_triggers (
20+ client : PrefectClient ,
21+ triggers : list [TriggerDefinition ],
22+ trigger_type : TriggerType = TriggerType .BUILTIN ,
23+ deprecated_triggers : list [str ] | None = None ,
24+ ) -> None :
2025 log = get_run_logger ()
2126
2227 # -------------------------------------------------------------
@@ -32,15 +37,15 @@ async def setup_triggers(client: PrefectClient) -> None:
3237 deployments_mapping : dict [str , UUID ] = {name : item .id for name , item in deployments .items ()}
3338 existing_automations = {item .name : item for item in await client .read_automations ()}
3439
35- builtin_automations = [
36- item .name for item in await client .read_automations () if item .name .startswith (TriggerType . BUILTIN .value )
40+ trigger_automations = [
41+ item .name for item in await client .read_automations () if item .name .startswith (trigger_type .value )
3742 ]
3843 trigger_names = [trigger .generate_name () for trigger in triggers ]
3944
40- _ , to_delete , _ = compare_lists ( list1 = builtin_automations , list2 = trigger_names )
45+ to_delete = set ( trigger_automations ) - set ( trigger_names )
4146
4247 # -------------------------------------------------------------
43- # Create or Update all builtin triggers
48+ # Create or Update all triggers
4449 # -------------------------------------------------------------
4550 for trigger in triggers :
4651 automation = AutomationCore (
@@ -61,7 +66,7 @@ async def setup_triggers(client: PrefectClient) -> None:
6166 log .info (f"{ trigger .name } Created" )
6267
6368 # -------------------------------------------------------------
64- # Delete Builtin Triggers that shouldn't be there
69+ # Delete Triggers that shouldn't be there
6570 # -------------------------------------------------------------
6671 for item_to_delete in to_delete :
6772 existing_automation = existing_automations .get (item_to_delete )
@@ -75,11 +80,12 @@ async def setup_triggers(client: PrefectClient) -> None:
7580 # -------------------------------------------------------------
7681 # Delete Deprecated triggers
7782 # -------------------------------------------------------------
78- for trigger_name in DEPRECATED_STATIC_TRIGGER_NAMES :
79- existing_automation = existing_automations .get (trigger_name )
83+ if deprecated_triggers :
84+ for trigger_name in deprecated_triggers :
85+ existing_automation = existing_automations .get (trigger_name )
8086
81- if not existing_automation :
82- continue
87+ if not existing_automation :
88+ continue
8389
84- await client .delete_automation (automation_id = existing_automation .id )
85- log .info (f"{ trigger_name } Deleted" )
90+ await client .delete_automation (automation_id = existing_automation .id )
91+ log .info (f"{ trigger_name } Deleted" )
0 commit comments