Skip to content

Commit

Permalink
IFC-248 - Branch parameters in SDK (#4056)
Browse files Browse the repository at this point in the history
* Add several missing branch parameters

---------

Co-authored-by: Guillaume Mazoyer <guillaume@opsmill.com>
  • Loading branch information
BeArchiTek and gmazoyer authored Aug 9, 2024
1 parent 91f0da5 commit 89e2d63
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 13 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ jobs:
globs: |
**/*.{md,mdx}
!changelog/*.md
!python_sdk/changelog/*.md
action-lint:
if: needs.files-changed.outputs.github_workflows == 'true'
Expand Down
4 changes: 3 additions & 1 deletion backend/tests/unit/git/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,9 @@ async def mock_schema_query_01(helper, httpx_mock: HTTPXMock) -> HTTPXMock:
encoding="UTF-8"
)

httpx_mock.add_response(method="GET", url="http://mock/api/schema/?branch=main", json=ujson.loads(response_text))
httpx_mock.add_response(
method="GET", url=re.compile(r"^http://mock/api/schema/\?branch=(main|cr1234)"), json=ujson.loads(response_text)
)
return httpx_mock


Expand Down
1 change: 1 addition & 0 deletions python_sdk/changelog/4056.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix the retrieving on schema and nodes on the right branch
2 changes: 1 addition & 1 deletion python_sdk/infrahub_sdk/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class InfrahubCheck:

def __init__(
self,
branch: str = "",
branch: Optional[str] = None,
root_directory: str = "",
output: Optional[str] = None,
initializer: Optional[InfrahubCheckInitializer] = None,
Expand Down
4 changes: 2 additions & 2 deletions python_sdk/infrahub_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ async def filters(
Returns:
list[InfrahubNodeSync]: List of Nodes that match the given filters.
"""
schema = await self.schema.get(kind=kind)
schema = await self.schema.get(kind=kind, branch=branch)

branch = branch or self.default_branch
if at:
Expand Down Expand Up @@ -1262,7 +1262,7 @@ def filters(
Returns:
list[InfrahubNodeSync]: List of Nodes that match the given filters.
"""
schema = self.schema.get(kind=kind)
schema = self.schema.get(kind=kind, branch=branch)

branch = branch or self.default_branch
if at:
Expand Down
6 changes: 4 additions & 2 deletions python_sdk/infrahub_sdk/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,12 @@ async def process_nodes(self, data: dict) -> None:
for kind in data:
if kind in self._init_client.schema.cache[self.branch_name]:
for result in data[kind].get("edges", []):
node = await self.infrahub_node.from_graphql(client=self._init_client, branch="main", data=result)
node = await self.infrahub_node.from_graphql(
client=self._init_client, branch=self.branch_name, data=result
)
self._nodes.append(node)
await node._process_relationships(
node_data=result, branch="main", related_nodes=self._related_nodes
node_data=result, branch=self.branch_name, related_nodes=self._related_nodes
)

for node in self._nodes + self._related_nodes:
Expand Down
8 changes: 4 additions & 4 deletions python_sdk/infrahub_sdk/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@ async def from_graphql(
node_kind = data.get("__typename", None) or data.get("node", {}).get("__typename", None)
if not node_kind:
raise ValueError("Unable to determine the type of the node, __typename not present in data")
schema = await client.schema.get(kind=node_kind)
schema = await client.schema.get(kind=node_kind, branch=branch)

return cls(client=client, schema=schema, branch=branch, data=cls._strip_alias(data))

Expand Down Expand Up @@ -1264,7 +1264,7 @@ async def generate_query_data_node(

peer_data: dict[str, Any] = {}
if rel_schema and prefetch_relationships:
peer_schema = await self._client.schema.get(kind=rel_schema.peer)
peer_schema = await self._client.schema.get(kind=rel_schema.peer, branch=self._branch)
peer_node = InfrahubNode(client=self._client, schema=peer_schema, branch=self._branch)
peer_data = await peer_node.generate_query_data_node(include=include, exclude=exclude, inherited=False)

Expand Down Expand Up @@ -1528,7 +1528,7 @@ def from_graphql(
node_kind = data.get("__typename", None) or data.get("node", {}).get("__typename", None)
if not node_kind:
raise ValueError("Unable to determine the type of the node, __typename not present in data")
schema = client.schema.get(kind=node_kind)
schema = client.schema.get(kind=node_kind, branch=branch)

return cls(client=client, schema=schema, branch=branch, data=cls._strip_alias(data))

Expand Down Expand Up @@ -1720,7 +1720,7 @@ def generate_query_data_node(

peer_data: dict[str, Any] = {}
if rel_schema and prefetch_relationships:
peer_schema = self._client.schema.get(kind=rel_schema.peer)
peer_schema = self._client.schema.get(kind=rel_schema.peer, branch=self._branch)
peer_node = InfrahubNodeSync(client=self._client, schema=peer_schema, branch=self._branch)
peer_data = peer_node.generate_query_data_node(include=include, exclude=exclude, inherited=False)

Expand Down
4 changes: 1 addition & 3 deletions python_sdk/tests/unit/sdk/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1661,9 +1661,7 @@ async def mock_schema_query_02(httpx_mock: HTTPXMock) -> HTTPXMock:
response_text = (get_fixtures_dir() / "schema_02.json").read_text(encoding="UTF-8")

httpx_mock.add_response(
method="GET",
url="http://mock/api/schema/?branch=main",
json=ujson.loads(response_text),
method="GET", url=re.compile(r"^http://mock/api/schema/\?branch=(main|cr1234)"), json=ujson.loads(response_text)
)
return httpx_mock

Expand Down

0 comments on commit 89e2d63

Please sign in to comment.