Component
Python SDK, infrahubctl
Infrahub SDK version
1.23.0 (current latest on PyPI)
Current Behavior
With INFRAHUB_DEFAULT_BRANCH=test set and no --branch on the command line, infrahubctl generator <name> <var>=<value> fails with:
Error: The requested branch was not found on the server
infrahub_sdk.exceptions.BranchNotFoundError: The requested branch was not found on the server
raised from generator._init_client.schema.all(branch=generator.branch_name), infrahub_sdk/ctl/generator.py:71.
The branch it asks for is not test, it is the active branch of the local git checkout.
InfrahubOperation.branch_name, infrahub_sdk/operation.py:32:
@property
def branch_name(self) -> str:
"""Return the name of the current git branch."""
if self.branch:
return self.branch
if not hasattr(self, "git") or not self.git:
self.git = GitRepoManager(self.root_directory)
self.branch = str(self.git.active_branch)
return self.branch
The client is never consulted. When no branch is passed explicitly, the git branch wins over the configured default_branch, even though client.default_branch is already resolved to test at that point.
That fallback is what default_branch_from_git is meant to gate, infrahub_sdk/config.py:50, default False, env var INFRAHUB_DEFAULT_BRANCH_FROM_GIT. ConfigBase.default_infrahub_branch honours it correctly, infrahub_sdk/config.py:225:
@property
def default_infrahub_branch(self) -> str:
branch: str | None = None
if not self.default_branch_from_git:
branch = self.default_branch
return get_branch(branch=branch)
So InfrahubOperation applies unconditionally the behaviour that this flag exists to opt into, and overrides an explicitly configured default_branch while doing it.
The same command is inconsistent with itself: the GraphQL query runs against the right branch, because execute_graphql_query falls back to client.config.default_infrahub_branch, infrahub_sdk/ctl/utils.py:122. Only the schema fetch and everything else going through _init_client uses the git branch.
infrahubctl transform builds transform_class(client=client, branch=branch, ...) with branch defaulting to None, infrahub_sdk/ctl/cli_commands.py:340, and then uses transform.branch_name, so it goes through the same path. The generator is the case actually observed.
Expected Behavior
With default_branch_from_git unset or False, a generator or transform run without an explicit branch targets the configured default_branch, the same branch the rest of the command already uses.
The local git branch is used only when default_branch_from_git is True.
Steps to Reproduce
- Set the client config:
export INFRAHUB_ADDRESS=https://infrahub.example.com/
export INFRAHUB_API_TOKEN=...
export INFRAHUB_DEFAULT_BRANCH=test
- Create the branch
test in Infrahub, and make sure the local git checkout is on a branch that does not exist in Infrahub, for example main or develop
- Define any generator in
.infrahub.yml with a parameters entry
- Run it with the parameter so it takes the non-group path, for example
infrahubctl generator process_pending_rules rule_id=100
- It fails with
BranchNotFoundError instead of running against test
Additional Information
Workaround is to pass --branch test explicitly on every invocation.
INFRAHUB_DEFAULT_BRANCH and INFRAHUB_DEFAULT_BRANCH_FROM_GIT are both documented in the SDK config reference on docs.infrahub.app, so the configuration used here is the documented one.
Found while working around #1289 on the same customer setup, SDK 1.23.0 on Python 3.12.13. The two are independent, #1289 blocks the group path even when the branch is correct.
Component
Python SDK, infrahubctl
Infrahub SDK version
1.23.0 (current latest on PyPI)
Current Behavior
With
INFRAHUB_DEFAULT_BRANCH=testset and no--branchon the command line,infrahubctl generator <name> <var>=<value>fails with:raised from
generator._init_client.schema.all(branch=generator.branch_name),infrahub_sdk/ctl/generator.py:71.The branch it asks for is not
test, it is the active branch of the local git checkout.InfrahubOperation.branch_name,infrahub_sdk/operation.py:32:The client is never consulted. When no branch is passed explicitly, the git branch wins over the configured
default_branch, even thoughclient.default_branchis already resolved totestat that point.That fallback is what
default_branch_from_gitis meant to gate,infrahub_sdk/config.py:50, defaultFalse, env varINFRAHUB_DEFAULT_BRANCH_FROM_GIT.ConfigBase.default_infrahub_branchhonours it correctly,infrahub_sdk/config.py:225:So
InfrahubOperationapplies unconditionally the behaviour that this flag exists to opt into, and overrides an explicitly configureddefault_branchwhile doing it.The same command is inconsistent with itself: the GraphQL query runs against the right branch, because
execute_graphql_queryfalls back toclient.config.default_infrahub_branch,infrahub_sdk/ctl/utils.py:122. Only the schema fetch and everything else going through_init_clientuses the git branch.infrahubctl transformbuildstransform_class(client=client, branch=branch, ...)withbranchdefaulting toNone,infrahub_sdk/ctl/cli_commands.py:340, and then usestransform.branch_name, so it goes through the same path. The generator is the case actually observed.Expected Behavior
With
default_branch_from_gitunset orFalse, a generator or transform run without an explicit branch targets the configureddefault_branch, the same branch the rest of the command already uses.The local git branch is used only when
default_branch_from_gitisTrue.Steps to Reproduce
testin Infrahub, and make sure the local git checkout is on a branch that does not exist in Infrahub, for examplemainordevelop.infrahub.ymlwith aparametersentryinfrahubctl generator process_pending_rules rule_id=100BranchNotFoundErrorinstead of running againsttestAdditional Information
Workaround is to pass
--branch testexplicitly on every invocation.INFRAHUB_DEFAULT_BRANCHandINFRAHUB_DEFAULT_BRANCH_FROM_GITare both documented in the SDK config reference on docs.infrahub.app, so the configuration used here is the documented one.Found while working around #1289 on the same customer setup, SDK 1.23.0 on Python 3.12.13. The two are independent, #1289 blocks the group path even when the branch is correct.