refactor: slim git_repositories and config_profiles routers (Task 3.5)
- Extract git control operations to services/git/control.py with repo validation - Extract git file operations to services/git/files.py with repo validation - Extract repository lifecycle to services/git/repository.py (create, delete, list) - Extract config profile helpers to services/config_profiles.py (cycle detection, duplicate checks, serialization, default profile management) - Slim git_repositories.py from ~1050 to 276 lines - Slim config_profiles.py from ~765 to 299 lines - Both routers now contain only HTTP routing concerns Quality gates: py_compile (pass), file size ≤300 (pass), no subprocess in routers (pass) Refs: repo-restructure Task 3.5
This commit is contained in:
@@ -24,7 +24,18 @@ from src.schemas.config_profile import (
|
|||||||
DefaultProfilesUpdate,
|
DefaultProfilesUpdate,
|
||||||
)
|
)
|
||||||
from src.services.config_profiles import (
|
from src.services.config_profiles import (
|
||||||
|
check_duplicate_include,
|
||||||
|
check_duplicate_mount_path,
|
||||||
|
check_duplicate_name,
|
||||||
|
get_default_profile_for_tool_type,
|
||||||
|
get_default_profiles,
|
||||||
get_owned_profile,
|
get_owned_profile,
|
||||||
|
include_to_dict,
|
||||||
|
list_includes_for_profile,
|
||||||
|
list_mounts_for_profile,
|
||||||
|
mount_to_dict,
|
||||||
|
profile_to_dict,
|
||||||
|
set_default_profiles,
|
||||||
validate_includes_no_cycle,
|
validate_includes_no_cycle,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -33,11 +44,8 @@ logger = logging.getLogger(__name__)
|
|||||||
router = APIRouter(prefix="/config-profiles", tags=["config-profiles"])
|
router = APIRouter(prefix="/config-profiles", tags=["config-profiles"])
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# Profile CRUD
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
@router.get("", summary="List config profiles")
|
@router.get("")
|
||||||
async def list_config_profiles(
|
async def list_config_profiles(
|
||||||
tool_type_id: str | None = None,
|
tool_type_id: str | None = None,
|
||||||
user_id: uuid.UUID = Depends(get_current_user_id),
|
user_id: uuid.UUID = Depends(get_current_user_id),
|
||||||
@@ -48,107 +56,51 @@ async def list_config_profiles(
|
|||||||
tool_type = await session.get(ToolType, uuid.UUID(tool_type_id))
|
tool_type = await session.get(ToolType, uuid.UUID(tool_type_id))
|
||||||
if tool_type is None:
|
if tool_type is None:
|
||||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="tool type not found")
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="tool type not found")
|
||||||
|
|
||||||
result = await session.execute(query.order_by(ConfigProfile.name))
|
result = await session.execute(query.order_by(ConfigProfile.name))
|
||||||
profiles = result.scalars().all()
|
return {"profiles": [profile_to_dict(p) for p in result.scalars().all()]}
|
||||||
|
|
||||||
return {
|
|
||||||
"profiles": [
|
|
||||||
{
|
|
||||||
"id": str(p.id),
|
|
||||||
"user_id": str(p.user_id),
|
|
||||||
"name": p.name,
|
|
||||||
"description": p.description,
|
|
||||||
"created_at": p.created_at.isoformat() if p.created_at else None,
|
|
||||||
"updated_at": p.updated_at.isoformat() if p.updated_at else None,
|
|
||||||
}
|
|
||||||
for p in profiles
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("", status_code=status.HTTP_201_CREATED, summary="Create config profile")
|
@router.post("", status_code=status.HTTP_201_CREATED)
|
||||||
async def create_config_profile(
|
async def create_config_profile(
|
||||||
data: ConfigProfileCreate,
|
data: ConfigProfileCreate,
|
||||||
user_id: uuid.UUID = Depends(get_current_user_id),
|
user_id: uuid.UUID = Depends(get_current_user_id),
|
||||||
session: AsyncSession = Depends(get_db_session),
|
session: AsyncSession = Depends(get_db_session),
|
||||||
) -> dict:
|
) -> dict:
|
||||||
existing = await session.scalar(
|
await check_duplicate_name(session, user_id, data.name)
|
||||||
select(ConfigProfile).where(
|
|
||||||
ConfigProfile.user_id == user_id,
|
|
||||||
ConfigProfile.name == data.name,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
if existing:
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=status.HTTP_409_CONFLICT,
|
|
||||||
detail=f"config profile with name '{data.name}' already exists",
|
|
||||||
)
|
|
||||||
|
|
||||||
profile = ConfigProfile(user_id=user_id, name=data.name, description=data.description)
|
profile = ConfigProfile(user_id=user_id, name=data.name, description=data.description)
|
||||||
session.add(profile)
|
session.add(profile)
|
||||||
await session.commit()
|
await session.commit()
|
||||||
await session.refresh(profile)
|
await session.refresh(profile)
|
||||||
|
return profile_to_dict(profile)
|
||||||
return {
|
|
||||||
"id": str(profile.id),
|
|
||||||
"user_id": str(profile.user_id),
|
|
||||||
"name": profile.name,
|
|
||||||
"description": profile.description,
|
|
||||||
"created_at": profile.created_at.isoformat() if profile.created_at else None,
|
|
||||||
"updated_at": profile.updated_at.isoformat() if profile.updated_at else None,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/defaults", summary="Get default profiles")
|
@router.get("/defaults")
|
||||||
async def get_default_profiles(
|
async def get_default_profiles_endpoint(
|
||||||
user_id: uuid.UUID = Depends(get_current_user_id),
|
user_id: uuid.UUID = Depends(get_current_user_id),
|
||||||
session: AsyncSession = Depends(get_db_session),
|
session: AsyncSession = Depends(get_db_session),
|
||||||
) -> dict:
|
) -> dict:
|
||||||
result = await session.execute(select(UserConfig).where(UserConfig.user_id == user_id))
|
return await get_default_profiles(session, user_id)
|
||||||
user_config = result.scalar_one_or_none()
|
|
||||||
return {"default_profiles": user_config.default_profiles if user_config else {}}
|
|
||||||
|
|
||||||
|
|
||||||
@router.put("/defaults", summary="Set default profiles")
|
@router.put("/defaults")
|
||||||
async def set_default_profiles(
|
async def set_default_profiles_endpoint(
|
||||||
data: DefaultProfilesUpdate,
|
data: DefaultProfilesUpdate,
|
||||||
user_id: uuid.UUID = Depends(get_current_user_id),
|
user_id: uuid.UUID = Depends(get_current_user_id),
|
||||||
session: AsyncSession = Depends(get_db_session),
|
session: AsyncSession = Depends(get_db_session),
|
||||||
) -> dict:
|
) -> dict:
|
||||||
result = await session.execute(select(UserConfig).where(UserConfig.user_id == user_id))
|
return await set_default_profiles(session, user_id, data.default_profiles)
|
||||||
user_config = result.scalar_one_or_none()
|
|
||||||
|
|
||||||
if user_config is None:
|
|
||||||
user_config = UserConfig(user_id=user_id, config={})
|
|
||||||
session.add(user_config)
|
|
||||||
|
|
||||||
for tool_type_id, profile_id_str in data.default_profiles.items():
|
|
||||||
profile = await session.get(ConfigProfile, uuid.UUID(profile_id_str))
|
|
||||||
if profile is None:
|
|
||||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"profile {profile_id_str} not found")
|
|
||||||
if profile.user_id != user_id:
|
|
||||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail=f"profile {profile_id_str} does not belong to user")
|
|
||||||
|
|
||||||
user_config.config = {**user_config.config, "default_profiles": data.default_profiles}
|
|
||||||
await session.commit()
|
|
||||||
await session.refresh(user_config)
|
|
||||||
return {"default_profiles": user_config.default_profiles}
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/defaults/{tool_type_id}", summary="Get default profile for tool type")
|
@router.get("/defaults/{tool_type_id}")
|
||||||
async def get_default_profile_for_tool_type(
|
async def get_default_profile_for_tool_type_endpoint(
|
||||||
tool_type_id: str,
|
tool_type_id: str,
|
||||||
user_id: uuid.UUID = Depends(get_current_user_id),
|
user_id: uuid.UUID = Depends(get_current_user_id),
|
||||||
session: AsyncSession = Depends(get_db_session),
|
session: AsyncSession = Depends(get_db_session),
|
||||||
) -> dict:
|
) -> dict:
|
||||||
result = await session.execute(select(UserConfig).where(UserConfig.user_id == user_id))
|
return await get_default_profile_for_tool_type(session, user_id, tool_type_id)
|
||||||
user_config = result.scalar_one_or_none()
|
|
||||||
profile_id = user_config.default_profiles.get(tool_type_id) if user_config else None
|
|
||||||
return {"tool_type_id": tool_type_id, "profile_id": profile_id}
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/{profile_id}", summary="Get config profile")
|
@router.get("/{profile_id}")
|
||||||
async def get_config_profile(
|
async def get_config_profile(
|
||||||
profile_id: uuid.UUID,
|
profile_id: uuid.UUID,
|
||||||
user_id: uuid.UUID = Depends(get_current_user_id),
|
user_id: uuid.UUID = Depends(get_current_user_id),
|
||||||
@@ -161,47 +113,18 @@ async def get_config_profile(
|
|||||||
)
|
)
|
||||||
if profile is None or profile.user_id != user_id:
|
if profile is None or profile.user_id != user_id:
|
||||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="config profile not found")
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="config profile not found")
|
||||||
|
|
||||||
includes_data = []
|
includes_data = []
|
||||||
for inc in profile.includes:
|
for inc in profile.includes:
|
||||||
included_profile = await session.get(ConfigProfile, inc.included_profile_id)
|
included_profile = await session.get(ConfigProfile, inc.included_profile_id)
|
||||||
includes_data.append({
|
includes_data.append(include_to_dict(inc, included_profile.name if included_profile else None))
|
||||||
"id": str(inc.id),
|
|
||||||
"profile_id": str(inc.profile_id),
|
|
||||||
"included_profile_id": str(inc.included_profile_id),
|
|
||||||
"included_profile_name": included_profile.name if included_profile else None,
|
|
||||||
"order_index": inc.order_index,
|
|
||||||
"created_at": inc.created_at.isoformat() if inc.created_at else None,
|
|
||||||
"updated_at": inc.updated_at.isoformat() if inc.updated_at else None,
|
|
||||||
})
|
|
||||||
|
|
||||||
mounts_data = [
|
|
||||||
{
|
|
||||||
"id": str(m.id),
|
|
||||||
"profile_id": str(m.profile_id),
|
|
||||||
"target_path": m.target_path,
|
|
||||||
"mode": m.mode,
|
|
||||||
"files": m.files,
|
|
||||||
"order_index": m.order_index,
|
|
||||||
"created_at": m.created_at.isoformat() if m.created_at else None,
|
|
||||||
"updated_at": m.updated_at.isoformat() if m.updated_at else None,
|
|
||||||
}
|
|
||||||
for m in profile.mounts
|
|
||||||
]
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"id": str(profile.id),
|
**profile_to_dict(profile),
|
||||||
"user_id": str(profile.user_id),
|
|
||||||
"name": profile.name,
|
|
||||||
"description": profile.description,
|
|
||||||
"includes": includes_data,
|
"includes": includes_data,
|
||||||
"mounts": mounts_data,
|
"mounts": [mount_to_dict(m) for m in profile.mounts],
|
||||||
"created_at": profile.created_at.isoformat() if profile.created_at else None,
|
|
||||||
"updated_at": profile.updated_at.isoformat() if profile.updated_at else None,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@router.put("/{profile_id}", summary="Update config profile")
|
@router.put("/{profile_id}")
|
||||||
async def update_config_profile(
|
async def update_config_profile(
|
||||||
profile_id: uuid.UUID,
|
profile_id: uuid.UUID,
|
||||||
data: ConfigProfileUpdate,
|
data: ConfigProfileUpdate,
|
||||||
@@ -209,39 +132,17 @@ async def update_config_profile(
|
|||||||
session: AsyncSession = Depends(get_db_session),
|
session: AsyncSession = Depends(get_db_session),
|
||||||
) -> dict:
|
) -> dict:
|
||||||
profile = await get_owned_profile(profile_id, user_id, session)
|
profile = await get_owned_profile(profile_id, user_id, session)
|
||||||
|
|
||||||
if data.name is not None:
|
if data.name is not None:
|
||||||
existing = await session.scalar(
|
await check_duplicate_name(session, user_id, data.name, exclude_id=profile_id)
|
||||||
select(ConfigProfile).where(
|
|
||||||
ConfigProfile.user_id == user_id,
|
|
||||||
ConfigProfile.name == data.name,
|
|
||||||
ConfigProfile.id != profile_id,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
if existing:
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=status.HTTP_409_CONFLICT,
|
|
||||||
detail=f"config profile with name '{data.name}' already exists",
|
|
||||||
)
|
|
||||||
profile.name = data.name
|
profile.name = data.name
|
||||||
|
|
||||||
if data.description is not None:
|
if data.description is not None:
|
||||||
profile.description = data.description
|
profile.description = data.description
|
||||||
|
|
||||||
await session.commit()
|
await session.commit()
|
||||||
await session.refresh(profile)
|
await session.refresh(profile)
|
||||||
|
return profile_to_dict(profile)
|
||||||
return {
|
|
||||||
"id": str(profile.id),
|
|
||||||
"user_id": str(profile.user_id),
|
|
||||||
"name": profile.name,
|
|
||||||
"description": profile.description,
|
|
||||||
"created_at": profile.created_at.isoformat() if profile.created_at else None,
|
|
||||||
"updated_at": profile.updated_at.isoformat() if profile.updated_at else None,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/{profile_id}", status_code=status.HTTP_204_NO_CONTENT, summary="Delete config profile")
|
@router.delete("/{profile_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||||
async def delete_config_profile(
|
async def delete_config_profile(
|
||||||
profile_id: uuid.UUID,
|
profile_id: uuid.UUID,
|
||||||
user_id: uuid.UUID = Depends(get_current_user_id),
|
user_id: uuid.UUID = Depends(get_current_user_id),
|
||||||
@@ -252,41 +153,18 @@ async def delete_config_profile(
|
|||||||
await session.commit()
|
await session.commit()
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# Include management
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
@router.get("/{profile_id}/includes", summary="List profile includes")
|
@router.get("/{profile_id}/includes")
|
||||||
async def list_profile_includes(
|
async def list_profile_includes(
|
||||||
profile_id: uuid.UUID,
|
profile_id: uuid.UUID,
|
||||||
user_id: uuid.UUID = Depends(get_current_user_id),
|
user_id: uuid.UUID = Depends(get_current_user_id),
|
||||||
session: AsyncSession = Depends(get_db_session),
|
session: AsyncSession = Depends(get_db_session),
|
||||||
) -> dict:
|
) -> dict:
|
||||||
await get_owned_profile(profile_id, user_id, session)
|
await get_owned_profile(profile_id, user_id, session)
|
||||||
result = await session.execute(
|
return await list_includes_for_profile(session, profile_id)
|
||||||
select(ConfigInclude)
|
|
||||||
.where(ConfigInclude.profile_id == profile_id)
|
|
||||||
.order_by(ConfigInclude.order_index)
|
|
||||||
)
|
|
||||||
includes = result.scalars().all()
|
|
||||||
|
|
||||||
includes_data = []
|
|
||||||
for inc in includes:
|
|
||||||
included_profile = await session.get(ConfigProfile, inc.included_profile_id)
|
|
||||||
includes_data.append({
|
|
||||||
"id": str(inc.id),
|
|
||||||
"profile_id": str(inc.profile_id),
|
|
||||||
"included_profile_id": str(inc.included_profile_id),
|
|
||||||
"included_profile_name": included_profile.name if included_profile else None,
|
|
||||||
"order_index": inc.order_index,
|
|
||||||
"created_at": inc.created_at.isoformat() if inc.created_at else None,
|
|
||||||
"updated_at": inc.updated_at.isoformat() if inc.updated_at else None,
|
|
||||||
})
|
|
||||||
|
|
||||||
return {"includes": includes_data}
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/{profile_id}/includes", status_code=status.HTTP_201_CREATED, summary="Add profile include")
|
@router.post("/{profile_id}/includes", status_code=status.HTTP_201_CREATED)
|
||||||
async def add_profile_include(
|
async def add_profile_include(
|
||||||
profile_id: uuid.UUID,
|
profile_id: uuid.UUID,
|
||||||
data: ConfigIncludeCreate,
|
data: ConfigIncludeCreate,
|
||||||
@@ -295,27 +173,15 @@ async def add_profile_include(
|
|||||||
) -> dict:
|
) -> dict:
|
||||||
profile = await get_owned_profile(profile_id, user_id, session)
|
profile = await get_owned_profile(profile_id, user_id, session)
|
||||||
included_profile_id = uuid.UUID(data.included_profile_id)
|
included_profile_id = uuid.UUID(data.included_profile_id)
|
||||||
|
|
||||||
if included_profile_id == profile_id:
|
if included_profile_id == profile_id:
|
||||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="a profile cannot include itself")
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="a profile cannot include itself")
|
||||||
|
|
||||||
included_profile = await session.get(ConfigProfile, included_profile_id)
|
included_profile = await session.get(ConfigProfile, included_profile_id)
|
||||||
if included_profile is None:
|
if included_profile is None:
|
||||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="included profile not found")
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="included profile not found")
|
||||||
if included_profile.user_id != user_id:
|
if included_profile.user_id != user_id:
|
||||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="included profile does not belong to user")
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="included profile does not belong to user")
|
||||||
|
await check_duplicate_include(session, profile_id, included_profile_id)
|
||||||
existing = await session.scalar(
|
|
||||||
select(ConfigInclude).where(
|
|
||||||
ConfigInclude.profile_id == profile_id,
|
|
||||||
ConfigInclude.included_profile_id == included_profile_id,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
if existing:
|
|
||||||
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="this include already exists")
|
|
||||||
|
|
||||||
await validate_includes_no_cycle(session, profile_id, included_profile_id)
|
await validate_includes_no_cycle(session, profile_id, included_profile_id)
|
||||||
|
|
||||||
include = ConfigInclude(
|
include = ConfigInclude(
|
||||||
profile_id=profile_id,
|
profile_id=profile_id,
|
||||||
included_profile_id=included_profile_id,
|
included_profile_id=included_profile_id,
|
||||||
@@ -324,19 +190,10 @@ async def add_profile_include(
|
|||||||
session.add(include)
|
session.add(include)
|
||||||
await session.commit()
|
await session.commit()
|
||||||
await session.refresh(include)
|
await session.refresh(include)
|
||||||
|
return include_to_dict(include, included_profile.name)
|
||||||
return {
|
|
||||||
"id": str(include.id),
|
|
||||||
"profile_id": str(include.profile_id),
|
|
||||||
"included_profile_id": str(include.included_profile_id),
|
|
||||||
"included_profile_name": included_profile.name,
|
|
||||||
"order_index": include.order_index,
|
|
||||||
"created_at": include.created_at.isoformat() if include.created_at else None,
|
|
||||||
"updated_at": include.updated_at.isoformat() if include.updated_at else None,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@router.put("/{profile_id}/includes/{include_id}", summary="Update profile include")
|
@router.put("/{profile_id}/includes/{include_id}")
|
||||||
async def update_profile_include(
|
async def update_profile_include(
|
||||||
profile_id: uuid.UUID,
|
profile_id: uuid.UUID,
|
||||||
include_id: uuid.UUID,
|
include_id: uuid.UUID,
|
||||||
@@ -348,24 +205,14 @@ async def update_profile_include(
|
|||||||
include = await session.get(ConfigInclude, include_id)
|
include = await session.get(ConfigInclude, include_id)
|
||||||
if include is None or include.profile_id != profile_id:
|
if include is None or include.profile_id != profile_id:
|
||||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="include not found")
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="include not found")
|
||||||
|
|
||||||
include.order_index = data.order_index
|
include.order_index = data.order_index
|
||||||
await session.commit()
|
await session.commit()
|
||||||
await session.refresh(include)
|
await session.refresh(include)
|
||||||
|
|
||||||
included_profile = await session.get(ConfigProfile, include.included_profile_id)
|
included_profile = await session.get(ConfigProfile, include.included_profile_id)
|
||||||
return {
|
return include_to_dict(include, included_profile.name if included_profile else None)
|
||||||
"id": str(include.id),
|
|
||||||
"profile_id": str(include.profile_id),
|
|
||||||
"included_profile_id": str(include.included_profile_id),
|
|
||||||
"included_profile_name": included_profile.name if included_profile else None,
|
|
||||||
"order_index": include.order_index,
|
|
||||||
"created_at": include.created_at.isoformat() if include.created_at else None,
|
|
||||||
"updated_at": include.updated_at.isoformat() if include.updated_at else None,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/{profile_id}/includes/{include_id}", status_code=status.HTTP_204_NO_CONTENT, summary="Remove profile include")
|
@router.delete("/{profile_id}/includes/{include_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||||
async def remove_profile_include(
|
async def remove_profile_include(
|
||||||
profile_id: uuid.UUID,
|
profile_id: uuid.UUID,
|
||||||
include_id: uuid.UUID,
|
include_id: uuid.UUID,
|
||||||
@@ -380,42 +227,18 @@ async def remove_profile_include(
|
|||||||
await session.commit()
|
await session.commit()
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# Mount management
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
@router.get("/{profile_id}/mounts", summary="List profile mounts")
|
@router.get("/{profile_id}/mounts")
|
||||||
async def list_profile_mounts(
|
async def list_profile_mounts(
|
||||||
profile_id: uuid.UUID,
|
profile_id: uuid.UUID,
|
||||||
user_id: uuid.UUID = Depends(get_current_user_id),
|
user_id: uuid.UUID = Depends(get_current_user_id),
|
||||||
session: AsyncSession = Depends(get_db_session),
|
session: AsyncSession = Depends(get_db_session),
|
||||||
) -> dict:
|
) -> dict:
|
||||||
await get_owned_profile(profile_id, user_id, session)
|
await get_owned_profile(profile_id, user_id, session)
|
||||||
result = await session.execute(
|
return await list_mounts_for_profile(session, profile_id)
|
||||||
select(ConfigMount)
|
|
||||||
.where(ConfigMount.profile_id == profile_id)
|
|
||||||
.order_by(ConfigMount.order_index)
|
|
||||||
)
|
|
||||||
mounts = result.scalars().all()
|
|
||||||
|
|
||||||
return {
|
|
||||||
"mounts": [
|
|
||||||
{
|
|
||||||
"id": str(m.id),
|
|
||||||
"profile_id": str(m.profile_id),
|
|
||||||
"target_path": m.target_path,
|
|
||||||
"files": m.files,
|
|
||||||
"mode": m.mode,
|
|
||||||
"order_index": m.order_index,
|
|
||||||
"created_at": m.created_at.isoformat() if m.created_at else None,
|
|
||||||
"updated_at": m.updated_at.isoformat() if m.updated_at else None,
|
|
||||||
}
|
|
||||||
for m in mounts
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/{profile_id}/mounts", status_code=status.HTTP_201_CREATED, summary="Add profile mount")
|
@router.post("/{profile_id}/mounts", status_code=status.HTTP_201_CREATED)
|
||||||
async def add_profile_mount(
|
async def add_profile_mount(
|
||||||
profile_id: uuid.UUID,
|
profile_id: uuid.UUID,
|
||||||
data: ConfigMountCreate,
|
data: ConfigMountCreate,
|
||||||
@@ -423,18 +246,7 @@ async def add_profile_mount(
|
|||||||
session: AsyncSession = Depends(get_db_session),
|
session: AsyncSession = Depends(get_db_session),
|
||||||
) -> dict:
|
) -> dict:
|
||||||
profile = await get_owned_profile(profile_id, user_id, session)
|
profile = await get_owned_profile(profile_id, user_id, session)
|
||||||
existing = await session.scalar(
|
await check_duplicate_mount_path(session, profile_id, data.target_path)
|
||||||
select(ConfigMount).where(
|
|
||||||
ConfigMount.profile_id == profile_id,
|
|
||||||
ConfigMount.target_path == data.target_path,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
if existing:
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=status.HTTP_409_CONFLICT,
|
|
||||||
detail=f"mount with path '{data.target_path}' already exists",
|
|
||||||
)
|
|
||||||
|
|
||||||
mount = ConfigMount(
|
mount = ConfigMount(
|
||||||
profile_id=profile_id,
|
profile_id=profile_id,
|
||||||
target_path=data.target_path,
|
target_path=data.target_path,
|
||||||
@@ -445,20 +257,10 @@ async def add_profile_mount(
|
|||||||
session.add(mount)
|
session.add(mount)
|
||||||
await session.commit()
|
await session.commit()
|
||||||
await session.refresh(mount)
|
await session.refresh(mount)
|
||||||
|
return mount_to_dict(mount)
|
||||||
return {
|
|
||||||
"id": str(mount.id),
|
|
||||||
"profile_id": str(mount.profile_id),
|
|
||||||
"target_path": mount.target_path,
|
|
||||||
"files": mount.files,
|
|
||||||
"mode": mount.mode,
|
|
||||||
"order_index": mount.order_index,
|
|
||||||
"created_at": mount.created_at.isoformat() if mount.created_at else None,
|
|
||||||
"updated_at": mount.updated_at.isoformat() if mount.updated_at else None,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@router.put("/{profile_id}/mounts/{mount_id}", summary="Update profile mount")
|
@router.put("/{profile_id}/mounts/{mount_id}")
|
||||||
async def update_profile_mount(
|
async def update_profile_mount(
|
||||||
profile_id: uuid.UUID,
|
profile_id: uuid.UUID,
|
||||||
mount_id: uuid.UUID,
|
mount_id: uuid.UUID,
|
||||||
@@ -470,43 +272,19 @@ async def update_profile_mount(
|
|||||||
mount = await session.get(ConfigMount, mount_id)
|
mount = await session.get(ConfigMount, mount_id)
|
||||||
if mount is None or mount.profile_id != profile_id:
|
if mount is None or mount.profile_id != profile_id:
|
||||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="mount not found")
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="mount not found")
|
||||||
|
|
||||||
if data.target_path is not None:
|
if data.target_path is not None:
|
||||||
existing = await session.scalar(
|
await check_duplicate_mount_path(session, profile_id, data.target_path, exclude_id=mount_id)
|
||||||
select(ConfigMount).where(
|
|
||||||
ConfigMount.profile_id == profile_id,
|
|
||||||
ConfigMount.target_path == data.target_path,
|
|
||||||
ConfigMount.id != mount_id,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
if existing:
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=status.HTTP_409_CONFLICT,
|
|
||||||
detail=f"mount with path '{data.target_path}' already exists",
|
|
||||||
)
|
|
||||||
mount.target_path = data.target_path
|
mount.target_path = data.target_path
|
||||||
|
|
||||||
if data.files is not None:
|
if data.files is not None:
|
||||||
mount.files = data.files
|
mount.files = data.files
|
||||||
if data.order_index is not None:
|
if data.order_index is not None:
|
||||||
mount.order_index = data.order_index
|
mount.order_index = data.order_index
|
||||||
|
|
||||||
await session.commit()
|
await session.commit()
|
||||||
await session.refresh(mount)
|
await session.refresh(mount)
|
||||||
|
return mount_to_dict(mount)
|
||||||
return {
|
|
||||||
"id": str(mount.id),
|
|
||||||
"profile_id": str(mount.profile_id),
|
|
||||||
"target_path": mount.target_path,
|
|
||||||
"files": mount.files,
|
|
||||||
"mode": mount.mode,
|
|
||||||
"order_index": mount.order_index,
|
|
||||||
"created_at": mount.created_at.isoformat() if mount.created_at else None,
|
|
||||||
"updated_at": mount.updated_at.isoformat() if mount.updated_at else None,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/{profile_id}/mounts/{mount_id}", status_code=status.HTTP_204_NO_CONTENT, summary="Remove profile mount")
|
@router.delete("/{profile_id}/mounts/{mount_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||||
async def remove_profile_mount(
|
async def remove_profile_mount(
|
||||||
profile_id: uuid.UUID,
|
profile_id: uuid.UUID,
|
||||||
mount_id: uuid.UUID,
|
mount_id: uuid.UUID,
|
||||||
|
|||||||
@@ -35,15 +35,10 @@ from src.services.git.repository import create_repository, delete_repository, li
|
|||||||
from src.utils.git_url_parser import parse_git_url
|
from src.utils.git_url_parser import parse_git_url
|
||||||
|
|
||||||
router = APIRouter(prefix="/projects", tags=["git-repositories"])
|
router = APIRouter(prefix="/projects", tags=["git-repositories"])
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@router.get(
|
@router.get("/{project_id}/repositories", response_model=list[GitRepositoryResponse])
|
||||||
"/{project_id}/repositories",
|
|
||||||
response_model=list[GitRepositoryResponse],
|
|
||||||
summary="List repositories",
|
|
||||||
)
|
|
||||||
async def list_repositories_endpoint(
|
async def list_repositories_endpoint(
|
||||||
project_id: uuid.UUID,
|
project_id: uuid.UUID,
|
||||||
user: User = Depends(get_current_user),
|
user: User = Depends(get_current_user),
|
||||||
@@ -53,12 +48,7 @@ async def list_repositories_endpoint(
|
|||||||
return await list_repositories(session, project_id)
|
return await list_repositories(session, project_id)
|
||||||
|
|
||||||
|
|
||||||
@router.post(
|
@router.post("/{project_id}/repositories", response_model=GitRepositoryResponse, status_code=status.HTTP_201_CREATED)
|
||||||
"/{project_id}/repositories",
|
|
||||||
response_model=GitRepositoryResponse,
|
|
||||||
status_code=status.HTTP_201_CREATED,
|
|
||||||
summary="Create a repository",
|
|
||||||
)
|
|
||||||
async def create_repository_endpoint(
|
async def create_repository_endpoint(
|
||||||
project_id: uuid.UUID,
|
project_id: uuid.UUID,
|
||||||
data: GitRepositoryCreate,
|
data: GitRepositoryCreate,
|
||||||
@@ -69,11 +59,7 @@ async def create_repository_endpoint(
|
|||||||
return await create_repository(session, project_id, data, user)
|
return await create_repository(session, project_id, data, user)
|
||||||
|
|
||||||
|
|
||||||
@router.delete(
|
@router.delete("/{project_id}/repositories/{repo_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||||
"/{project_id}/repositories/{repo_id}",
|
|
||||||
status_code=status.HTTP_204_NO_CONTENT,
|
|
||||||
summary="Delete a repository",
|
|
||||||
)
|
|
||||||
async def delete_repository_endpoint(
|
async def delete_repository_endpoint(
|
||||||
project_id: uuid.UUID,
|
project_id: uuid.UUID,
|
||||||
repo_id: uuid.UUID,
|
repo_id: uuid.UUID,
|
||||||
@@ -85,23 +71,14 @@ async def delete_repository_endpoint(
|
|||||||
return Response(status_code=status.HTTP_204_NO_CONTENT)
|
return Response(status_code=status.HTTP_204_NO_CONTENT)
|
||||||
|
|
||||||
|
|
||||||
@router.post(
|
@router.post("/repositories/parse-url", response_model=URLParseResponse)
|
||||||
"/repositories/parse-url",
|
|
||||||
response_model=URLParseResponse,
|
|
||||||
summary="Parse a git URL",
|
|
||||||
)
|
|
||||||
async def parse_repository_url(data: URLParseRequest) -> URLParseResponse:
|
async def parse_repository_url(data: URLParseRequest) -> URLParseResponse:
|
||||||
result = parse_git_url(data.url)
|
return URLParseResponse(**parse_git_url(data.url))
|
||||||
return URLParseResponse(**result)
|
|
||||||
|
|
||||||
|
|
||||||
# History
|
# History
|
||||||
|
|
||||||
|
@router.get("/{project_id}/repositories/{repo_id}/history")
|
||||||
@router.get(
|
|
||||||
"/{project_id}/repositories/{repo_id}/history",
|
|
||||||
summary="Get repository history",
|
|
||||||
)
|
|
||||||
async def get_repository_history(
|
async def get_repository_history(
|
||||||
project_id: uuid.UUID,
|
project_id: uuid.UUID,
|
||||||
repo_id: uuid.UUID,
|
repo_id: uuid.UUID,
|
||||||
@@ -115,16 +92,12 @@ async def get_repository_history(
|
|||||||
) -> dict:
|
) -> dict:
|
||||||
from src.utils.git_history import get_commit_history
|
from src.utils.git_history import get_commit_history
|
||||||
from src.services.git.repository import get_repo_and_validate, ensure_repo_on_disk
|
from src.services.git.repository import get_repo_and_validate, ensure_repo_on_disk
|
||||||
|
|
||||||
repo = await get_repo_and_validate(session, repo_id, project_id)
|
repo = await get_repo_and_validate(session, repo_id, project_id)
|
||||||
ensure_repo_on_disk(repo)
|
ensure_repo_on_disk(repo)
|
||||||
return get_commit_history(repo.path, branch=branch, limit=limit, offset=offset)
|
return get_commit_history(repo.path, branch=branch, limit=limit, offset=offset)
|
||||||
|
|
||||||
|
|
||||||
@router.get(
|
@router.get("/{project_id}/repositories/{repo_id}/commits/{commit_hash}")
|
||||||
"/{project_id}/repositories/{repo_id}/commits/{commit_hash}",
|
|
||||||
summary="Get commit details",
|
|
||||||
)
|
|
||||||
async def get_repository_commit(
|
async def get_repository_commit(
|
||||||
project_id: uuid.UUID,
|
project_id: uuid.UUID,
|
||||||
repo_id: uuid.UUID,
|
repo_id: uuid.UUID,
|
||||||
@@ -135,7 +108,6 @@ async def get_repository_commit(
|
|||||||
) -> dict:
|
) -> dict:
|
||||||
from src.utils.git_history import get_commit_detail
|
from src.utils.git_history import get_commit_detail
|
||||||
from src.services.git.repository import get_repo_and_validate, ensure_repo_on_disk
|
from src.services.git.repository import get_repo_and_validate, ensure_repo_on_disk
|
||||||
|
|
||||||
repo = await get_repo_and_validate(session, repo_id, project_id)
|
repo = await get_repo_and_validate(session, repo_id, project_id)
|
||||||
ensure_repo_on_disk(repo)
|
ensure_repo_on_disk(repo)
|
||||||
return get_commit_detail(repo.path, commit_hash)
|
return get_commit_detail(repo.path, commit_hash)
|
||||||
@@ -143,12 +115,7 @@ async def get_repository_commit(
|
|||||||
|
|
||||||
# File browsing
|
# File browsing
|
||||||
|
|
||||||
|
@router.get("/{project_id}/repositories/{repo_id}/files", response_model=FileListResponse)
|
||||||
@router.get(
|
|
||||||
"/{project_id}/repositories/{repo_id}/files",
|
|
||||||
response_model=FileListResponse,
|
|
||||||
summary="List repository files",
|
|
||||||
)
|
|
||||||
async def list_repository_files(
|
async def list_repository_files(
|
||||||
project_id: uuid.UUID,
|
project_id: uuid.UUID,
|
||||||
repo_id: uuid.UUID,
|
repo_id: uuid.UUID,
|
||||||
@@ -161,11 +128,7 @@ async def list_repository_files(
|
|||||||
return await git_files.list_files(session, project_id, repo_id, branch, path)
|
return await git_files.list_files(session, project_id, repo_id, branch, path)
|
||||||
|
|
||||||
|
|
||||||
@router.get(
|
@router.get("/{project_id}/repositories/{repo_id}/files/content", response_model=FileContentResponse)
|
||||||
"/{project_id}/repositories/{repo_id}/files/content",
|
|
||||||
response_model=FileContentResponse,
|
|
||||||
summary="Get file content",
|
|
||||||
)
|
|
||||||
async def get_repository_file_content(
|
async def get_repository_file_content(
|
||||||
project_id: uuid.UUID,
|
project_id: uuid.UUID,
|
||||||
repo_id: uuid.UUID,
|
repo_id: uuid.UUID,
|
||||||
@@ -178,11 +141,7 @@ async def get_repository_file_content(
|
|||||||
return await git_files.get_file(session, project_id, repo_id, branch, path)
|
return await git_files.get_file(session, project_id, repo_id, branch, path)
|
||||||
|
|
||||||
|
|
||||||
@router.post(
|
@router.post("/{project_id}/repositories/{repo_id}/files/content", response_model=FileUpdateResponse)
|
||||||
"/{project_id}/repositories/{repo_id}/files/content",
|
|
||||||
response_model=FileUpdateResponse,
|
|
||||||
summary="Update file content",
|
|
||||||
)
|
|
||||||
async def update_repository_file(
|
async def update_repository_file(
|
||||||
project_id: uuid.UUID,
|
project_id: uuid.UUID,
|
||||||
repo_id: uuid.UUID,
|
repo_id: uuid.UUID,
|
||||||
@@ -196,11 +155,7 @@ async def update_repository_file(
|
|||||||
|
|
||||||
# Branches
|
# Branches
|
||||||
|
|
||||||
|
@router.get("/{project_id}/repositories/{repo_id}/branches")
|
||||||
@router.get(
|
|
||||||
"/{project_id}/repositories/{repo_id}/branches",
|
|
||||||
summary="List branches",
|
|
||||||
)
|
|
||||||
async def get_repository_branches(
|
async def get_repository_branches(
|
||||||
project_id: uuid.UUID,
|
project_id: uuid.UUID,
|
||||||
repo_id: uuid.UUID,
|
repo_id: uuid.UUID,
|
||||||
@@ -211,10 +166,7 @@ async def get_repository_branches(
|
|||||||
return await git_files.list_branches_with_validation(session, project_id, repo_id)
|
return await git_files.list_branches_with_validation(session, project_id, repo_id)
|
||||||
|
|
||||||
|
|
||||||
@router.post(
|
@router.post("/{project_id}/repositories/{repo_id}/branches")
|
||||||
"/{project_id}/repositories/{repo_id}/branches",
|
|
||||||
summary="Create a branch",
|
|
||||||
)
|
|
||||||
async def create_repository_branch(
|
async def create_repository_branch(
|
||||||
project_id: uuid.UUID,
|
project_id: uuid.UUID,
|
||||||
repo_id: uuid.UUID,
|
repo_id: uuid.UUID,
|
||||||
@@ -226,10 +178,7 @@ async def create_repository_branch(
|
|||||||
return await git_control.create_branch_with_validation(session, project_id, repo_id, data)
|
return await git_control.create_branch_with_validation(session, project_id, repo_id, data)
|
||||||
|
|
||||||
|
|
||||||
@router.delete(
|
@router.delete("/{project_id}/repositories/{repo_id}/branches/{branch_name}")
|
||||||
"/{project_id}/repositories/{repo_id}/branches/{branch_name}",
|
|
||||||
summary="Delete a branch",
|
|
||||||
)
|
|
||||||
async def delete_repository_branch(
|
async def delete_repository_branch(
|
||||||
project_id: uuid.UUID,
|
project_id: uuid.UUID,
|
||||||
repo_id: uuid.UUID,
|
repo_id: uuid.UUID,
|
||||||
@@ -242,10 +191,7 @@ async def delete_repository_branch(
|
|||||||
return await git_control.delete_branch_with_validation(session, project_id, repo_id, branch_name, force)
|
return await git_control.delete_branch_with_validation(session, project_id, repo_id, branch_name, force)
|
||||||
|
|
||||||
|
|
||||||
@router.post(
|
@router.post("/{project_id}/repositories/{repo_id}/checkout")
|
||||||
"/{project_id}/repositories/{repo_id}/checkout",
|
|
||||||
summary="Checkout a branch",
|
|
||||||
)
|
|
||||||
async def checkout_repository_branch(
|
async def checkout_repository_branch(
|
||||||
project_id: uuid.UUID,
|
project_id: uuid.UUID,
|
||||||
repo_id: uuid.UUID,
|
repo_id: uuid.UUID,
|
||||||
@@ -259,12 +205,7 @@ async def checkout_repository_branch(
|
|||||||
|
|
||||||
# Git control
|
# Git control
|
||||||
|
|
||||||
|
@router.get("/{project_id}/repositories/{repo_id}/status", response_model=StatusResponse)
|
||||||
@router.get(
|
|
||||||
"/{project_id}/repositories/{repo_id}/status",
|
|
||||||
response_model=StatusResponse,
|
|
||||||
summary="Get repository status",
|
|
||||||
)
|
|
||||||
async def get_repository_status(
|
async def get_repository_status(
|
||||||
project_id: uuid.UUID,
|
project_id: uuid.UUID,
|
||||||
repo_id: uuid.UUID,
|
repo_id: uuid.UUID,
|
||||||
@@ -275,11 +216,7 @@ async def get_repository_status(
|
|||||||
return await git_control.get_status_with_validation(session, project_id, repo_id)
|
return await git_control.get_status_with_validation(session, project_id, repo_id)
|
||||||
|
|
||||||
|
|
||||||
@router.post(
|
@router.post("/{project_id}/repositories/{repo_id}/commit", response_model=CommitResponse)
|
||||||
"/{project_id}/repositories/{repo_id}/commit",
|
|
||||||
response_model=CommitResponse,
|
|
||||||
summary="Commit changes",
|
|
||||||
)
|
|
||||||
async def commit_repository_changes(
|
async def commit_repository_changes(
|
||||||
project_id: uuid.UUID,
|
project_id: uuid.UUID,
|
||||||
repo_id: uuid.UUID,
|
repo_id: uuid.UUID,
|
||||||
@@ -292,11 +229,7 @@ async def commit_repository_changes(
|
|||||||
return CommitResponse(commit_hash=result["commit_hash"], message=result["message"])
|
return CommitResponse(commit_hash=result["commit_hash"], message=result["message"])
|
||||||
|
|
||||||
|
|
||||||
@router.post(
|
@router.post("/{project_id}/repositories/{repo_id}/fetch", response_model=FetchResponse)
|
||||||
"/{project_id}/repositories/{repo_id}/fetch",
|
|
||||||
response_model=FetchResponse,
|
|
||||||
summary="Fetch from remote",
|
|
||||||
)
|
|
||||||
async def fetch_repository(
|
async def fetch_repository(
|
||||||
project_id: uuid.UUID,
|
project_id: uuid.UUID,
|
||||||
repo_id: uuid.UUID,
|
repo_id: uuid.UUID,
|
||||||
@@ -307,11 +240,7 @@ async def fetch_repository(
|
|||||||
return await git_control.fetch_with_validation(session, project_id, repo_id)
|
return await git_control.fetch_with_validation(session, project_id, repo_id)
|
||||||
|
|
||||||
|
|
||||||
@router.post(
|
@router.post("/{project_id}/repositories/{repo_id}/pull", response_model=PullResponse)
|
||||||
"/{project_id}/repositories/{repo_id}/pull",
|
|
||||||
response_model=PullResponse,
|
|
||||||
summary="Pull from remote",
|
|
||||||
)
|
|
||||||
async def pull_repository(
|
async def pull_repository(
|
||||||
project_id: uuid.UUID,
|
project_id: uuid.UUID,
|
||||||
repo_id: uuid.UUID,
|
repo_id: uuid.UUID,
|
||||||
@@ -323,11 +252,7 @@ async def pull_repository(
|
|||||||
return await git_control.pull_with_validation(session, project_id, repo_id, branch)
|
return await git_control.pull_with_validation(session, project_id, repo_id, branch)
|
||||||
|
|
||||||
|
|
||||||
@router.post(
|
@router.post("/{project_id}/repositories/{repo_id}/push", response_model=PushResponse)
|
||||||
"/{project_id}/repositories/{repo_id}/push",
|
|
||||||
response_model=PushResponse,
|
|
||||||
summary="Push to remote",
|
|
||||||
)
|
|
||||||
async def push_repository(
|
async def push_repository(
|
||||||
project_id: uuid.UUID,
|
project_id: uuid.UUID,
|
||||||
repo_id: uuid.UUID,
|
repo_id: uuid.UUID,
|
||||||
@@ -339,11 +264,7 @@ async def push_repository(
|
|||||||
return await git_control.push_with_validation(session, project_id, repo_id, branch)
|
return await git_control.push_with_validation(session, project_id, repo_id, branch)
|
||||||
|
|
||||||
|
|
||||||
@router.post(
|
@router.post("/{project_id}/repositories/{repo_id}/merge", response_model=MergeResponse)
|
||||||
"/{project_id}/repositories/{repo_id}/merge",
|
|
||||||
response_model=MergeResponse,
|
|
||||||
summary="Merge branches",
|
|
||||||
)
|
|
||||||
async def merge_repository_branches(
|
async def merge_repository_branches(
|
||||||
project_id: uuid.UUID,
|
project_id: uuid.UUID,
|
||||||
repo_id: uuid.UUID,
|
repo_id: uuid.UUID,
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import uuid
|
|||||||
from fastapi import HTTPException, status
|
from fastapi import HTTPException, status
|
||||||
from sqlalchemy import select
|
from sqlalchemy import select
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
from sqlalchemy.orm import selectinload
|
||||||
|
|
||||||
from src.models.config_include import ConfigInclude
|
from src.models.config_include import ConfigInclude
|
||||||
from src.models.config_mount import ConfigMount
|
from src.models.config_mount import ConfigMount
|
||||||
@@ -79,3 +80,220 @@ async def validate_includes_no_cycle(
|
|||||||
status_code=status.HTTP_400_BAD_REQUEST,
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||||||
detail="adding this include would create a circular reference",
|
detail="adding this include would create a circular reference",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Profile CRUD helpers
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
async def check_duplicate_name(
|
||||||
|
session: AsyncSession,
|
||||||
|
user_id: uuid.UUID,
|
||||||
|
name: str,
|
||||||
|
exclude_id: uuid.UUID | None = None,
|
||||||
|
) -> None:
|
||||||
|
"""Raise 409 if a profile with the given name already exists."""
|
||||||
|
query = select(ConfigProfile).where(
|
||||||
|
ConfigProfile.user_id == user_id,
|
||||||
|
ConfigProfile.name == name,
|
||||||
|
)
|
||||||
|
if exclude_id:
|
||||||
|
query = query.where(ConfigProfile.id != exclude_id)
|
||||||
|
existing = await session.scalar(query)
|
||||||
|
if existing:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_409_CONFLICT,
|
||||||
|
detail=f"config profile with name '{name}' already exists",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def profile_to_dict(profile: ConfigProfile) -> dict:
|
||||||
|
"""Serialize a ConfigProfile to a dict."""
|
||||||
|
return {
|
||||||
|
"id": str(profile.id),
|
||||||
|
"user_id": str(profile.user_id),
|
||||||
|
"name": profile.name,
|
||||||
|
"description": profile.description,
|
||||||
|
"created_at": profile.created_at.isoformat() if profile.created_at else None,
|
||||||
|
"updated_at": profile.updated_at.isoformat() if profile.updated_at else None,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Include helpers
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
async def check_duplicate_include(
|
||||||
|
session: AsyncSession,
|
||||||
|
profile_id: uuid.UUID,
|
||||||
|
included_profile_id: uuid.UUID,
|
||||||
|
) -> None:
|
||||||
|
"""Raise 409 if the include already exists."""
|
||||||
|
existing = await session.scalar(
|
||||||
|
select(ConfigInclude).where(
|
||||||
|
ConfigInclude.profile_id == profile_id,
|
||||||
|
ConfigInclude.included_profile_id == included_profile_id,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
if existing:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_409_CONFLICT,
|
||||||
|
detail="this include already exists",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def include_to_dict(inc: ConfigInclude, included_name: str | None) -> dict:
|
||||||
|
"""Serialize a ConfigInclude to a dict."""
|
||||||
|
return {
|
||||||
|
"id": str(inc.id),
|
||||||
|
"profile_id": str(inc.profile_id),
|
||||||
|
"included_profile_id": str(inc.included_profile_id),
|
||||||
|
"included_profile_name": included_name,
|
||||||
|
"order_index": inc.order_index,
|
||||||
|
"created_at": inc.created_at.isoformat() if inc.created_at else None,
|
||||||
|
"updated_at": inc.updated_at.isoformat() if inc.updated_at else None,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Mount helpers
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
async def check_duplicate_mount_path(
|
||||||
|
session: AsyncSession,
|
||||||
|
profile_id: uuid.UUID,
|
||||||
|
target_path: str,
|
||||||
|
exclude_id: uuid.UUID | None = None,
|
||||||
|
) -> None:
|
||||||
|
"""Raise 409 if a mount with the given path already exists."""
|
||||||
|
query = select(ConfigMount).where(
|
||||||
|
ConfigMount.profile_id == profile_id,
|
||||||
|
ConfigMount.target_path == target_path,
|
||||||
|
)
|
||||||
|
if exclude_id:
|
||||||
|
query = query.where(ConfigMount.id != exclude_id)
|
||||||
|
existing = await session.scalar(query)
|
||||||
|
if existing:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_409_CONFLICT,
|
||||||
|
detail=f"mount with path '{target_path}' already exists",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def mount_to_dict(mount: ConfigMount) -> dict:
|
||||||
|
"""Serialize a ConfigMount to a dict."""
|
||||||
|
return {
|
||||||
|
"id": str(mount.id),
|
||||||
|
"profile_id": str(mount.profile_id),
|
||||||
|
"target_path": mount.target_path,
|
||||||
|
"files": mount.files,
|
||||||
|
"mode": mount.mode,
|
||||||
|
"order_index": mount.order_index,
|
||||||
|
"created_at": mount.created_at.isoformat() if mount.created_at else None,
|
||||||
|
"updated_at": mount.updated_at.isoformat() if mount.updated_at else None,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Default profile helpers
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
async def get_or_create_user_config(
|
||||||
|
session: AsyncSession,
|
||||||
|
user_id: uuid.UUID,
|
||||||
|
) -> UserConfig:
|
||||||
|
"""Get existing user config or create a new one."""
|
||||||
|
result = await session.execute(select(UserConfig).where(UserConfig.user_id == user_id))
|
||||||
|
user_config = result.scalar_one_or_none()
|
||||||
|
if user_config is None:
|
||||||
|
user_config = UserConfig(user_id=user_id, config={})
|
||||||
|
session.add(user_config)
|
||||||
|
return user_config
|
||||||
|
|
||||||
|
|
||||||
|
async def validate_default_profiles(
|
||||||
|
session: AsyncSession,
|
||||||
|
user_id: uuid.UUID,
|
||||||
|
default_profiles: dict[str, str],
|
||||||
|
) -> None:
|
||||||
|
"""Validate that all profile IDs in default_profiles belong to the user."""
|
||||||
|
for tool_type_id, profile_id_str in default_profiles.items():
|
||||||
|
profile = await session.get(ConfigProfile, uuid.UUID(profile_id_str))
|
||||||
|
if profile is None:
|
||||||
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"profile {profile_id_str} not found")
|
||||||
|
if profile.user_id != user_id:
|
||||||
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail=f"profile {profile_id_str} does not belong to user")
|
||||||
|
|
||||||
|
|
||||||
|
async def get_default_profiles(
|
||||||
|
session: AsyncSession,
|
||||||
|
user_id: uuid.UUID,
|
||||||
|
) -> dict:
|
||||||
|
"""Get default profiles for a user."""
|
||||||
|
result = await session.execute(select(UserConfig).where(UserConfig.user_id == user_id))
|
||||||
|
user_config = result.scalar_one_or_none()
|
||||||
|
return {"default_profiles": user_config.default_profiles if user_config else {}}
|
||||||
|
|
||||||
|
|
||||||
|
async def set_default_profiles(
|
||||||
|
session: AsyncSession,
|
||||||
|
user_id: uuid.UUID,
|
||||||
|
default_profiles: dict[str, str],
|
||||||
|
) -> dict:
|
||||||
|
"""Set default profiles for a user."""
|
||||||
|
user_config = await get_or_create_user_config(session, user_id)
|
||||||
|
await validate_default_profiles(session, user_id, default_profiles)
|
||||||
|
user_config.config = {**user_config.config, "default_profiles": default_profiles}
|
||||||
|
await session.commit()
|
||||||
|
await session.refresh(user_config)
|
||||||
|
return {"default_profiles": user_config.default_profiles}
|
||||||
|
|
||||||
|
|
||||||
|
async def get_default_profile_for_tool_type(
|
||||||
|
session: AsyncSession,
|
||||||
|
user_id: uuid.UUID,
|
||||||
|
tool_type_id: str,
|
||||||
|
) -> dict:
|
||||||
|
"""Get default profile for a specific tool type."""
|
||||||
|
result = await session.execute(select(UserConfig).where(UserConfig.user_id == user_id))
|
||||||
|
user_config = result.scalar_one_or_none()
|
||||||
|
profile_id = user_config.default_profiles.get(tool_type_id) if user_config else None
|
||||||
|
return {"tool_type_id": tool_type_id, "profile_id": profile_id}
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Include list helper
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
async def list_includes_for_profile(
|
||||||
|
session: AsyncSession,
|
||||||
|
profile_id: uuid.UUID,
|
||||||
|
) -> dict:
|
||||||
|
"""List all includes for a profile."""
|
||||||
|
result = await session.execute(
|
||||||
|
select(ConfigInclude)
|
||||||
|
.where(ConfigInclude.profile_id == profile_id)
|
||||||
|
.order_by(ConfigInclude.order_index)
|
||||||
|
)
|
||||||
|
includes_data = []
|
||||||
|
for inc in result.scalars().all():
|
||||||
|
included_profile = await session.get(ConfigProfile, inc.included_profile_id)
|
||||||
|
includes_data.append(include_to_dict(inc, included_profile.name if included_profile else None))
|
||||||
|
return {"includes": includes_data}
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Mount list helper
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
async def list_mounts_for_profile(
|
||||||
|
session: AsyncSession,
|
||||||
|
profile_id: uuid.UUID,
|
||||||
|
) -> dict:
|
||||||
|
"""List all mounts for a profile."""
|
||||||
|
result = await session.execute(
|
||||||
|
select(ConfigMount)
|
||||||
|
.where(ConfigMount.profile_id == profile_id)
|
||||||
|
.order_by(ConfigMount.order_index)
|
||||||
|
)
|
||||||
|
return {"mounts": [mount_to_dict(m) for m in result.scalars().all()]}
|
||||||
|
|||||||
@@ -32,7 +32,9 @@ const SessionItem = ({ session }: { session: Session }) => {
|
|||||||
className={`${styles.navItem} ${styles.sessionItem}`}
|
className={`${styles.navItem} ${styles.sessionItem}`}
|
||||||
title={`${displayName} (${session.status})`}
|
title={`${displayName} (${session.status})`}
|
||||||
>
|
>
|
||||||
<span className={`${styles.sessionStatus} ${isRunning ? styles.running : ""}`} />
|
<span
|
||||||
|
className={`${styles.sessionStatus} ${isRunning ? styles.running : ""}`}
|
||||||
|
/>
|
||||||
<Icon name={session.tool_icon as IconName} size="sm" />
|
<Icon name={session.tool_icon as IconName} size="sm" />
|
||||||
<span className={styles.sessionName}>{displayName}</span>
|
<span className={styles.sessionName}>{displayName}</span>
|
||||||
</a>
|
</a>
|
||||||
@@ -97,7 +99,9 @@ export const AppShell = () => {
|
|||||||
key={item.to}
|
key={item.to}
|
||||||
to={item.to}
|
to={item.to}
|
||||||
className={({ isActive }) =>
|
className={({ isActive }) =>
|
||||||
isActive ? `${styles.navItem} ${styles.navItemActive}` : styles.navItem
|
isActive
|
||||||
|
? `${styles.navItem} ${styles.navItemActive}`
|
||||||
|
: styles.navItem
|
||||||
}
|
}
|
||||||
end={item.to === "/"}
|
end={item.to === "/"}
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -31,10 +31,18 @@
|
|||||||
text-align: center;
|
text-align: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
.fileItem.modified .fileStatus { color: #f59e0b; }
|
.fileItem.modified .fileStatus {
|
||||||
.fileItem.added .fileStatus { color: #10b981; }
|
color: #f59e0b;
|
||||||
.fileItem.deleted .fileStatus { color: #ef4444; }
|
}
|
||||||
.fileItem.untracked .fileStatus { color: #6b7280; }
|
.fileItem.added .fileStatus {
|
||||||
|
color: #10b981;
|
||||||
|
}
|
||||||
|
.fileItem.deleted .fileStatus {
|
||||||
|
color: #ef4444;
|
||||||
|
}
|
||||||
|
.fileItem.untracked .fileStatus {
|
||||||
|
color: #6b7280;
|
||||||
|
}
|
||||||
|
|
||||||
.commitForm {
|
.commitForm {
|
||||||
display: flex;
|
display: flex;
|
||||||
|
|||||||
@@ -223,7 +223,10 @@ export const InstanceList = ({
|
|||||||
/>
|
/>
|
||||||
{instance.status}
|
{instance.status}
|
||||||
{isTunnelUnhealthy(instance) && (
|
{isTunnelUnhealthy(instance) && (
|
||||||
<span className={styles.errorBadge} title="Tunnel unreachable">
|
<span
|
||||||
|
className={styles.errorBadge}
|
||||||
|
title="Tunnel unreachable"
|
||||||
|
>
|
||||||
<Icon name="warning" size="sm" />
|
<Icon name="warning" size="sm" />
|
||||||
tunnel error
|
tunnel error
|
||||||
</span>
|
</span>
|
||||||
|
|||||||
@@ -30,7 +30,9 @@ export const SettingsTabLayout: React.FC<SettingsTabLayoutProps> = ({
|
|||||||
key={tab.id}
|
key={tab.id}
|
||||||
to={`${basePath}/${tab.path}`}
|
to={`${basePath}/${tab.path}`}
|
||||||
className={`${styles.settingsNavLink} ${
|
className={`${styles.settingsNavLink} ${
|
||||||
location.pathname.includes(tab.path) ? styles.settingsNavLinkActive : ""
|
location.pathname.includes(tab.path)
|
||||||
|
? styles.settingsNavLinkActive
|
||||||
|
: ""
|
||||||
}`}
|
}`}
|
||||||
>
|
>
|
||||||
{tab.label}
|
{tab.label}
|
||||||
|
|||||||
Reference in New Issue
Block a user