Skip to content

Commit dbbe385

Browse files
authored
Merge pull request #240 from opsmill/wvd-release-1.6.1
2 parents 1db17a9 + ddadf3a commit dbbe385

File tree

22 files changed

+327
-305
lines changed

22 files changed

+327
-305
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ jobs:
190190
uses: actions/setup-python@v5
191191
with:
192192
python-version: "3.12"
193+
- name: "Set environment variables"
194+
run: |
195+
RUNNER_NAME=$(echo "${{ runner.name }}" | grep -o 'ghrunner[0-9]\+' | sed 's/ghrunner\([0-9]\+\)/ghrunner_\1/')
196+
echo "PYTEST_DEBUG_TEMPROOT=/var/lib/github/${RUNNER_NAME}/_temp" >> $GITHUB_ENV
193197
- name: "Setup environment"
194198
run: |
195199
pipx install poetry==1.8.5

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@ This project uses [*towncrier*](https://towncrier.readthedocs.io/) and the chang
1111

1212
<!-- towncrier release notes start -->
1313

14+
## [1.6.1](https://github.com/opsmill/infrahub-sdk-python/tree/v1.6.1) - 2025-01-16
15+
16+
Fixes release of v1.6.0
17+
18+
## [1.6.0](https://github.com/opsmill/infrahub-sdk-python/tree/v1.6.0) - 2025-01-16
19+
20+
### Added
21+
22+
- Replace GitPython with dulwich ([#130](https://github.com/opsmill/infrahub-sdk-python/issues/130))
23+
24+
### Changed
25+
26+
- Added possibility to use filters for the SDK client's count method
27+
28+
### Fixed
29+
30+
- Fixes issue where using `parallel` query execution could lead to excessive and unneeded GraphQL queries
31+
1432
## [1.5.0](https://github.com/opsmill/infrahub-sdk-python/tree/v1.5.0) - 2025-01-09
1533

1634
### Added

infrahub_sdk/analyzer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def variables(self) -> list[GraphQLQueryVariable]:
9191
else:
9292
data["default_value"] = variable.default_value.value
9393

94-
if not data.get("default_value", None) and non_null:
94+
if not data.get("default_value") and non_null:
9595
data["required"] = True
9696

9797
response.append(GraphQLQueryVariable(**data))

infrahub_sdk/checks.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
from typing import TYPE_CHECKING, Any
99

1010
import ujson
11-
from git.repo import Repo
1211
from pydantic import BaseModel, Field
1312

13+
from infrahub_sdk.repository import GitRepoManager
14+
1415
from .exceptions import UninitializedError
1516

1617
if TYPE_CHECKING:
@@ -43,7 +44,7 @@ def __init__(
4344
params: dict | None = None,
4445
client: InfrahubClient | None = None,
4546
):
46-
self.git: Repo | None = None
47+
self.git: GitRepoManager | None = None
4748
self.initializer = initializer or InfrahubCheckInitializer()
4849

4950
self.logs: list[dict[str, Any]] = []
@@ -137,10 +138,9 @@ def branch_name(self) -> str:
137138
return self.branch
138139

139140
if not self.git:
140-
self.git = Repo(self.root_directory)
141+
self.git = GitRepoManager(self.root_directory)
141142

142143
self.branch = str(self.git.active_branch)
143-
144144
return self.branch
145145

146146
@abstractmethod

infrahub_sdk/client.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -547,16 +547,21 @@ async def count(
547547
at: Timestamp | None = None,
548548
branch: str | None = None,
549549
timeout: int | None = None,
550+
**kwargs: Any,
550551
) -> int:
551552
"""Return the number of nodes of a given kind."""
553+
filters = kwargs
552554
schema = await self.schema.get(kind=kind, branch=branch)
553555

554556
branch = branch or self.default_branch
555557
if at:
556558
at = Timestamp(at)
557559

558560
response = await self.execute_graphql(
559-
query=Query(query={schema.kind: {"count": None}}).render(), branch_name=branch, at=at, timeout=timeout
561+
query=Query(query={schema.kind: {"count": None, "@filters": filters}}).render(),
562+
branch_name=branch,
563+
at=at,
564+
timeout=timeout,
560565
)
561566
return int(response.get(schema.kind, {}).get("count", 0))
562567

@@ -781,7 +786,7 @@ async def process_batch() -> tuple[list[InfrahubNode], list[InfrahubNode]]:
781786
nodes = []
782787
related_nodes = []
783788
batch_process = await self.create_batch()
784-
count = await self.count(kind=schema.kind)
789+
count = await self.count(kind=schema.kind, **filters)
785790
total_pages = (count + pagination_size - 1) // pagination_size
786791

787792
for page_number in range(1, total_pages + 1):
@@ -1181,7 +1186,7 @@ async def allocate_next_ip_address(
11811186
async def allocate_next_ip_address(
11821187
self,
11831188
resource_pool: CoreNode,
1184-
kind: Literal[None] = ...,
1189+
kind: None = ...,
11851190
identifier: str | None = ...,
11861191
prefix_length: int | None = ...,
11871192
address_type: str | None = ...,
@@ -1196,7 +1201,7 @@ async def allocate_next_ip_address(
11961201
async def allocate_next_ip_address(
11971202
self,
11981203
resource_pool: CoreNode,
1199-
kind: Literal[None] = ...,
1204+
kind: None = ...,
12001205
identifier: str | None = ...,
12011206
prefix_length: int | None = ...,
12021207
address_type: str | None = ...,
@@ -1211,7 +1216,7 @@ async def allocate_next_ip_address(
12111216
async def allocate_next_ip_address(
12121217
self,
12131218
resource_pool: CoreNode,
1214-
kind: Literal[None] = ...,
1219+
kind: None = ...,
12151220
identifier: str | None = ...,
12161221
prefix_length: int | None = ...,
12171222
address_type: str | None = ...,
@@ -1328,7 +1333,7 @@ async def allocate_next_ip_prefix(
13281333
async def allocate_next_ip_prefix(
13291334
self,
13301335
resource_pool: CoreNode,
1331-
kind: Literal[None] = ...,
1336+
kind: None = ...,
13321337
identifier: str | None = ...,
13331338
prefix_length: int | None = ...,
13341339
member_type: str | None = ...,
@@ -1344,7 +1349,7 @@ async def allocate_next_ip_prefix(
13441349
async def allocate_next_ip_prefix(
13451350
self,
13461351
resource_pool: CoreNode,
1347-
kind: Literal[None] = ...,
1352+
kind: None = ...,
13481353
identifier: str | None = ...,
13491354
prefix_length: int | None = ...,
13501355
member_type: str | None = ...,
@@ -1360,7 +1365,7 @@ async def allocate_next_ip_prefix(
13601365
async def allocate_next_ip_prefix(
13611366
self,
13621367
resource_pool: CoreNode,
1363-
kind: Literal[None] = ...,
1368+
kind: None = ...,
13641369
identifier: str | None = ...,
13651370
prefix_length: int | None = ...,
13661371
member_type: str | None = ...,
@@ -1651,16 +1656,21 @@ def count(
16511656
at: Timestamp | None = None,
16521657
branch: str | None = None,
16531658
timeout: int | None = None,
1659+
**kwargs: Any,
16541660
) -> int:
16551661
"""Return the number of nodes of a given kind."""
1662+
filters = kwargs
16561663
schema = self.schema.get(kind=kind, branch=branch)
16571664

16581665
branch = branch or self.default_branch
16591666
if at:
16601667
at = Timestamp(at)
16611668

16621669
response = self.execute_graphql(
1663-
query=Query(query={schema.kind: {"count": None}}).render(), branch_name=branch, at=at, timeout=timeout
1670+
query=Query(query={schema.kind: {"count": None, "@filters": filters}}).render(),
1671+
branch_name=branch,
1672+
at=at,
1673+
timeout=timeout,
16641674
)
16651675
return int(response.get(schema.kind, {}).get("count", 0))
16661676

@@ -1920,7 +1930,7 @@ def process_batch() -> tuple[list[InfrahubNodeSync], list[InfrahubNodeSync]]:
19201930
related_nodes = []
19211931
batch_process = self.create_batch()
19221932

1923-
count = self.count(kind=schema.kind)
1933+
count = self.count(kind=schema.kind, **filters)
19241934
total_pages = (count + pagination_size - 1) // pagination_size
19251935

19261936
for page_number in range(1, total_pages + 1):
@@ -2296,7 +2306,7 @@ def allocate_next_ip_address(
22962306
def allocate_next_ip_address(
22972307
self,
22982308
resource_pool: CoreNodeSync,
2299-
kind: Literal[None] = ...,
2309+
kind: None = ...,
23002310
identifier: str | None = ...,
23012311
prefix_length: int | None = ...,
23022312
address_type: str | None = ...,
@@ -2311,7 +2321,7 @@ def allocate_next_ip_address(
23112321
def allocate_next_ip_address(
23122322
self,
23132323
resource_pool: CoreNodeSync,
2314-
kind: Literal[None] = ...,
2324+
kind: None = ...,
23152325
identifier: str | None = ...,
23162326
prefix_length: int | None = ...,
23172327
address_type: str | None = ...,
@@ -2326,7 +2336,7 @@ def allocate_next_ip_address(
23262336
def allocate_next_ip_address(
23272337
self,
23282338
resource_pool: CoreNodeSync,
2329-
kind: Literal[None] = ...,
2339+
kind: None = ...,
23302340
identifier: str | None = ...,
23312341
prefix_length: int | None = ...,
23322342
address_type: str | None = ...,
@@ -2439,7 +2449,7 @@ def allocate_next_ip_prefix(
24392449
def allocate_next_ip_prefix(
24402450
self,
24412451
resource_pool: CoreNodeSync,
2442-
kind: Literal[None] = ...,
2452+
kind: None = ...,
24432453
identifier: str | None = ...,
24442454
prefix_length: int | None = ...,
24452455
member_type: str | None = ...,
@@ -2455,7 +2465,7 @@ def allocate_next_ip_prefix(
24552465
def allocate_next_ip_prefix(
24562466
self,
24572467
resource_pool: CoreNodeSync,
2458-
kind: Literal[None] = ...,
2468+
kind: None = ...,
24592469
identifier: str | None = ...,
24602470
prefix_length: int | None = ...,
24612471
member_type: str | None = ...,
@@ -2471,7 +2481,7 @@ def allocate_next_ip_prefix(
24712481
def allocate_next_ip_prefix(
24722482
self,
24732483
resource_pool: CoreNodeSync,
2474-
kind: Literal[None] = ...,
2484+
kind: None = ...,
24752485
identifier: str | None = ...,
24762486
prefix_length: int | None = ...,
24772487
member_type: str | None = ...,

infrahub_sdk/generator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from abc import abstractmethod
55
from typing import TYPE_CHECKING
66

7-
from git.repo import Repo
7+
from infrahub_sdk.repository import GitRepoManager
88

99
from .exceptions import UninitializedError
1010

@@ -30,7 +30,7 @@ def __init__(
3030
) -> None:
3131
self.query = query
3232
self.branch = branch
33-
self.git: Repo | None = None
33+
self.git: GitRepoManager | None = None
3434
self.params = params or {}
3535
self.root_directory = root_directory or os.getcwd()
3636
self.generator_instance = generator_instance
@@ -81,7 +81,7 @@ def branch_name(self) -> str:
8181
return self.branch
8282

8383
if not self.git:
84-
self.git = Repo(self.root_directory)
84+
self.git = GitRepoManager(self.root_directory)
8585

8686
self.branch = str(self.git.active_branch)
8787

infrahub_sdk/node.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ async def from_graphql(
10741074
timeout: int | None = None,
10751075
) -> Self:
10761076
if not schema:
1077-
node_kind = data.get("__typename", None) or data.get("node", {}).get("__typename", None)
1077+
node_kind = data.get("__typename") or data.get("node", {}).get("__typename", None)
10781078
if not node_kind:
10791079
raise ValueError("Unable to determine the type of the node, __typename not present in data")
10801080
schema = await client.schema.get(kind=node_kind, branch=branch, timeout=timeout)
@@ -1594,7 +1594,7 @@ def from_graphql(
15941594
timeout: int | None = None,
15951595
) -> Self:
15961596
if not schema:
1597-
node_kind = data.get("__typename", None) or data.get("node", {}).get("__typename", None)
1597+
node_kind = data.get("__typename") or data.get("node", {}).get("__typename", None)
15981598
if not node_kind:
15991599
raise ValueError("Unable to determine the type of the node, __typename not present in data")
16001600
schema = client.schema.get(kind=node_kind, branch=branch, timeout=timeout)

infrahub_sdk/pytest_plugin/items/base.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import pytest
88
import ujson
9-
from git.exc import InvalidGitRepositoryError
109

1110
from ..exceptions import InvalidResourceConfigError
1211
from ..models import InfrahubInputOutputTest
@@ -28,7 +27,6 @@ def __init__(
2827
**kwargs: dict[str, Any],
2928
):
3029
super().__init__(*args, **kwargs) # type: ignore[arg-type]
31-
3230
self.resource_name: str = resource_name
3331
self.resource_config: InfrahubRepositoryConfigElement = resource_config
3432
self.test: InfrahubTest = test
@@ -68,9 +66,6 @@ def runtest(self) -> None:
6866
"""Run the test logic."""
6967

7068
def repr_failure(self, excinfo: pytest.ExceptionInfo, style: str | None = None) -> str: # noqa: ARG002
71-
if isinstance(excinfo.value, InvalidGitRepositoryError):
72-
return f"Invalid Git repository at {excinfo.value}"
73-
7469
return str(excinfo.value)
7570

7671
def reportinfo(self) -> tuple[Path | str, int | None, str]:

infrahub_sdk/repository.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from __future__ import annotations
2+
3+
from pathlib import Path
4+
5+
from dulwich import porcelain
6+
from dulwich.repo import Repo
7+
8+
9+
class GitRepoManager:
10+
def __init__(self, root_directory: str, branch: str = "main"):
11+
self.root_directory = root_directory
12+
self.branch = branch
13+
self.git: Repo = self.initialize_repo()
14+
15+
def initialize_repo(self) -> Repo:
16+
# Check if the directory already has a repository
17+
18+
root_path = Path(self.root_directory)
19+
20+
if root_path.exists() and (root_path / ".git").is_dir():
21+
repo = Repo(self.root_directory) # Open existing repo
22+
else:
23+
repo = Repo.init(self.root_directory, default_branch=self.branch.encode("utf-8"))
24+
25+
if not repo:
26+
raise ValueError("Failed to initialize or open a repository.")
27+
28+
return repo
29+
30+
@property
31+
def active_branch(self) -> str | None:
32+
active_branch = porcelain.active_branch(self.root_directory).decode("utf-8")
33+
return active_branch

0 commit comments

Comments
 (0)