Skip to content

Commit 565674f

Browse files
authored
Fix issue with render ctl command not using branch environment variables (#536)
* Fix branch handling in `_run_transform` and `execute_graphql_query` functions * Add changelog entry for fixing branch handling in `_run_transform` and `execute_graphql_query` functions * Remove unused import of `get_branch` from `utils.py` * Add jinja2 transform and GraphQL query for tags, and implement branch selection test * Update changelog to specify environment variable usage for branch management in `_run_transform` and `execute_graphql_query` functions. * Remove unused `get_branch` import and set default branch in `validate_graphql` function
1 parent 71c150d commit 565674f

File tree

8 files changed

+70
-5
lines changed

8 files changed

+70
-5
lines changed

changelog/535.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix branch handling in `_run_transform` and `execute_graphql_query` functions in Infrahubctl to use environment variables for branch management.

infrahub_sdk/ctl/cli_commands.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
from ..schema import MainSchemaTypesAll, SchemaRoot
4747
from ..template import Jinja2Template
4848
from ..template.exceptions import JinjaTemplateError
49-
from ..utils import get_branch, write_to_file
49+
from ..utils import write_to_file
5050
from ..yaml import SchemaFile
5151
from .exporter import dump
5252
from .importer import load
@@ -208,7 +208,6 @@ async def _run_transform(
208208
debug: Prints debug info to the command line
209209
repository_config: Repository config object. This is used to load the graphql query from the repository.
210210
"""
211-
branch = get_branch(branch)
212211

213212
try:
214213
response = execute_graphql_query(

infrahub_sdk/ctl/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ def execute_graphql_query(
118118
query_str = query_object.load_query()
119119

120120
client = initialize_client_sync()
121+
122+
if not branch:
123+
branch = client.config.default_infrahub_branch
124+
121125
response = client.execute_graphql(
122126
query=query_str,
123127
branch_name=branch,

infrahub_sdk/ctl/validate.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from ..ctl.exceptions import QueryNotFoundError
1515
from ..ctl.utils import catch_exception, find_graphql_query, parse_cli_vars
1616
from ..exceptions import GraphQLError
17-
from ..utils import get_branch, write_to_file
17+
from ..utils import write_to_file
1818
from ..yaml import SchemaFile
1919
from .parameters import CONFIG_PARAM
2020
from .utils import load_yamlfile_from_disk_and_exit
@@ -68,8 +68,6 @@ def validate_graphql(
6868
) -> None:
6969
"""Validate the format of a GraphQL Query stored locally by executing it on a remote GraphQL endpoint"""
7070

71-
branch = get_branch(branch)
72-
7371
try:
7472
query_str = find_graphql_query(query)
7573
except QueryNotFoundError:
@@ -81,6 +79,10 @@ def validate_graphql(
8179
variables_dict = parse_cli_vars(variables)
8280

8381
client = initialize_client_sync()
82+
83+
if not branch:
84+
branch = client.config.default_infrahub_branch
85+
8486
try:
8587
response = client.execute_graphql(
8688
query=query_str,

tests/fixtures/repos/ctl_integration/.infrahub.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ generator_definitions:
2626
parameters:
2727
name: "name__value"
2828

29+
jinja2_transforms:
30+
- name: tags
31+
query: "tags_query"
32+
template_path: "templates/tags.j2"
2933

3034
queries:
3135
- name: animal_person
3236
file_path: queries/animal_person.gql
37+
- name: tags_query
38+
file_path: templates/tags_query.gql
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ data['BuiltinTag']['edges'][0]['node']['name']['value'] }}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
query TagsQuery($name: String!) {
2+
BuiltinTag(name__value: $name) {
3+
edges {
4+
node {
5+
name {
6+
value
7+
}
8+
}
9+
}
10+
}
11+
}

tests/unit/ctl/test_render_app.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,44 @@ def test_validate_template_not_found(test_case: RenderAppFailure, httpx_mock: HT
7373
output = runner.invoke(app, ["render", test_case.template, "name=red"])
7474
assert test_case.error in strip_color(output.stdout)
7575
assert output.exit_code == 1
76+
77+
78+
@pytest.mark.parametrize(
79+
"cli_branch,env_branch,from_git,expected_branch",
80+
[
81+
("cli-branch", None, False, "cli-branch"),
82+
(None, "env-branch", False, "env-branch"),
83+
(None, None, True, "git-branch"),
84+
],
85+
)
86+
@requires_python_310
87+
def test_render_branch_selection(monkeypatch, httpx_mock: HTTPXMock, cli_branch, env_branch, from_git, expected_branch):
88+
"""Test that the render command uses the correct branch source."""
89+
90+
if from_git:
91+
monkeypatch.setattr("dulwich.porcelain.active_branch", lambda _: b"git-branch")
92+
93+
httpx_mock.add_response(
94+
method="POST",
95+
url=f"http://mock/graphql/{expected_branch}",
96+
json=json.loads(
97+
read_fixture(
98+
"red_tag.json",
99+
"unit/test_infrahubctl/red_tags_query",
100+
)
101+
),
102+
)
103+
104+
with temp_repo_and_cd(source_dir=FIXTURE_BASE_DIR / "ctl_integration"):
105+
args = ["render", "tags", "name=red"]
106+
env = {}
107+
# Add test-specific variables
108+
if cli_branch:
109+
args.extend(["--branch", cli_branch])
110+
if env_branch:
111+
env["INFRAHUB_DEFAULT_BRANCH"] = env_branch
112+
env["INFRAHUB_DEFAULT_BRANCH_FROM_GIT"] = "false"
113+
if from_git:
114+
env["INFRAHUB_DEFAULT_BRANCH_FROM_GIT"] = "true"
115+
output = runner.invoke(app, args, env=env)
116+
assert output.exit_code == 0

0 commit comments

Comments
 (0)