Skip to content

bug: generator and transform runs ignore default_branch and take the local git branch instead #1290

Description

@lancamat1

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

  1. Set the client config:
export INFRAHUB_ADDRESS=https://infrahub.example.com/
export INFRAHUB_API_TOKEN=...
export INFRAHUB_DEFAULT_BRANCH=test
  1. 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
  2. Define any generator in .infrahub.yml with a parameters entry
  3. Run it with the parameter so it takes the non-group path, for example infrahubctl generator process_pending_rules rule_id=100
  4. 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.

Metadata

Metadata

Assignees

Labels

state/need-triageThis issue needs to be triagedtype/bugSomething isn't working as expected

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions