-
Notifications
You must be signed in to change notification settings - Fork 13
fix: use configured default_branch in generators, transforms, checks #1293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pthmas
wants to merge
3
commits into
stable
Choose a base branch
from
issue-1290
base: stable
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+137
−25
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fixed generators, transforms and checks run without an explicit branch, which targeted the local Git branch instead of the configured `default_branch` and failed with `BranchNotFoundError` when that Git branch did not exist in Infrahub. The local Git branch is now used only when `default_branch_from_git` is enabled. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import pathlib | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from infrahub_sdk import Config, InfrahubClient | ||
| from infrahub_sdk.node import InfrahubNode | ||
| from infrahub_sdk.transforms import InfrahubTransform | ||
|
|
||
| if TYPE_CHECKING: | ||
| import pytest | ||
|
|
||
|
|
||
| class DummyTransform(InfrahubTransform): | ||
| query = "my_query" | ||
|
|
||
| def transform(self, data: dict) -> dict: | ||
| return data | ||
|
|
||
|
|
||
| def _build_transform(client: InfrahubClient, branch: str = "", root_directory: str = "") -> DummyTransform: | ||
| return DummyTransform(client=client, infrahub_node=InfrahubNode, branch=branch, root_directory=root_directory) | ||
|
|
||
|
|
||
| def _stub_git_branch(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| """Replace the Git lookup so tests never depend on the state of the local checkout. | ||
|
|
||
| The stub encodes the directory it was asked about, so callers can assert which repository | ||
| the branch was resolved from. | ||
| """ | ||
|
|
||
| def fake_get_branch(branch: str | None = None, directory: str = ".") -> str: | ||
| return branch or f"git:{directory}" | ||
|
|
||
| monkeypatch.setattr("infrahub_sdk.config.get_branch", fake_get_branch) | ||
|
|
||
|
|
||
| async def test_branch_name_uses_explicit_branch() -> None: | ||
| client = InfrahubClient(config=Config(address="http://mock", default_branch="test")) | ||
|
|
||
| transform = _build_transform(client=client, branch="explicit") | ||
|
|
||
| assert transform.branch_name == "explicit" | ||
| assert transform._init_client.default_branch == "explicit" | ||
|
|
||
|
|
||
| async def test_branch_name_falls_back_to_configured_default_branch() -> None: | ||
| """Without an explicit branch, the configured default branch wins over the local Git branch.""" | ||
| client = InfrahubClient(config=Config(address="http://mock", default_branch="test")) | ||
|
|
||
| transform = _build_transform(client=client) | ||
|
|
||
| assert transform.branch_name == "test" | ||
| assert transform._init_client.default_branch == "test" | ||
|
|
||
|
|
||
| async def test_branch_name_falls_back_to_git_branch_when_opted_in(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| _stub_git_branch(monkeypatch) | ||
| client = InfrahubClient(config=Config(address="http://mock", default_branch="test", default_branch_from_git=True)) | ||
|
|
||
| transform = _build_transform(client=client) | ||
|
|
||
| assert transform.branch_name == f"git:{pathlib.Path.cwd()}" | ||
|
|
||
|
|
||
| async def test_git_branch_is_read_from_the_root_directory(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| _stub_git_branch(monkeypatch) | ||
| client = InfrahubClient(config=Config(address="http://mock", default_branch="test", default_branch_from_git=True)) | ||
|
|
||
| transform = _build_transform(client=client, root_directory="/some/repository") | ||
|
|
||
| assert transform.branch_name == "git:/some/repository" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.