From ecc0acc8dd884cda652e16ef7daa5b2cc1babfba Mon Sep 17 00:00:00 2001 From: Fusion Date: Thu, 21 May 2026 10:47:30 +0200 Subject: [PATCH] fix(logging): add detailed error logging to git repository endpoints and utilities --- apps/api/src/api/git_repositories.py | 17 +++++++++++++++++ apps/api/src/utils/git_files.py | 12 ++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/apps/api/src/api/git_repositories.py b/apps/api/src/api/git_repositories.py index 184ce34..4d04789 100644 --- a/apps/api/src/api/git_repositories.py +++ b/apps/api/src/api/git_repositories.py @@ -1,3 +1,4 @@ +import logging import os import shutil import subprocess @@ -36,6 +37,8 @@ from src.utils.git_url_parser import parse_git_url router = APIRouter(prefix="/projects", tags=["git-repositories"]) +logger = logging.getLogger(__name__) + async def _get_user(session: AsyncSession, user_id: uuid.UUID) -> User: """Fetch a user by ID or raise 401 if not found.""" @@ -494,6 +497,14 @@ async def list_repository_files( ], ) except RuntimeError as e: + logger.error( + "Failed to list files for repo %s (path=%s, branch=%s): %s", + repo_id, + path, + branch, + str(e), + exc_info=True, + ) raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e)) @@ -599,6 +610,12 @@ async def get_repository_branches( default_branch=default_branch, ) except RuntimeError as e: + logger.error( + "Failed to list branches for repo %s: %s", + repo_id, + str(e), + exc_info=True, + ) raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e)) diff --git a/apps/api/src/utils/git_files.py b/apps/api/src/utils/git_files.py index d98cfa3..510f210 100644 --- a/apps/api/src/utils/git_files.py +++ b/apps/api/src/utils/git_files.py @@ -54,6 +54,8 @@ def _run_git_command(repo_path: str, *args: str) -> str: return result.stdout +logger = logging.getLogger(__name__) + def list_tree(repo_path: str, branch: str = "main", path: str = "") -> list[FileTreeEntry]: """List files and directories in a repository path. @@ -69,12 +71,14 @@ def list_tree(repo_path: str, branch: str = "main", path: str = "") -> list[File try: output = _run_git_command(repo_path, "ls-tree", "-l", tree_path) - except RuntimeError: + except RuntimeError as e: + logger.warning("git ls-tree failed for %s with branch '%s': %s", repo_path, tree_path, str(e)) # Try with HEAD if branch doesn't exist tree_path = f"HEAD:{path}" if path else "HEAD" try: output = _run_git_command(repo_path, "ls-tree", "-l", tree_path) except RuntimeError as e: + logger.error("git ls-tree failed for %s with HEAD: %s", repo_path, str(e)) # Check if this is an empty repository (no commits yet) error_msg = str(e).lower() if "not a valid object name" in error_msg or "does not exist" in error_msg: @@ -256,7 +260,11 @@ def list_branches(repo_path: str) -> tuple[list[BranchInfo], str]: Tuple of (list of BranchInfo, default branch name) """ # Get all branches - output = _run_git_command(repo_path, "branch", "-a", "--format=%(refname:short)") + try: + output = _run_git_command(repo_path, "branch", "-a", "--format=%(refname:short)") + except RuntimeError as e: + logger.error("Failed to list branches for %s: %s", repo_path, str(e)) + raise branches: list[BranchInfo] = [] default_branch = "main"