Skip to content

fix: use configured default_branch in generators, transforms, checks - #1293

Open
pthmas wants to merge 3 commits into
stablefrom
issue-1290
Open

fix: use configured default_branch in generators, transforms, checks#1293
pthmas wants to merge 3 commits into
stablefrom
issue-1290

Conversation

@pthmas

@pthmas pthmas commented Aug 27, 2026

Copy link
Copy Markdown

Why

Running infrahubctl generator, transform or check without --branch targeted the
local Git checkout's active branch instead of the configured default_branch, failing with
BranchNotFoundError whenever that Git branch did not exist in Infrahub. The fallback the
default_branch_from_git flag exists to gate was applied unconditionally, overriding an
explicitly configured INFRAHUB_DEFAULT_BRANCH.

Goal: with default_branch_from_git unset or false, an operation with no explicit branch
targets the configured default_branch — the same branch the rest of the command already uses.

Non-goals: Config.clone drops an explicit branch argument when default_branch_from_git
is enabled. Pre-existing, out of scope here, will be filed separately.

Closes #1290

What changed

Behavioral changes:

  • Generators, transforms and checks run without an explicit branch now target the configured
    default_branch.
  • The local Git branch is used only when default_branch_from_git is enabled.
  • infrahubctl generator is no longer internally inconsistent: the GraphQL query and the
    schema fetch resolve to the same branch.

Implementation notes:

  • InfrahubOperation.__init__ resolves the branch once as branch or client.default_branch.
    client.default_branch is already the output of ConfigBase.default_infrahub_branch, which
    makes the default_branch_from_git decision — so the flag stays honoured in exactly one place
    rather than being reimplemented. Same pattern already used in ctl/cli_commands.py:386.
  • branch_name becomes a plain accessor; the GitRepoManager lookup and the self.git
    attribute are removed from operation.py.
  • InfrahubCheck.branch_name gets the same treatment. Its client is optional, so with no client
    (no config to consult) it falls back to the existing get_branch() helper.
  • Fixed at the base class, not the call sites — InfrahubGenerator and InfrahubTransform both
    inherit from InfrahubOperation, as does every library consumer.

What stayed the same:

  • No public API or signature changes. branch_name and root_directory are unchanged.
  • default_branch_from_git=true behaviour is unchanged.
  • No generated-doc drift; the config reference already documented the intended behaviour.

How to review

Start with infrahub_sdk/operation.py — the whole fix is self.branch = branch or client.default_branch. infrahub_sdk/checks.py is the same change with an extra no-client arm.

How to test

uv run pytest tests/unit/sdk/test_operation.py tests/unit/sdk/checks/
uv run invoke format lint-code

Regression coverage: explicit branch wins / no branch + flag off → configured default_branch /
no branch + flag on → local Git branch. Both new default_branch assertions fail on the
pre-fix code.

Impact & rollout

  • Backward compatibility: behaviour change. Anyone relying on the implicit Git fallback
    without setting default_branch_from_git=true now gets the configured default_branch
    (default main) instead of their checkout branch. That is the documented contract; noted in
    the changelog entry.
  • Config/env changes: none. INFRAHUB_DEFAULT_BRANCH and INFRAHUB_DEFAULT_BRANCH_FROM_GIT
    now behave as documented.
  • Deployment notes: safe to deploy.

Checklist

  • Tests added/updated
  • Changelog entry added (changelog/1290.fixed.md)
  • External docs updated — not needed, generated config reference already correct
  • Internal .md docs updated — not needed

Summary by cubic

Fixes generators, transforms, and checks to target the configured default_branch when no explicit branch is passed, instead of falling back to the local Git checkout branch, which previously caused BranchNotFoundError when that branch didn't exist in Infrahub.

Behavior changes

  • The local Git branch is used only when default_branch_from_git is enabled, and is resolved from the operation's root_directory rather than the process working directory.
  • infrahubctl generator now resolves the same branch for its GraphQL query and schema fetch.
  • No public API or signature changes; branch_name and root_directory are unchanged.

Anyone relying on the implicit Git fallback without default_branch_from_git now gets the configured default_branch (default main) instead of their checkout branch. No config or env changes required. Closes #1290.

Written for commit 01712fb. Summary will update on new commits.

Review in cubic

…hecks

`InfrahubOperation.branch_name` and `InfrahubCheck.branch_name` resolved the
branch from the local Git checkout whenever no explicit branch was passed,
ignoring the client's resolved default branch. Runs against a configured
`default_branch` therefore failed with `BranchNotFoundError` when the local Git
branch did not exist in Infrahub.

Resolve the branch from `client.default_branch` instead, which already honours
the `default_branch_from_git` config flag, so the Git branch is only used when
that flag is enabled.
@pthmas pthmas added the type/bug Something isn't working as expected label Aug 27, 2026
@pthmas
pthmas requested a review from a team as a code owner August 27, 2026 15:52
@pthmas pthmas added the type/bug Something isn't working as expected label Aug 27, 2026
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Aug 27, 2026

Copy link
Copy Markdown

Deploying infrahub-sdk-python with  Cloudflare Pages  Cloudflare Pages

Latest commit: 01712fb
Status: ✅  Deploy successful!
Preview URL: https://5128c29f.infrahub-sdk-python.pages.dev
Branch Preview URL: https://issue-1290.infrahub-sdk-python.pages.dev

View logs

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 5 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread infrahub_sdk/operation.py Outdated
Comment thread tests/unit/sdk/test_operation.py Outdated
Comment thread infrahub_sdk/operation.py
@codecov

codecov Bot commented Aug 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

@@            Coverage Diff             @@
##           stable    #1293      +/-   ##
==========================================
+ Coverage   84.24%   84.27%   +0.03%     
==========================================
  Files         147      147              
  Lines       13066    13040      -26     
  Branches     1940     1928      -12     
==========================================
- Hits        11007    10990      -17     
+ Misses       1494     1489       -5     
+ Partials      565      561       -4     
Flag Coverage Δ
integration-tests 39.05% <33.33%> (-0.13%) ⬇️
python-3.10 57.09% <58.33%> (+0.09%) ⬆️
python-3.11 57.09% <58.33%> (+0.09%) ⬆️
python-3.12 57.09% <58.33%> (+0.09%) ⬆️
python-3.13 57.09% <58.33%> (+0.09%) ⬆️
python-3.14 57.09% <58.33%> (+0.09%) ⬆️
python-filler-3.12 23.62% <75.00%> (-0.08%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
infrahub_sdk/checks.py 74.75% <100.00%> (+4.56%) ⬆️
infrahub_sdk/config.py 91.61% <100.00%> (+0.15%) ⬆️
infrahub_sdk/operation.py 89.18% <100.00%> (+0.55%) ⬆️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

`ConfigBase.default_infrahub_branch` always resolved the local Git branch from
the process working directory. Operations and checks are rooted at their own
`root_directory`, so with `default_branch_from_git` enabled they could resolve
against a different repository than the one they run in.

Add `ConfigBase.get_default_infrahub_branch(directory=...)`, which the existing
property now delegates to, and pass `root_directory` from `InfrahubOperation`
and `InfrahubCheck`.

Stub the Git lookup in the branch resolution tests. They previously compared
against `get_branch()` evaluated at assert time, which depends on the state of
the local checkout and fails outright when it has no active branch, as in CI.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 5 files (changes from recent commits).

Requires human review: Auto-approval blocked because this review re-detected 1 unresolved issue already reported by Cubic.

Re-trigger cubic

Comment thread infrahub_sdk/checks.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type/bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant