Skip to content

Commit 761a1c7

Browse files
authored
Merge pull request #142 from opsmill/lgu-add-wait-until-completion
Add wait_until_completion to InfrahubBranchManager.create
2 parents a0afaa0 + 9105436 commit 761a1c7

File tree

3 files changed

+94
-5
lines changed

3 files changed

+94
-5
lines changed

infrahub_sdk/branch.py

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __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
45
from urllib.parse import urlencode
56

67
from pydantic import BaseModel
@@ -72,14 +73,44 @@ 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 = {
113+
# Should be switched to `wait_until_completion` once `background_execution` is removed server side.
83114
"background_execution": background_execution,
84115
"data": {
85116
"name": branch_name,
@@ -91,6 +122,10 @@ async def create(
91122
query = Mutation(mutation="BranchCreate", input_data=input_data, query=MUTATION_QUERY_DATA)
92123
response = await self.client.execute_graphql(query=query.render(), tracker="mutation-branch-create")
93124

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"])
94129
return BranchData(**response["BranchCreate"]["object"])
95130

96131
async def delete(self, branch_name: str) -> bool:
@@ -209,14 +244,44 @@ def get(self, branch_name: str) -> BranchData:
209244
raise BranchNotFoundError(identifier=branch_name)
210245
return BranchData(**data["Branch"][0])
211246

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+
212267
def create(
213268
self,
214269
branch_name: str,
215270
sync_with_git: bool = True,
216271
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
219283
input_data = {
284+
# Should be switched to `wait_until_completion` once `background_execution` is removed server side.
220285
"background_execution": background_execution,
221286
"data": {
222287
"name": branch_name,
@@ -228,6 +293,10 @@ def create(
228293
query = Mutation(mutation="BranchCreate", input_data=input_data, query=MUTATION_QUERY_DATA)
229294
response = self.client.execute_graphql(query=query.render(), tracker="mutation-branch-create")
230295

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"])
231300
return BranchData(**response["BranchCreate"]["object"])
232301

233302
def delete(self, branch_name: str) -> bool:

tests/integration/test_infrahub_client.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from infrahub.server import app
1010

1111
from infrahub_sdk import Config, InfrahubClient
12+
from infrahub_sdk.branch import BranchData
1213
from infrahub_sdk.constants import InfrahubClientMode
1314
from infrahub_sdk.exceptions import BranchNotFoundError
1415
from infrahub_sdk.node import InfrahubNode
@@ -283,3 +284,12 @@ async def test_profile(self, client: InfrahubClient, db: InfrahubDatabase, init_
283284

284285
obj1 = await client.get(kind="BuiltinStatus", id=obj.id)
285286
assert obj1.description.value == "description in profile"
287+
288+
async def test_create_branch(self, client: InfrahubClient, db: InfrahubDatabase, init_db_base, base_dataset):
289+
branch = await client.branch.create(branch_name="new-branch-1")
290+
assert isinstance(branch, BranchData)
291+
assert branch.id is not None
292+
293+
async def test_create_branch_async(self, client: InfrahubClient, db: InfrahubDatabase, init_db_base, base_dataset):
294+
task_id = await client.branch.create(branch_name="new-branch-2", wait_until_completion=False)
295+
assert isinstance(task_id, str)

tests/integration/test_infrahub_client_sync.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from infrahub.server import app
99

1010
from infrahub_sdk import Config, InfrahubClientSync
11+
from infrahub_sdk.branch import BranchData
1112
from infrahub_sdk.constants import InfrahubClientMode
1213
from infrahub_sdk.exceptions import BranchNotFoundError
1314
from infrahub_sdk.node import InfrahubNodeSync
@@ -285,3 +286,12 @@ def test_profile(self, client: InfrahubClientSync, db: InfrahubDatabase, init_db
285286

286287
obj1 = client.get(kind="BuiltinStatus", id=obj.id)
287288
assert obj1.description.value == "description in profile"
289+
290+
def test_create_branch(self, client: InfrahubClientSync, db: InfrahubDatabase, init_db_base, base_dataset):
291+
branch = client.branch.create(branch_name="new-branch-1")
292+
assert isinstance(branch, BranchData)
293+
assert branch.id is not None
294+
295+
def test_create_branch_async(self, client: InfrahubClientSync, db: InfrahubDatabase, init_db_base, base_dataset):
296+
task_id = client.branch.create(branch_name="new-branch-2", wait_until_completion=False)
297+
assert isinstance(task_id, str)

0 commit comments

Comments
 (0)