Skip to content

Commit cadf62e

Browse files
fix: ensure base_url defaults correctly when not set
When base_url is not provided via config file or CORTEX_BASE_URL environment variable, the CLI now properly defaults to https://api.getcortexapp.com before attempting to strip the value. Previously, when no config file existed, url would remain None and cause an AttributeError when url.strip() was called. This affected GitHub Actions workflows and other automated environments. Changes: - Add safety check to set default URL if None before strip operation - Add test case to verify proper defaulting behavior Fixes #151 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent a9d3721 commit cadf62e

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

cortexapps_cli/cli.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import cortexapps_cli.commands.deploys as deploys
2424
import cortexapps_cli.commands.discovery_audit as discovery_audit
2525
import cortexapps_cli.commands.docs as docs
26+
import cortexapps_cli.commands.entity_relationships as entity_relationships
27+
import cortexapps_cli.commands.entity_relationship_types as entity_relationship_types
2628
import cortexapps_cli.commands.entity_types as entity_types
2729
import cortexapps_cli.commands.gitops_logs as gitops_logs
2830
import cortexapps_cli.commands.groups as groups
@@ -57,6 +59,8 @@
5759
app.add_typer(deploys.app, name="deploys")
5860
app.add_typer(discovery_audit.app, name="discovery-audit")
5961
app.add_typer(docs.app, name="docs")
62+
app.add_typer(entity_relationships.app, name="entity-relationships")
63+
app.add_typer(entity_relationship_types.app, name="entity-relationship-types")
6064
app.add_typer(entity_types.app, name="entity-types")
6165
app.add_typer(gitops_logs.app, name="gitops-logs")
6266
app.add_typer(groups.app, name="groups")
@@ -121,6 +125,10 @@ def global_callback(
121125
else:
122126
url = "https://api.getcortexapp.com"
123127

128+
# Set default URL if not provided
129+
if not url:
130+
url = "https://api.getcortexapp.com"
131+
124132
# strip any quotes or spaces from the api_key and url
125133
api_key = api_key.strip('"\' ')
126134
url = url.strip('"\' /')

tests/test_config_file.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,28 @@ def test_config_file_base_url_env_var(monkeypatch, tmp_path):
7070
f.write_text(content)
7171
monkeypatch.setenv("CORTEX_BASE_URL", "https://api.getcortexapp.com")
7272
cli(["-c", str(f), "-t", "mySection", "entity-types", "list"])
73+
74+
def test_no_base_url_defaults_correctly(monkeypatch, tmp_path):
75+
"""
76+
Test that when base_url is not provided via config file or environment variable,
77+
the CLI defaults to https://api.getcortexapp.com without crashing.
78+
79+
This reproduces the bug from issue #151 where url.strip() was called on None.
80+
"""
81+
# Remove CORTEX_BASE_URL from environment
82+
monkeypatch.delenv("CORTEX_BASE_URL", raising=False)
83+
84+
cortex_api_key = os.getenv('CORTEX_API_KEY')
85+
86+
# Create config file WITHOUT base_url
87+
f = tmp_path / "cortex_config_no_base_url"
88+
template = Template("""
89+
[default]
90+
api_key = "${cortex_api_key}"
91+
""")
92+
content = template.substitute(cortex_api_key=cortex_api_key)
93+
f.write_text(content)
94+
95+
# This should not crash and should use the default URL
96+
response = cli(["-c", str(f), "entity-types", "list"])
97+
assert 'definitions' in response, "Should successfully call API with default URL"

0 commit comments

Comments
 (0)