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

Make Workspace name lower cased #711

Merged
merged 2 commits into from
Jan 22, 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
22 changes: 11 additions & 11 deletions src/codegate/db/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import datetime
import re
from typing import Any, Optional
from typing import Annotated, Any, Optional

from pydantic import BaseModel, field_validator
from pydantic import BaseModel, StringConstraints


class Alert(BaseModel):
Expand Down Expand Up @@ -40,18 +39,19 @@ class Setting(BaseModel):
other_settings: Optional[Any]


WorskpaceNameStr = Annotated[
str,
StringConstraints(
strip_whitespace=True, to_lower=True, pattern=r"^[a-zA-Z0-9_-]+$", strict=True
),
]


class Workspace(BaseModel):
id: str
name: str
name: WorskpaceNameStr
system_prompt: Optional[str]

@field_validator("name", mode="plain")
@classmethod
def name_must_be_alphanumeric(cls, value):
if not re.match(r"^[a-zA-Z0-9_-]+$", value):
raise ValueError("name must be alphanumeric and can only contain _ and -")
return value


class Session(BaseModel):
id: str
Expand Down
4 changes: 2 additions & 2 deletions src/codegate/pipeline/cli/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ async def _add_workspace(self, flags: Dict[str, str], args: List[str]) -> str:
return "Please provide a name. Use `codegate workspace add your_workspace_name`"

try:
_ = await self.workspace_crud.add_workspace(new_workspace_name)
ws = await self.workspace_crud.add_workspace(new_workspace_name)
except ValidationError:
return "Invalid workspace name: It should be alphanumeric and dashes"
except AlreadyExistsError:
Expand All @@ -195,7 +195,7 @@ async def _add_workspace(self, flags: Dict[str, str], args: List[str]) -> str:
except Exception:
return "An error occurred while adding the workspace"

return f"Workspace **{new_workspace_name}** has been added"
return f"Workspace **{ws.name}** has been added"

async def _rename_workspace(self, flags: Dict[str, str], args: List[str]) -> str:
"""
Expand Down
4 changes: 3 additions & 1 deletion tests/pipeline/workspace/test_workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest

from codegate.db.models import Workspace as WorkspaceModel
from codegate.db.models import WorkspaceActive
from codegate.pipeline.cli.commands import Workspace

Expand Down Expand Up @@ -80,7 +81,8 @@ async def test_add_workspaces(args, existing_workspaces, expected_message):
with patch("codegate.workspaces.crud.WorkspaceCrud", autospec=True) as mock_recorder_cls:
mock_recorder = mock_recorder_cls.return_value
workspace_commands.workspace_crud = mock_recorder
mock_recorder.add_workspace = AsyncMock()
created_workspace = WorkspaceModel(id="1", name="myworkspace", system_prompt=None)
mock_recorder.add_workspace = AsyncMock(return_value=created_workspace)

# Call the method
result = await workspace_commands._add_workspace(None, args)
Expand Down
Loading