Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Handle reserved keywords for workspaces #672

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/codegate/api/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ async def create_workspace(request: v1_models.CreateWorkspaceRequest) -> v1_mode
"Invalid workspace name. " "Please use only alphanumeric characters and dashes"
),
)
except crud.WorkspaceCrudError as e:
raise HTTPException(status_code=400, detail=str(e))
except Exception:
raise HTTPException(status_code=500, detail="Internal server error")

Expand Down
2 changes: 2 additions & 0 deletions src/codegate/pipeline/cli/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ async def _add_workspace(self, flags: Dict[str, str], args: List[str]) -> str:
return "Invalid workspace name: It should be alphanumeric and dashes"
except AlreadyExistsError:
return f"Workspace **{new_workspace_name}** already exists"
except crud.WorkspaceCrudError:
return "An error occurred while adding the workspace"
except Exception:
return "An error occurred while adding the workspace"

Expand Down
12 changes: 11 additions & 1 deletion src/codegate/workspaces/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ class WorkspaceAlreadyActiveError(WorkspaceCrudError):
pass


DEFAULT_WORKSPACE_NAME = "default"

# These are reserved keywords that cannot be used for workspaces
RESERVED_WORKSPACE_KEYWORDS = [DEFAULT_WORKSPACE_NAME, "active"]


class WorkspaceCrud:

def __init__(self):
Expand All @@ -29,6 +35,10 @@ async def add_workspace(self, new_workspace_name: str) -> Workspace:
Args:
name (str): The name of the workspace
"""
if new_workspace_name == "":
raise WorkspaceCrudError("Workspace name cannot be empty.")
if new_workspace_name in RESERVED_WORKSPACE_KEYWORDS:
raise WorkspaceCrudError(f"Workspace name {new_workspace_name} is reserved.")
db_recorder = DbRecorder()
workspace_created = await db_recorder.add_workspace(new_workspace_name)
return workspace_created
Expand Down Expand Up @@ -102,7 +112,7 @@ async def soft_delete_workspace(self, workspace_name: str):
"""
if workspace_name == "":
raise WorkspaceCrudError("Workspace name cannot be empty.")
if workspace_name == "default":
if workspace_name == DEFAULT_WORKSPACE_NAME:
raise WorkspaceCrudError("Cannot delete default workspace.")

selected_workspace = await self._db_reader.get_workspace_by_name(workspace_name)
Expand Down
Loading