66from typing import TYPE_CHECKING , Any , Optional , Union
77from uuid import UUID , uuid4
88
9- from infrahub_sdk .protocols import CoreCustomWebhook , CoreStandardWebhook , CoreTransformPython , CoreWebhook
109from pydantic import BaseModel , ConfigDict , Field , computed_field
1110from typing_extensions import Self
1211
1312from infrahub .core import registry
1413from infrahub .core .constants import InfrahubKind
1514from infrahub .core .timestamp import Timestamp
1615from infrahub .git .repository import InfrahubReadOnlyRepository , InfrahubRepository
17- from infrahub .transformations .constants import DEFAULT_TRANSFORM_TIMEOUT
1816from infrahub .trigger .models import EventTrigger , ExecuteWorkflow , TriggerDefinition , TriggerType
1917from infrahub .workflows .catalogue import WEBHOOK_PROCESS
2018
2119if TYPE_CHECKING :
2220 from httpx import Response
21+ from infrahub_sdk .protocols import CoreCustomWebhook , CoreStandardWebhook , CoreTransformPython , CoreWebhook
2322
2423 from infrahub .services import InfrahubServices
2524
@@ -79,15 +78,17 @@ class EventContext(BaseModel):
7978
8079 @classmethod
8180 def from_event (cls , event_id : str , event_type : str , event_occured_at : str , event_payload : dict [str , Any ]) -> Self :
82- """Extract the context from the raw eventwe are getting from Prefect."""
81+ """Extract the context from the raw event we are getting from Prefect."""
8382
8483 infrahub_context : dict [str , Any ] = event_payload .get ("context" , {})
8584 account_info : dict [str , Any ] = infrahub_context .get ("account" , {})
8685 branch_info : dict [str , Any ] = infrahub_context .get ("branch" , {})
8786
8887 return cls (
8988 id = event_id ,
90- branch = branch_info .get ("name" ) if branch_info else None ,
89+ branch = branch_info .get ("name" )
90+ if branch_info and branch_info .get ("name" ) != registry .get_global_branch ().name
91+ else None ,
9192 account_id = account_info .get ("account_id" ),
9293 occured_at = event_occured_at ,
9394 event = event_type ,
@@ -114,10 +115,16 @@ def _assign_headers(self) -> None:
114115 def webhook_type (self ) -> str :
115116 return self .__class__ .__name__
116117
117- async def send (self , data : dict [str , Any ], context : EventContext , service : InfrahubServices ) -> Response :
118+ async def prepare (self , data : dict [str , Any ], context : EventContext , service : InfrahubServices ) -> None :
118119 await self ._prepare_payload (data = data , context = context , service = service )
119120 self ._assign_headers ()
120- return await service .http .post (url = self .url , json = self ._payload , headers = self ._headers )
121+
122+ async def send (self , data : dict [str , Any ], context : EventContext , service : InfrahubServices ) -> Response :
123+ await self .prepare (data = data , context = context , service = service )
124+ return await service .http .post (url = self .url , json = self .get_payload (), headers = self ._headers )
125+
126+ def get_payload (self ) -> dict [str , Any ]:
127+ return self ._payload
121128
122129 def to_cache (self ) -> dict [str , Any ]:
123130 return self .model_dump ()
@@ -179,6 +186,7 @@ class TransformWebhook(Webhook):
179186 transform_name : str = Field (...)
180187 transform_class : str = Field (...)
181188 transform_file : str = Field (...)
189+ transform_timeout : int = Field (...)
182190
183191 async def _prepare_payload (self , data : dict [str , Any ], context : EventContext , service : InfrahubServices ) -> None :
184192 repo : Union [InfrahubReadOnlyRepository , InfrahubRepository ]
@@ -197,17 +205,11 @@ async def _prepare_payload(self, data: dict[str, Any], context: EventContext, se
197205 service = service ,
198206 )
199207
200- default_branch = repo .default_branch
201- commit = repo .get_commit_value (branch_name = default_branch )
202-
203- timeout = DEFAULT_TRANSFORM_TIMEOUT
204- if transform := await service .client .get (
205- kind = CoreTransformPython , name__value = self .transform_name , raise_when_missing = False
206- ):
207- timeout = transform .timeout .value
208+ branch = context .branch or repo .default_branch
209+ commit = repo .get_commit_value (branch_name = branch )
208210
209- self ._payload = await repo .execute_python_transform .with_options (timeout_seconds = timeout )(
210- branch_name = default_branch ,
211+ self ._payload = await repo .execute_python_transform .with_options (timeout_seconds = self . transform_timeout )(
212+ branch_name = branch ,
211213 commit = commit ,
212214 location = f"{ self .transform_file } ::{ self .transform_class } " ,
213215 data = {"data" : data , ** context .model_dump ()},
@@ -227,4 +229,5 @@ def from_object(cls, obj: CoreCustomWebhook, transform: CoreTransformPython) ->
227229 transform_name = transform .name .value ,
228230 transform_class = transform .class_name .value ,
229231 transform_file = transform .file_path .value ,
232+ transform_timeout = transform .timeout .value ,
230233 )
0 commit comments