Skip to content

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

Merged
pthmas merged 4 commits into
stablefrom
issue-1290
Oct 1, 2026
Merged

pthmas merged 4 commits into
stablefrom
issue-1290

Conversation

@pthmas

@pthmas pthmas commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

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, eliminating the BranchNotFoundError that occurred when the local Git branch didn't exist in Infrahub.

  • The local Git branch is used only when default_branch_from_git is enabled, and is resolved from the operation's root_directory instead of the process working directory.
  • infrahubctl generator now resolves the same branch for its GraphQL query and schema fetch.
  • Behavior change: operations without an explicit branch or default_branch_from_git now target the configured default_branch (default main) instead of the checkout branch. No config or env changes required. Closes bug: generator and transform runs ignore default_branch and take the local git branch instead #1290.

Written for commit f7499ae. 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: f7499ae
Status: ✅  Deploy successful!
Preview URL: https://70f9ab83.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 Resolved
Comment thread tests/unit/sdk/test_operation.py Resolved
Comment thread infrahub_sdk/operation.py Resolved
@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%   85.06%   +0.82%     
==========================================
  Files         147      148       +1     
  Lines       13066    13282     +216     
  Branches     1940     1950      +10     
==========================================
+ Hits        11007    11298     +291     
+ Misses       1494     1421      -73     
+ Partials      565      563       -2     
Flag Coverage Δ
integration-tests 38.70% <22.22%> (-0.48%) ⬇️
python-3.10 57.98% <77.77%> (+0.98%) ⬆️
python-3.11 57.96% <77.77%> (+0.97%) ⬆️
python-3.12 57.96% <77.77%> (+0.97%) ⬆️
python-3.13 57.96% <77.77%> (+0.97%) ⬆️
python-3.14 57.97% <77.77%> (+0.98%) ⬆️
python-filler-3.12 23.76% <44.44%> (+0.05%) ⬆️

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.76% <100.00%> (+0.30%) ⬆️
infrahub_sdk/operation.py 89.18% <100.00%> (+0.55%) ⬆️

... and 11 files 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 Resolved
@pthmas pthmas self-assigned this Aug 28, 2026

@ogenstad ogenstad 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.

LGTM.

Comment thread changelog/1290.fixed.md Resolved

@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 7 files

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

Re-trigger cubic

Comment thread infrahub_sdk/checks.py Resolved
@pthmas
pthmas merged commit 494e74a into stable Oct 1, 2026
23 checks passed
@pthmas
pthmas deleted the issue-1290 branch October 1, 2026 12:18
fatih-acar added a commit to opsmill/infrahub that referenced this pull request Oct 11, 2026
INFP-739]

opsmill/infrahub-sdk-python#1414 adds cost_level and cost_estimate to
the CoreGraphQLQuery protocols, so `invoke backend.validate-generated`
finds the SDK protocols up to date. The commit is on top of the SDK
stable branch, which also brings dependency bumps and the fix that makes
generators, transforms and checks use the configured default branch
(opsmill/infrahub-sdk-python#1293).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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

2 participants