From 001358c64fccbd7aaf23248b27a2215a5fdcc624 Mon Sep 17 00:00:00 2001 From: Kimi Agent Date: Fri, 20 Mar 2026 17:29:55 -0400 Subject: [PATCH] refactor: break up create_gitea_issue_via_mcp into helpers (#647) Co-authored-by: Kimi Agent Co-committed-by: Kimi Agent --- src/timmy/mcp_tools.py | 20 ++++++++------- tests/timmy/test_mcp_tools.py | 46 +++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/src/timmy/mcp_tools.py b/src/timmy/mcp_tools.py index ea746798..6d215874 100644 --- a/src/timmy/mcp_tools.py +++ b/src/timmy/mcp_tools.py @@ -205,9 +205,9 @@ def _bridge_to_work_order(title: str, body: str, category: str) -> None: async def _ensure_issue_session(): - """Lazily create and connect the module-level MCP issue session. + """Get or create the cached MCP session, connecting if needed. - Returns the connected ``MCPTools`` instance or raises on failure. + Returns the connected ``MCPTools`` instance. """ from agno.tools.mcp import MCPTools @@ -226,18 +226,18 @@ async def _ensure_issue_session(): return _issue_session -def _build_issue_args(title: str, body: str) -> dict: - """Build the ``issue_write`` tool arguments dict. - - Appends the auto-filing signature and parses owner/repo from settings. - """ +def _build_issue_body(body: str) -> str: + """Append the auto-filing signature to the issue body.""" full_body = body if full_body: full_body += "\n\n" full_body += "---\n*Auto-filed by Timmy's thinking engine*" + return full_body + +def _build_issue_args(title: str, full_body: str) -> dict: + """Build MCP tool arguments for ``issue_write`` with method=create.""" owner, repo = settings.gitea_repo.split("/", 1) - return { "method": "create", "owner": owner, @@ -272,7 +272,9 @@ async def create_gitea_issue_via_mcp(title: str, body: str = "", labels: str = " try: session = await _ensure_issue_session() - args = _build_issue_args(title, body) + full_body = _build_issue_body(body) + args = _build_issue_args(title, full_body) + result = await session.session.call_tool("issue_write", arguments=args) _bridge_to_work_order(title, body, _category_from_labels(labels)) diff --git a/tests/timmy/test_mcp_tools.py b/tests/timmy/test_mcp_tools.py index a86c00e0..844ca6b3 100644 --- a/tests/timmy/test_mcp_tools.py +++ b/tests/timmy/test_mcp_tools.py @@ -6,6 +6,9 @@ import pytest from timmy.mcp_tools import ( _bridge_to_work_order, + _build_issue_args, + _build_issue_body, + _category_from_labels, _generate_avatar_image, _parse_command, close_mcp_sessions, @@ -132,6 +135,49 @@ def test_filesystem_mcp_returns_tools(): assert "/home/user/project" in params_kwargs["args"] +# --------------------------------------------------------------------------- +# _build_issue_body / _build_issue_args / _category_from_labels +# --------------------------------------------------------------------------- + + +def test_build_issue_body_appends_signature(): + """_build_issue_body appends the auto-filing signature.""" + result = _build_issue_body("Some description") + assert result.startswith("Some description\n\n") + assert "Auto-filed by Timmy" in result + + +def test_build_issue_body_empty(): + """_build_issue_body handles empty body.""" + result = _build_issue_body("") + assert result.startswith("---\n") + + +def test_build_issue_args(): + """_build_issue_args returns correct MCP arguments.""" + with patch("timmy.mcp_tools.settings") as mock_settings: + mock_settings.gitea_repo = "owner/repo" + result = _build_issue_args("Title", "Body") + assert result == { + "method": "create", + "owner": "owner", + "repo": "repo", + "title": "Title", + "body": "Body", + } + + +def test_category_from_labels_bug(): + """_category_from_labels returns 'bug' when labels contain bug.""" + assert _category_from_labels("bug, enhancement") == "bug" + + +def test_category_from_labels_default(): + """_category_from_labels returns 'suggestion' by default.""" + assert _category_from_labels("enhancement") == "suggestion" + assert _category_from_labels("") == "suggestion" + + # --------------------------------------------------------------------------- # create_gitea_issue_via_mcp # ---------------------------------------------------------------------------