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
1 change: 1 addition & 0 deletions changelog/236.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Respect default branch for client.query_gql_query() and client.set_context_properties()
10 changes: 5 additions & 5 deletions infrahub_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def set_context_properties(
delete_unused_nodes=delete_unused_nodes,
group_type=group_type,
group_params=group_params,
branch=branch,
branch=branch or self.default_branch,
)

def _graphql_url(
Expand Down Expand Up @@ -1103,13 +1103,13 @@ async def query_gql_query(
) -> dict:
url = f"{self.address}/api/query/{name}"
url_params = copy.deepcopy(params or {})
url_params["branch"] = branch_name or self.default_branch

headers = copy.copy(self.headers or {})

if self.insert_tracker and tracker:
headers["X-Infrahub-Tracker"] = tracker

if branch_name:
url_params["branch"] = branch_name
if at:
url_params["at"] = at

Expand Down Expand Up @@ -2242,13 +2242,13 @@ def query_gql_query(
) -> dict:
url = f"{self.address}/api/query/{name}"
url_params = copy.deepcopy(params or {})
url_params["branch"] = branch_name or self.default_branch

Comment on lines +2245 to +2246
Copy link

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Mirror the non-breaking precedence in sync path.

Same concern and fix as async; keep params['branch'] if provided, else fall back to default.

-        url_params["branch"] = branch_name or self.default_branch
+        if branch_name:
+            url_params["branch"] = branch_name
+        elif "branch" not in url_params and self.default_branch:
+            url_params["branch"] = self.default_branch

Optional follow-up: extract this small precedence block into a BaseClient helper to DRY both call sites.


🏁 Script executed:

#!/bin/bash
# Locate client.py file
fd -g client.py infrahub_sdk

# Search for sync branch assignment
rg -n 'url_params\["branch"\]' -C5 infrahub_sdk/client.py

# Search for branch_name usage
rg -n 'branch_name' -C5 infrahub_sdk/client.py

# Search for async functions where branch is set
rg -n 'async def' -C5 infrahub_sdk/client.py

Length of output: 35479


Apply conditional branch precedence in both REST query methods

– In infrahub_sdk/client.py around line 1106 (async query_gql_query) and line 2245 (sync REST query), replace

url_params["branch"] = branch_name or self.default_branch

with:

if branch_name:
    url_params["branch"] = branch_name
elif "branch" not in url_params and self.default_branch:
    url_params["branch"] = self.default_branch

– Optional: extract this block into a BaseClient helper to DRY both call sites.

🤖 Prompt for AI Agents
In infrahub_sdk/client.py around lines 1106 (async query_gql_query) and 2245
(sync REST query), the branch parameter is unconditionally set using
"branch_name or self.default_branch"; change it to conditionally prefer an
explicit branch_name, only add default_branch if no "branch" already exists in
url_params and self.default_branch is set (i.e., if branch_name: set
url_params["branch"]=branch_name; elif "branch" not in url_params and
self.default_branch: set url_params["branch"]=self.default_branch). Apply the
same logic to both call sites, and optionally extract this logic into a
BaseClient helper method to avoid duplication and call it from both locations.

headers = copy.copy(self.headers or {})

if self.insert_tracker and tracker:
headers["X-Infrahub-Tracker"] = tracker

if branch_name:
url_params["branch"] = branch_name
if at:
url_params["at"] = at
if subscribers:
Expand Down