79 lines
2.5 KiB
Python
79 lines
2.5 KiB
Python
from typing import Any
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from pydantic import BaseModel, Field
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.auth.deps import get_current_user
|
|
from app.db.base import get_db
|
|
from app.db.models import User
|
|
from app.projects.service import ProjectService
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
class GiteaBinding(BaseModel):
|
|
gitea_owner: str = Field(min_length=1)
|
|
gitea_repo: str = Field(min_length=1)
|
|
default_branch: str = "main"
|
|
|
|
|
|
class WorkItemCreate(BaseModel):
|
|
text: str = Field(min_length=1)
|
|
project_slug: str | None = None
|
|
|
|
|
|
@router.get("/projects")
|
|
def list_projects(db: Session = Depends(get_db), user: User = Depends(get_current_user)) -> list[dict[str, Any]]:
|
|
return ProjectService(db, user.id).list_projects()
|
|
|
|
|
|
@router.post("/projects/sync-taiga")
|
|
def sync_taiga_projects(db: Session = Depends(get_db), user: User = Depends(get_current_user)) -> list[dict[str, Any]]:
|
|
try:
|
|
return ProjectService(db, user.id).sync_taiga_projects()
|
|
except ValueError as exc:
|
|
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
|
|
|
|
|
@router.put("/projects/{taiga_slug}/gitea")
|
|
def bind_gitea(
|
|
taiga_slug: str,
|
|
payload: GiteaBinding,
|
|
db: Session = Depends(get_db), user: User = Depends(get_current_user),
|
|
) -> dict[str, Any]:
|
|
try:
|
|
return ProjectService(db, user.id).bind_gitea(
|
|
taiga_slug,
|
|
payload.gitea_owner,
|
|
payload.gitea_repo,
|
|
payload.default_branch,
|
|
)
|
|
except ValueError as exc:
|
|
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
|
|
|
|
|
@router.post("/work-items")
|
|
async def create_work_item(
|
|
payload: WorkItemCreate,
|
|
db: Session = Depends(get_db), user: User = Depends(get_current_user),
|
|
) -> dict[str, Any]:
|
|
try:
|
|
return await ProjectService(db, user.id).create_work_item(
|
|
payload.text,
|
|
project_slug=payload.project_slug,
|
|
)
|
|
except ValueError as exc:
|
|
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
|
except Exception as exc:
|
|
raise HTTPException(status_code=502, detail=str(exc)) from exc
|
|
|
|
|
|
@router.get("/work-items")
|
|
def list_work_items(
|
|
limit: int = 30,
|
|
status: str | None = None,
|
|
db: Session = Depends(get_db), user: User = Depends(get_current_user),
|
|
) -> list[dict[str, Any]]:
|
|
return ProjectService(db, user.id).list_work_items(limit=limit, status=status)
|