"""API /api/repos — proxy autenticato verso Gitea.""" from __future__ import annotations from fastapi import APIRouter, Depends, HTTPException, Query from core import gitea from core.auth import require_auth from core.model_spec import fetch_and_parse_spec router = APIRouter(prefix="/api/repos", tags=["repos"]) @router.get("") async def list_repos(user=Depends(require_auth)): try: return await gitea.list_repos() except Exception as e: raise HTTPException(502, f"gitea: {e}") @router.get("/{owner}/{repo}/branches") async def branches(owner: str, repo: str, user=Depends(require_auth)): try: return await gitea.list_branches(f"{owner}/{repo}") except Exception as e: raise HTTPException(502, f"gitea: {e}") @router.get("/{owner}/{repo}/commits") async def commits(owner: str, repo: str, branch: str = Query("main"), user=Depends(require_auth)): try: return await gitea.list_commits(f"{owner}/{repo}", branch) except Exception as e: raise HTTPException(502, f"gitea: {e}") @router.get("/{owner}/{repo}/file") async def file_raw(owner: str, repo: str, ref: str, path: str, user=Depends(require_auth)): try: raw = await gitea.get_file_raw(f"{owner}/{repo}", ref, path) return {"content": raw.decode("utf-8", errors="replace"), "size": len(raw)} except Exception as e: raise HTTPException(404, f"file not found: {e}") @router.get("/{owner}/{repo}/spec") async def spec(owner: str, repo: str, ref: str = Query("main"), user=Depends(require_auth)): s = await fetch_and_parse_spec(f"{owner}/{repo}", ref) if s is None: raise HTTPException(404, "model.yml not found at ref") return s