1
1
from __future__ import annotations
2
2
3
- from typing import TYPE_CHECKING , Any , Optional , Union
3
+ import warnings
4
+ from typing import TYPE_CHECKING , Any , Literal , Optional , Union , overload
4
5
from urllib .parse import urlencode
5
6
6
7
from pydantic import BaseModel
@@ -72,14 +73,44 @@ class InfrahubBranchManager(InfraHubBranchManagerBase):
72
73
def __init__ (self , client : InfrahubClient ):
73
74
self .client = client
74
75
76
+ @overload
75
77
async def create (
76
78
self ,
77
79
branch_name : str ,
78
80
sync_with_git : bool = True ,
79
81
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
82
112
input_data = {
113
+ # Should be switched to `wait_until_completion` once `background_execution` is removed server side.
83
114
"background_execution" : background_execution ,
84
115
"data" : {
85
116
"name" : branch_name ,
@@ -91,6 +122,10 @@ async def create(
91
122
query = Mutation (mutation = "BranchCreate" , input_data = input_data , query = MUTATION_QUERY_DATA )
92
123
response = await self .client .execute_graphql (query = query .render (), tracker = "mutation-branch-create" )
93
124
125
+ # Make sure server version is recent enough to support background execution, as previously
126
+ # using background_execution=True had no effect.
127
+ if background_execution and "task" in response ["BranchCreate" ]:
128
+ return BranchData (** response ["BranchCreate" ]["task" ]["id" ])
94
129
return BranchData (** response ["BranchCreate" ]["object" ])
95
130
96
131
async def delete (self , branch_name : str ) -> bool :
@@ -209,14 +244,44 @@ def get(self, branch_name: str) -> BranchData:
209
244
raise BranchNotFoundError (identifier = branch_name )
210
245
return BranchData (** data ["Branch" ][0 ])
211
246
247
+ @overload
248
+ def create (
249
+ self ,
250
+ branch_name : str ,
251
+ sync_with_git : bool = True ,
252
+ description : str = "" ,
253
+ wait_until_completion : Literal [True ] = True ,
254
+ background_execution : Optional [bool ] = False ,
255
+ ) -> BranchData : ...
256
+
257
+ @overload
258
+ def create (
259
+ self ,
260
+ branch_name : str ,
261
+ sync_with_git : bool = True ,
262
+ description : str = "" ,
263
+ wait_until_completion : Literal [False ] = False ,
264
+ background_execution : Optional [bool ] = False ,
265
+ ) -> str : ...
266
+
212
267
def create (
213
268
self ,
214
269
branch_name : str ,
215
270
sync_with_git : bool = True ,
216
271
description : str = "" ,
217
- background_execution : bool = False ,
218
- ) -> BranchData :
272
+ wait_until_completion : bool = True ,
273
+ background_execution : Optional [bool ] = False ,
274
+ ) -> Union [BranchData , str ]:
275
+ if background_execution is not None :
276
+ warnings .warn (
277
+ "`background_execution` is deprecated, please use `wait_until_completion` instead." ,
278
+ DeprecationWarning ,
279
+ stacklevel = 1 ,
280
+ )
281
+
282
+ background_execution = background_execution or not wait_until_completion
219
283
input_data = {
284
+ # Should be switched to `wait_until_completion` once `background_execution` is removed server side.
220
285
"background_execution" : background_execution ,
221
286
"data" : {
222
287
"name" : branch_name ,
@@ -228,6 +293,10 @@ def create(
228
293
query = Mutation (mutation = "BranchCreate" , input_data = input_data , query = MUTATION_QUERY_DATA )
229
294
response = self .client .execute_graphql (query = query .render (), tracker = "mutation-branch-create" )
230
295
296
+ # Make sure server version is recent enough to support background execution, as previously
297
+ # using background_execution=True had no effect.
298
+ if background_execution and "task" in response ["BranchCreate" ]:
299
+ return BranchData (** response ["BranchCreate" ]["task" ]["id" ])
231
300
return BranchData (** response ["BranchCreate" ]["object" ])
232
301
233
302
def delete (self , branch_name : str ) -> bool :
0 commit comments