Skip to content

Commit 9249a6f

Browse files
committed
Add branch parameter to clone methods
1 parent e903010 commit 9249a6f

File tree

5 files changed

+41
-8
lines changed

5 files changed

+41
-8
lines changed

changelog/+398b0883.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added a "branch" parameter to the client.clone() method to allow properly cloning a client that targets another branch.

infrahub_sdk/client.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,11 @@ def _build_ip_prefix_allocation_query(
271271
input_data={"data": input_data},
272272
)
273273

274+
def _clone_config(self, branch: str | None = None) -> Config:
275+
config = copy.deepcopy(self.config)
276+
config.default_branch = branch or config.default_branch
277+
return config
278+
274279

275280
class InfrahubClient(BaseClient):
276281
"""GraphQL Client to interact with Infrahub."""
@@ -847,9 +852,9 @@ async def process_non_batch() -> tuple[list[InfrahubNode], list[InfrahubNode]]:
847852
self.store.set(node=node)
848853
return nodes
849854

850-
def clone(self) -> InfrahubClient:
855+
def clone(self, branch: str | None = None) -> InfrahubClient:
851856
"""Return a cloned version of the client using the same configuration"""
852-
return InfrahubClient(config=self.config)
857+
return InfrahubClient(config=self._clone_config(branch=branch))
853858

854859
async def execute_graphql(
855860
self,
@@ -1591,9 +1596,9 @@ def delete(self, kind: str | type[SchemaTypeSync], id: str, branch: str | None =
15911596
node = InfrahubNodeSync(client=self, schema=schema, branch=branch, data={"id": id})
15921597
node.delete()
15931598

1594-
def clone(self) -> InfrahubClientSync:
1599+
def clone(self, branch: str | None = None) -> InfrahubClientSync:
15951600
"""Return a cloned version of the client using the same configuration"""
1596-
return InfrahubClientSync(config=self.config)
1601+
return InfrahubClientSync(config=self._clone_config(branch=branch))
15971602

15981603
def execute_graphql(
15991604
self,

infrahub_sdk/generator.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ def __init__(
3838
self.params = params or {}
3939
self.root_directory = root_directory or os.getcwd()
4040
self.generator_instance = generator_instance
41-
self._init_client = client.clone()
42-
self._init_client.config.default_branch = self._init_client.default_branch = self.branch_name
43-
self._init_client.store._default_branch = self.branch_name
41+
self._init_client = client.clone(branch=self.branch_name)
4442
self._client: InfrahubClient | None = None
4543
self._nodes: list[InfrahubNode] = []
4644
self._related_nodes: list[InfrahubNode] = []

infrahub_sdk/recorder.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ def record(response: httpx.Response) -> None:
3131
def default(cls) -> NoRecorder:
3232
return cls()
3333

34+
def __eq__(self, other: object) -> bool:
35+
return isinstance(other, NoRecorder)
36+
3437

3538
class JSONRecorder(BaseSettings):
3639
model_config = SettingsConfigDict(env_prefix="INFRAHUB_JSON_RECORDER_")

tests/unit/sdk/test_client.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from infrahub_sdk import InfrahubClient, InfrahubClientSync
77
from infrahub_sdk.exceptions import NodeNotFoundError
88
from infrahub_sdk.node import InfrahubNode, InfrahubNodeSync
9+
from tests.unit.sdk.conftest import BothClients
910

1011
pytestmark = pytest.mark.httpx_mock(can_send_already_matched_responses=True)
1112

@@ -761,12 +762,37 @@ async def test_query_echo(httpx_mock: HTTPXMock, echo_clients, client_type):
761762

762763

763764
@pytest.mark.parametrize("client_type", client_types)
764-
async def test_clone(clients, client_type):
765+
async def test_clone(clients: BothClients, client_type: str) -> None:
766+
"""Validate that the configuration of a cloned client is a replica of the original client"""
765767
if client_type == "standard":
766768
clone = clients.standard.clone()
767769
assert clone.config == clients.standard.config
768770
assert isinstance(clone, InfrahubClient)
771+
assert clients.standard.default_branch == clone.default_branch
769772
else:
770773
clone = clients.sync.clone()
771774
assert clone.config == clients.sync.config
772775
assert isinstance(clone, InfrahubClientSync)
776+
assert clients.sync.default_branch == clone.default_branch
777+
778+
779+
@pytest.mark.parametrize("client_type", client_types)
780+
async def test_clone_define_branch(clients: BothClients, client_type: str) -> None:
781+
"""Validate that the clone branch parameter sets the correct branch of the cloned client"""
782+
clone_branch = "my_other_branch"
783+
if client_type == "standard":
784+
original_branch = clients.standard.default_branch
785+
clone = clients.standard.clone(branch=clone_branch)
786+
assert clients.standard.store._default_branch == original_branch
787+
assert isinstance(clone, InfrahubClient)
788+
assert clients.standard.default_branch != clone.default_branch
789+
else:
790+
original_branch = clients.standard.default_branch
791+
clone = clients.sync.clone(branch="my_other_branch")
792+
assert clients.sync.store._default_branch == original_branch
793+
assert isinstance(clone, InfrahubClientSync)
794+
assert clients.sync.default_branch != clone.default_branch
795+
796+
assert clone.default_branch == clone_branch
797+
assert original_branch != clone_branch
798+
assert clone.store._default_branch == clone_branch

0 commit comments

Comments
 (0)