Skip to content

Commit bb63b82

Browse files
committed
Add wait_until_completion to InfrahubBranchManager.create
1 parent a0afaa0 commit bb63b82

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

infrahub_sdk/branch.py

Lines changed: 38 additions & 4 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,15 +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+
wait_until_completion = wait_until_completion or not background_execution
82112
input_data = {
83-
"background_execution": background_execution,
113+
"wait_until_completion": wait_until_completion,
84114
"data": {
85115
"name": branch_name,
86116
"description": description,
@@ -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:

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)

0 commit comments

Comments
 (0)