Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 74 additions & 5 deletions infrahub_sdk/branch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any, Optional, Union
import warnings
from typing import TYPE_CHECKING, Any, Literal, Optional, Union, overload
from urllib.parse import urlencode

from pydantic import BaseModel
Expand Down Expand Up @@ -72,14 +73,44 @@
def __init__(self, client: InfrahubClient):
self.client = client

@overload
async def create(
self,
branch_name: str,
sync_with_git: bool = True,
description: str = "",
background_execution: bool = False,
) -> BranchData:
wait_until_completion: Literal[True] = True,
background_execution: Optional[bool] = False,
) -> BranchData: ...

@overload
async def create(
self,
branch_name: str,
sync_with_git: bool = True,
description: str = "",
wait_until_completion: Literal[False] = False,
background_execution: Optional[bool] = False,
) -> str: ...

async def create(
self,
branch_name: str,
sync_with_git: bool = True,
description: str = "",
wait_until_completion: bool = True,
background_execution: Optional[bool] = False,
) -> Union[BranchData, str]:
if background_execution is not None:
warnings.warn(
"`background_execution` is deprecated, please use `wait_until_completion` instead.",
DeprecationWarning,
stacklevel=1,
)

background_execution = background_execution or not wait_until_completion
input_data = {
# Should be switched to `wait_until_completion` once `background_execution` is removed server side.
"background_execution": background_execution,
"data": {
"name": branch_name,
Expand All @@ -91,6 +122,10 @@
query = Mutation(mutation="BranchCreate", input_data=input_data, query=MUTATION_QUERY_DATA)
response = await self.client.execute_graphql(query=query.render(), tracker="mutation-branch-create")

# Make sure server version is recent enough to support background execution, as previously
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if this is so useful because if the server doesn't support wait_until_completion it will blow up when you execute the query 2 lines up

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed, let's use deprecated background_execution as query input field so query does not break, and we will switch to wait_until_completion the day background_execution is completely removed from the graphql query.

# using background_execution=True had no effect.
if background_execution and "task" in response["BranchCreate"]:
return BranchData(**response["BranchCreate"]["task"]["id"])

Check warning on line 128 in infrahub_sdk/branch.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/branch.py#L128

Added line #L128 was not covered by tests
return BranchData(**response["BranchCreate"]["object"])

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

@overload
def create(
self,
branch_name: str,
sync_with_git: bool = True,
description: str = "",
wait_until_completion: Literal[True] = True,
background_execution: Optional[bool] = False,
) -> BranchData: ...

@overload
def create(
self,
branch_name: str,
sync_with_git: bool = True,
description: str = "",
wait_until_completion: Literal[False] = False,
background_execution: Optional[bool] = False,
) -> str: ...

def create(
self,
branch_name: str,
sync_with_git: bool = True,
description: str = "",
background_execution: bool = False,
) -> BranchData:
wait_until_completion: bool = True,
background_execution: Optional[bool] = False,
) -> Union[BranchData, str]:
if background_execution is not None:
warnings.warn(

Check warning on line 276 in infrahub_sdk/branch.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/branch.py#L276

Added line #L276 was not covered by tests
"`background_execution` is deprecated, please use `wait_until_completion` instead.",
DeprecationWarning,
stacklevel=1,
)

background_execution = background_execution or not wait_until_completion

Check warning on line 282 in infrahub_sdk/branch.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/branch.py#L282

Added line #L282 was not covered by tests
input_data = {
# Should be switched to `wait_until_completion` once `background_execution` is removed server side.
"background_execution": background_execution,
"data": {
"name": branch_name,
Expand All @@ -228,6 +293,10 @@
query = Mutation(mutation="BranchCreate", input_data=input_data, query=MUTATION_QUERY_DATA)
response = self.client.execute_graphql(query=query.render(), tracker="mutation-branch-create")

# Make sure server version is recent enough to support background execution, as previously
# using background_execution=True had no effect.
if background_execution and "task" in response["BranchCreate"]:
return BranchData(**response["BranchCreate"]["task"]["id"])

Check warning on line 299 in infrahub_sdk/branch.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/branch.py#L299

Added line #L299 was not covered by tests
return BranchData(**response["BranchCreate"]["object"])

def delete(self, branch_name: str) -> bool:
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/test_infrahub_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from infrahub.server import app

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

obj1 = await client.get(kind="BuiltinStatus", id=obj.id)
assert obj1.description.value == "description in profile"

async def test_create_branch(self, client: InfrahubClient, db: InfrahubDatabase, init_db_base, base_dataset):
branch = await client.branch.create(branch_name="new-branch-1")
assert isinstance(branch, BranchData)
assert branch.id is not None

async def test_create_branch_async(self, client: InfrahubClient, db: InfrahubDatabase, init_db_base, base_dataset):
task_id = await client.branch.create(branch_name="new-branch-2", wait_until_completion=False)
assert isinstance(task_id, str)
10 changes: 10 additions & 0 deletions tests/integration/test_infrahub_client_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from infrahub.server import app

from infrahub_sdk import Config, InfrahubClientSync
from infrahub_sdk.branch import BranchData
from infrahub_sdk.constants import InfrahubClientMode
from infrahub_sdk.exceptions import BranchNotFoundError
from infrahub_sdk.node import InfrahubNodeSync
Expand Down Expand Up @@ -285,3 +286,12 @@ def test_profile(self, client: InfrahubClientSync, db: InfrahubDatabase, init_db

obj1 = client.get(kind="BuiltinStatus", id=obj.id)
assert obj1.description.value == "description in profile"

def test_create_branch(self, client: InfrahubClientSync, db: InfrahubDatabase, init_db_base, base_dataset):
branch = client.branch.create(branch_name="new-branch-1")
assert isinstance(branch, BranchData)
assert branch.id is not None

def test_create_branch_async(self, client: InfrahubClientSync, db: InfrahubDatabase, init_db_base, base_dataset):
task_id = client.branch.create(branch_name="new-branch-2", wait_until_completion=False)
assert isinstance(task_id, str)