11from  __future__ import  annotations 
22
3- from  typing  import  TYPE_CHECKING , Any , Optional , Union 
3+ import  warnings 
4+ from  typing  import  TYPE_CHECKING , Any , Literal , Optional , Union , overload 
45from  urllib .parse  import  urlencode 
56
67from  pydantic  import  BaseModel 
@@ -72,13 +73,42 @@ class InfrahubBranchManager(InfraHubBranchManagerBase):
7273    def  __init__ (self , client : InfrahubClient ):
7374        self .client  =  client 
7475
76+     @overload  
7577    async  def  create (
7678        self ,
7779        branch_name : str ,
7880        sync_with_git : bool  =  True ,
7981        description : str  =  "" ,
80-         background_execution : bool  =  False ,
81-     ) ->  BranchData :
82+         wait_until_completion : Literal [True ] =  True ,
83+         background_execution : Optional [bool ] =  False ,
84+     ) ->  BranchData : ...
85+ 
86+     @overload  
87+     async  def  create (
88+         self ,
89+         branch_name : str ,
90+         sync_with_git : bool  =  True ,
91+         description : str  =  "" ,
92+         wait_until_completion : Literal [False ] =  False ,
93+         background_execution : Optional [bool ] =  False ,
94+     ) ->  str : ...
95+ 
96+     async  def  create (
97+         self ,
98+         branch_name : str ,
99+         sync_with_git : bool  =  True ,
100+         description : str  =  "" ,
101+         wait_until_completion : bool  =  True ,
102+         background_execution : Optional [bool ] =  False ,
103+     ) ->  Union [BranchData , str ]:
104+         if  background_execution  is  not   None :
105+             warnings .warn (
106+                 "`background_execution` is deprecated, please use `wait_until_completion` instead." ,
107+                 DeprecationWarning ,
108+                 stacklevel = 1 ,
109+             )
110+ 
111+         background_execution  =  background_execution  or  not  wait_until_completion 
82112        input_data  =  {
83113            "background_execution" : background_execution ,
84114            "data" : {
@@ -91,6 +121,10 @@ async def create(
91121        query  =  Mutation (mutation = "BranchCreate" , input_data = input_data , query = MUTATION_QUERY_DATA )
92122        response  =  await  self .client .execute_graphql (query = query .render (), tracker = "mutation-branch-create" )
93123
124+         # Make sure server version is recent enough to support background execution, as previously 
125+         # using background_execution=True had no effect. 
126+         if  not  wait_until_completion  and  "task"  in  response ["BranchCreate" ]:
127+             return  BranchData (** response ["BranchCreate" ]["task" ]["id" ])
94128        return  BranchData (** response ["BranchCreate" ]["object" ])
95129
96130    async  def  delete (self , branch_name : str ) ->  bool :
@@ -209,13 +243,42 @@ def get(self, branch_name: str) -> BranchData:
209243            raise  BranchNotFoundError (identifier = branch_name )
210244        return  BranchData (** data ["Branch" ][0 ])
211245
246+     @overload  
247+     def  create (
248+         self ,
249+         branch_name : str ,
250+         sync_with_git : bool  =  True ,
251+         description : str  =  "" ,
252+         wait_until_completion : Literal [True ] =  True ,
253+         background_execution : Optional [bool ] =  False ,
254+     ) ->  BranchData : ...
255+ 
256+     @overload  
257+     def  create (
258+         self ,
259+         branch_name : str ,
260+         sync_with_git : bool  =  True ,
261+         description : str  =  "" ,
262+         wait_until_completion : Literal [False ] =  False ,
263+         background_execution : Optional [bool ] =  False ,
264+     ) ->  str : ...
265+ 
212266    def  create (
213267        self ,
214268        branch_name : str ,
215269        sync_with_git : bool  =  True ,
216270        description : str  =  "" ,
217-         background_execution : bool  =  False ,
218-     ) ->  BranchData :
271+         wait_until_completion : bool  =  True ,
272+         background_execution : Optional [bool ] =  False ,
273+     ) ->  Union [BranchData , str ]:
274+         if  background_execution  is  not   None :
275+             warnings .warn (
276+                 "`background_execution` is deprecated, please use `wait_until_completion` instead." ,
277+                 DeprecationWarning ,
278+                 stacklevel = 1 ,
279+             )
280+ 
281+         background_execution  =  background_execution  or  not  wait_until_completion 
219282        input_data  =  {
220283            "background_execution" : background_execution ,
221284            "data" : {
@@ -228,6 +291,10 @@ def create(
228291        query  =  Mutation (mutation = "BranchCreate" , input_data = input_data , query = MUTATION_QUERY_DATA )
229292        response  =  self .client .execute_graphql (query = query .render (), tracker = "mutation-branch-create" )
230293
294+         # Make sure server version is recent enough to support background execution, as previously 
295+         # using background_execution=True had no effect. 
296+         if  not  wait_until_completion  and  "task"  in  response ["BranchCreate" ]:
297+             return  BranchData (** response ["BranchCreate" ]["task" ]["id" ])
231298        return  BranchData (** response ["BranchCreate" ]["object" ])
232299
233300    def  delete (self , branch_name : str ) ->  bool :
0 commit comments