diff --git a/changelog/325.removed.md b/changelog/325.removed.md new file mode 100644 index 000000000..e79b0c1d4 --- /dev/null +++ b/changelog/325.removed.md @@ -0,0 +1 @@ +Removed `client.branch.diff_data()` from both the async and sync clients. The method relied on a `GET /api/diff/data` REST endpoint that does not exist in Infrahub, so every call returned a 404. Use `client.get_diff_tree()` to retrieve the full diff of a branch against its base branch, or `client.get_diff_summary()` for the list of changed nodes; both use the `DiffTree` GraphQL query. diff --git a/docs/docs/python-sdk/guides/branches.mdx b/docs/docs/python-sdk/guides/branches.mdx index 64016048e..ffe2049d7 100644 --- a/docs/docs/python-sdk/guides/branches.mdx +++ b/docs/docs/python-sdk/guides/branches.mdx @@ -108,15 +108,40 @@ The Python SDK provides multiple methods to manage the branches in an Infrahub i -## Generating a diff for a branch +## Getting the diff for a branch + +Use `get_diff_tree` to retrieve the full diff of a branch compared to its base branch, including summary counts and the list of changed nodes. It returns `None` if no diff exists for the branch. + + + + + ```python + from infrahub_sdk import InfrahubClient + client = InfrahubClient() + diff = await client.get_diff_tree(branch="new-branch") + ``` + + + + + ```python + from infrahub_sdk import InfrahubClientSync + client = InfrahubClientSync() + diff = client.get_diff_tree(branch="new-branch") + ``` + + + + +If you only need the list of changed nodes, `get_diff_summary` returns them without the diff metadata. ```python from infrahub_sdk import InfrahubClient - client = await InfrahubClient() - diff = await client.branch.diff_data(branch_name="new-branch") + client = InfrahubClient() + node_diffs = await client.get_diff_summary(branch="new-branch") ``` @@ -125,7 +150,7 @@ The Python SDK provides multiple methods to manage the branches in an Infrahub i ```python from infrahub_sdk import InfrahubClientSync client = InfrahubClientSync() - diff = client.branch.diff_data(branch_name="new-branch") + node_diffs = client.get_diff_summary(branch="new-branch") ``` diff --git a/infrahub_sdk/branch.py b/infrahub_sdk/branch.py index 5e1459a6d..b310522e4 100644 --- a/infrahub_sdk/branch.py +++ b/infrahub_sdk/branch.py @@ -1,14 +1,12 @@ from __future__ import annotations from enum import Enum -from typing import TYPE_CHECKING, Any, Literal, overload -from urllib.parse import urlencode +from typing import TYPE_CHECKING, Literal, overload from pydantic import BaseModel from .exceptions import BranchNotFoundError from .graphql import Mutation, Query -from .utils import decode_json if TYPE_CHECKING: from .client import InfrahubClient, InfrahubClientSync @@ -61,30 +59,7 @@ class BranchData(BaseModel): QUERY_ONE_BRANCH_DATA = {"Branch": {**BRANCH_DATA, **BRANCH_DATA_FILTER}} -class InfraHubBranchManagerBase: - @classmethod - def generate_diff_data_url( - cls, - client: InfrahubClient | InfrahubClientSync, - branch_name: str, - branch_only: bool = True, - time_from: str | None = None, - time_to: str | None = None, - ) -> str: - """Generate the URL for the diff_data function.""" - url = f"{client.address}/api/diff/data" - url_params = {} - url_params["branch"] = branch_name - url_params["branch_only"] = str(branch_only).lower() - if time_from: - url_params["time_from"] = time_from - if time_to: - url_params["time_to"] = time_to - - return url + urlencode(url_params) - - -class InfrahubBranchManager(InfraHubBranchManagerBase): +class InfrahubBranchManager: def __init__(self, client: InfrahubClient) -> None: self.client = client @@ -206,25 +181,8 @@ async def get(self, branch_name: str) -> BranchData: raise BranchNotFoundError(identifier=branch_name) return BranchData(**data["Branch"][0]) - async def diff_data( - self, - branch_name: str, - branch_only: bool = True, - time_from: str | None = None, - time_to: str | None = None, - ) -> dict[Any, Any]: - url = self.generate_diff_data_url( - client=self.client, - branch_name=branch_name, - branch_only=branch_only, - time_from=time_from, - time_to=time_to, - ) - response = await self.client._get(url=url, headers=self.client.headers) - return decode_json(response=response) - -class InfrahubBranchManagerSync(InfraHubBranchManagerBase): +class InfrahubBranchManagerSync: def __init__(self, client: InfrahubClientSync) -> None: self.client = client @@ -300,23 +258,6 @@ def delete(self, branch_name: str) -> bool: response = self.client.execute_graphql(query=query.render(), tracker="mutation-branch-delete") return response["BranchDelete"]["ok"] - def diff_data( - self, - branch_name: str, - branch_only: bool = True, - time_from: str | None = None, - time_to: str | None = None, - ) -> dict[Any, Any]: - url = self.generate_diff_data_url( - client=self.client, - branch_name=branch_name, - branch_only=branch_only, - time_from=time_from, - time_to=time_to, - ) - response = self.client._get(url=url, headers=self.client.headers) - return decode_json(response=response) - def merge(self, branch_name: str) -> bool: input_data = { "data": {