@@ -119,38 +119,60 @@ async def configure_webhook_all(service: InfrahubServices) -> None:
119119 log .info (f"{ len (triggers )} Webhooks automation configuration completed" )
120120
121121
122- @flow (name = "webhook-setup-automation-one" , flow_run_name = "Configuration webhook automation for one webhook" )
123- async def configure_webhook_one (event_type : str , event_data : dict , service : InfrahubServices ) -> None :
122+ @flow (name = "webhook-setup-automation-one" , flow_run_name = "Configuration webhook automation for {webhook_name}" )
123+ async def configure_webhook_one (
124+ webhook_name : str , # noqa: ARG001
125+ event_data : dict ,
126+ service : InfrahubServices ,
127+ ) -> None :
124128 log = get_run_logger ()
125129
126130 webhook = await service .client .get (kind = CoreWebhook , id = event_data ["node_id" ])
127131 trigger = WebhookTriggerDefinition .from_object (webhook )
128132
129- delete_automation : bool = "infrahub.node.deleted" in event_type
130-
131133 async with get_client (sync_client = False ) as prefect_client :
132134 # Query the deployment associated with the trigger to have its ID
133135 deployment_name = trigger .get_deployment_names ()[0 ]
134136 deployment = await prefect_client .read_deployment_by_name (name = f"{ deployment_name } /{ deployment_name } " )
135137
136138 automation = AutomationCore (
137139 name = trigger .generate_name (),
138- description = trigger .description ,
140+ description = trigger .get_description () ,
139141 enabled = True ,
140142 trigger = trigger .trigger .get_prefect (),
141143 actions = [action .get (deployment .id ) for action in trigger .actions ],
142144 )
143145
144146 existing_automations = await prefect_client .read_automations_by_name (trigger .generate_name ())
147+ existing_automation = existing_automations [0 ] if existing_automations else None
145148
146- if existing_automations and not delete_automation :
147- existing_automation = existing_automations [0 ]
149+ if existing_automation :
148150 await prefect_client .update_automation (automation_id = existing_automation .id , automation = automation )
149151 log .info (f"Automation { trigger .generate_name ()} updated" )
150- elif existing_automations and delete_automation :
151- existing_automation = existing_automations [0 ]
152- await prefect_client .delete_automation (automation_id = existing_automation .id )
153- log .info (f"Automation { trigger .generate_name ()} deleted" )
154152 else :
155153 await prefect_client .create_automation (automation = automation )
156154 log .info (f"Automation { trigger .generate_name ()} created" )
155+
156+ await service .cache .delete (key = f"webhook:{ webhook .id } " )
157+
158+
159+ @flow (name = "webhook-delete-automation" , flow_run_name = "Delete webhook automation for {webhook_name}" )
160+ async def delete_webhook_automation (
161+ webhook_id : str ,
162+ webhook_name : str , # noqa: ARG001
163+ event_data : dict , # noqa: ARG001
164+ service : InfrahubServices ,
165+ ) -> None :
166+ log = get_run_logger ()
167+
168+ async with get_client (sync_client = False ) as prefect_client :
169+ automation_name = WebhookTriggerDefinition .generate_name_from_id (id = webhook_id )
170+
171+ existing_automations = await prefect_client .read_automations_by_name (automation_name )
172+ existing_automation = existing_automations [0 ] if existing_automations else None
173+
174+ if existing_automation :
175+ await prefect_client .delete_automation (automation_id = existing_automation .id )
176+ log .info (f"Automation { automation_name } deleted" )
177+
178+ await service .cache .delete (key = f"webhook:{ webhook_id } " )
0 commit comments