Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
35 changes: 14 additions & 21 deletions budjira/cli/connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from __future__ import annotations

import os
from typing import TYPE_CHECKING

import typer
from rich.console import Console
Expand All @@ -15,9 +14,6 @@
from budjira.utils.description import DescriptionDialectOption # noqa: TC001 - Typer resolves it at runtime
from budjira.utils.errors import BudjiraError

if TYPE_CHECKING:
from budjira.models.connection import DescriptionDialect

app = typer.Typer(
name="connect",
help="Manage Jira connections",
Expand Down Expand Up @@ -137,24 +133,21 @@ def add_connection(
default="<keep existing>" if existing and credential_store.has_credentials(existing) else None,
)

# Keep the dialect of an existing connection unless it is explicitly changed
dialect: DescriptionDialect
# Updating carries the stored connection forward: this command only asks for a
# handful of fields, and everything else (Tempo, custom fields, board, prompts)
# must survive an edit of URL, email or project key.
stored_values = existing.model_dump() if existing else {}
changed_values: dict[str, object] = {
"name": name,
"url": url,
"email": email,
"project_key": project_key,
}
if description_dialect is not None:
dialect = description_dialect.value
elif existing:
dialect = existing.description_dialect
else:
dialect = "markdown"

# Create connection
# Pydantic will validate and convert url string to HttpUrl
connection = Connection(
name=name,
url=url, # type: ignore[arg-type]
email=email,
project_key=project_key,
description_dialect=dialect,
)
changed_values["description_dialect"] = description_dialect.value

# Pydantic validates the merged values and converts the url string to HttpUrl
connection = Connection(**{**stored_values, **changed_values})

# Save connection
if existing:
Expand Down
4 changes: 4 additions & 0 deletions budjira/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ def _serialize_connection(self, conn: Connection) -> dict[str, object]:
"description_dialect": conn.description_dialect,
}

# Add board_id if set
if conn.board_id is not None:
result["board_id"] = conn.board_id

# Add ai_prompt if set
if conn.ai_prompt is not None:
result["ai_prompt"] = conn.ai_prompt
Expand Down
68 changes: 68 additions & 0 deletions tests/cli/test_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,74 @@ def test_add_keeps_description_dialect_of_existing_connection(self, tmp_path: Pa
assert stored is not None
assert stored.description_dialect == "wiki"

def test_add_preserves_configuration_of_existing_connection(self, tmp_path: Path) -> None:
"""Updating a connection must not reset fields the command never asked for (#108)."""
from budjira.models.custom_field import CustomFieldConfig, CustomFieldType

with (
patch("budjira.config.settings.xdg_config_home", return_value=tmp_path / "config"),
patch("budjira.config.settings.xdg_data_home", return_value=tmp_path / "data"),
patch("budjira.config.credentials.get_settings") as mock_cred_get_settings,
patch("budjira.cli.connect._auto_sync_metadata"),
):
import budjira.config.credentials
import budjira.config.settings

budjira.config.settings._settings = None
budjira.config.credentials._credential_store = None

from budjira.config import get_settings

settings = get_settings()
mock_cred_get_settings.return_value = settings
settings.add_connection(
Connection(
name="Configured",
url="https://test.atlassian.net",
email="old@example.com",
project_key="OLD",
tempo_enabled=True,
cache_enabled=True,
cache_ttl_hours=48,
board_id=42,
ai_prompt="house rules",
custom_fields={
"system": CustomFieldConfig(field_id="customfield_10001", type=CustomFieldType.SELECT)
},
)
)

result = runner.invoke(
app,
[
"connect",
"add",
"--name",
"Configured",
"--url",
"https://test.atlassian.net",
"--email",
"new@example.com",
"--project",
"NEW",
],
input="y\ntoken\n",
)

assert result.exit_code == 0
stored = settings.load_connections().find_by_name("Configured")
assert stored is not None
# What was given changes ...
assert stored.email == "new@example.com"
assert stored.project_key == "NEW"
# ... everything else survives
assert stored.tempo_enabled is True
assert stored.cache_enabled is True
assert stored.cache_ttl_hours == 48
assert stored.board_id == 42
assert stored.ai_prompt == "house rules"
assert stored.custom_fields["system"].field_id == "customfield_10001"


class TestConnectList:
"""Test connect list command."""
Expand Down
29 changes: 29 additions & 0 deletions tests/config/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,35 @@ def test_tempo_enabled_persistence(self, temp_settings: Settings) -> None:
loaded_conn = loaded.connections[0]
assert loaded_conn.tempo_enabled is True, "tempo_enabled should be persisted to TOML"

def test_board_id_persistence(self, temp_settings: Settings) -> None:
"""A board configured for sprint operations must survive a save (issue #108)."""
temp_settings.add_connection(
Connection(
name="Test Board",
url="https://test.atlassian.net",
email="test@example.com",
project_key="TEST",
board_id=42,
)
)

loaded = temp_settings.load_connections()

assert loaded.connections[0].board_id == 42

def test_board_id_omitted_when_unset(self, temp_settings: Settings) -> None:
"""An unset board writes no key, like the other optional fields."""
temp_settings.add_connection(
Connection(
name="Test No Board",
url="https://test.atlassian.net",
email="test@example.com",
project_key="TEST",
)
)

assert "board_id" not in temp_settings.connections_file.read_text()

def test_tempo_disabled_by_default(self, temp_settings: Settings) -> None:
"""Test that tempo_enabled defaults to False when not specified."""
connection = Connection(
Expand Down
Loading