Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/535.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix branch handling in `_run_transform` and `execute_graphql_query` functions in Infrahubctl to use environment variables for branch management.
3 changes: 1 addition & 2 deletions infrahub_sdk/ctl/cli_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
from ..schema import MainSchemaTypesAll, SchemaRoot
from ..template import Jinja2Template
from ..template.exceptions import JinjaTemplateError
from ..utils import get_branch, write_to_file
from ..utils import write_to_file
from ..yaml import SchemaFile
from .exporter import dump
from .importer import load
Expand Down Expand Up @@ -208,7 +208,6 @@ async def _run_transform(
debug: Prints debug info to the command line
repository_config: Repository config object. This is used to load the graphql query from the repository.
"""
branch = get_branch(branch)

try:
response = execute_graphql_query(
Expand Down
4 changes: 4 additions & 0 deletions infrahub_sdk/ctl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ def execute_graphql_query(
query_str = query_object.load_query()

client = initialize_client_sync()

if not branch:
branch = client.config.default_infrahub_branch

response = client.execute_graphql(
query=query_str,
branch_name=branch,
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/repos/ctl_integration/.infrahub.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ generator_definitions:
parameters:
name: "name__value"

jinja2_transforms:
- name: tags
query: "tags_query"
template_path: "templates/tags.j2"

queries:
- name: animal_person
file_path: queries/animal_person.gql
- name: tags_query
file_path: templates/tags_query.gql
1 change: 1 addition & 0 deletions tests/fixtures/repos/ctl_integration/templates/tags.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ data['BuiltinTag']['edges'][0]['node']['name']['value'] }}
11 changes: 11 additions & 0 deletions tests/fixtures/repos/ctl_integration/templates/tags_query.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
query TagsQuery($name: String!) {
BuiltinTag(name__value: $name) {
edges {
node {
name {
value
}
}
}
}
}
41 changes: 41 additions & 0 deletions tests/unit/ctl/test_render_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,44 @@ def test_validate_template_not_found(test_case: RenderAppFailure, httpx_mock: HT
output = runner.invoke(app, ["render", test_case.template, "name=red"])
assert test_case.error in strip_color(output.stdout)
assert output.exit_code == 1


@pytest.mark.parametrize(
"cli_branch,env_branch,from_git,expected_branch",
[
("cli-branch", None, False, "cli-branch"),
(None, "env-branch", False, "env-branch"),
(None, None, True, "git-branch"),
],
)
@requires_python_310
def test_render_branch_selection(monkeypatch, httpx_mock: HTTPXMock, cli_branch, env_branch, from_git, expected_branch):
"""Test that the render command uses the correct branch source."""

if from_git:
monkeypatch.setattr("dulwich.porcelain.active_branch", lambda _: b"git-branch")

httpx_mock.add_response(
method="POST",
url=f"http://mock/graphql/{expected_branch}",
json=json.loads(
read_fixture(
"red_tag.json",
"unit/test_infrahubctl/red_tags_query",
)
),
)

with temp_repo_and_cd(source_dir=FIXTURE_BASE_DIR / "ctl_integration"):
args = ["render", "tags", "name=red"]
env = {}
# Add test-specific variables
if cli_branch:
args.extend(["--branch", cli_branch])
if env_branch:
env["INFRAHUB_DEFAULT_BRANCH"] = env_branch
env["INFRAHUB_DEFAULT_BRANCH_FROM_GIT"] = "false"
if from_git:
env["INFRAHUB_DEFAULT_BRANCH_FROM_GIT"] = "true"
output = runner.invoke(app, args, env=env)
assert output.exit_code == 0
Loading