fix: address PR review feedback (round 3)
Bugs:
- assign_tag/remove_tag now call _unwrap_ocs to surface OCS-level errors
- trash_page changed to idempotentHint=False (trashing twice errors)
- WebDAV path parts stripped of slashes to prevent double-slash paths
Robustness:
- _unwrap_ocs uses ocs.get("data", {}) instead of ocs["data"]
- Unit test added for missing data key in OCS envelope
Minor:
- MCP error codes use -1 (project convention) instead of HTTP status codes
- update_collective docstring notes that emoji is required
- CollectiveTag.color validated as hex format via field_validator
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
44a27bd9e9
commit
f3caad122d
@@ -40,7 +40,7 @@ class CollectivesClient(BaseNextcloudClient):
|
|||||||
if status_code >= 400:
|
if status_code >= 400:
|
||||||
message = meta.get("message", "OCS error")
|
message = meta.get("message", "OCS error")
|
||||||
raise OCSError(status_code, message)
|
raise OCSError(status_code, message)
|
||||||
return ocs["data"]
|
return ocs.get("data", {})
|
||||||
|
|
||||||
# Collectives
|
# Collectives
|
||||||
|
|
||||||
@@ -215,19 +215,21 @@ class CollectivesClient(BaseNextcloudClient):
|
|||||||
|
|
||||||
async def assign_tag(self, collective_id: int, page_id: int, tag_id: int) -> None:
|
async def assign_tag(self, collective_id: int, page_id: int, tag_id: int) -> None:
|
||||||
"""Assign a tag to a page."""
|
"""Assign a tag to a page."""
|
||||||
await self._make_request(
|
response = await self._make_request(
|
||||||
"PUT",
|
"PUT",
|
||||||
f"{API_BASE}/collectives/{collective_id}/pages/{page_id}/tags/{tag_id}",
|
f"{API_BASE}/collectives/{collective_id}/pages/{page_id}/tags/{tag_id}",
|
||||||
headers=self._get_ocs_headers(),
|
headers=self._get_ocs_headers(),
|
||||||
)
|
)
|
||||||
|
self._unwrap_ocs(response.json())
|
||||||
|
|
||||||
async def remove_tag(self, collective_id: int, page_id: int, tag_id: int) -> None:
|
async def remove_tag(self, collective_id: int, page_id: int, tag_id: int) -> None:
|
||||||
"""Remove a tag from a page."""
|
"""Remove a tag from a page."""
|
||||||
await self._make_request(
|
response = await self._make_request(
|
||||||
"DELETE",
|
"DELETE",
|
||||||
f"{API_BASE}/collectives/{collective_id}/pages/{page_id}/tags/{tag_id}",
|
f"{API_BASE}/collectives/{collective_id}/pages/{page_id}/tags/{tag_id}",
|
||||||
headers=self._get_ocs_headers(),
|
headers=self._get_ocs_headers(),
|
||||||
)
|
)
|
||||||
|
self._unwrap_ocs(response.json())
|
||||||
|
|
||||||
# Trash
|
# Trash
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
"""Pydantic models for Nextcloud Collectives app."""
|
"""Pydantic models for Nextcloud Collectives app."""
|
||||||
|
|
||||||
from pydantic import BaseModel, Field
|
import re
|
||||||
|
|
||||||
|
from pydantic import BaseModel, Field, field_validator
|
||||||
|
|
||||||
from .base import BaseResponse, StatusResponse
|
from .base import BaseResponse, StatusResponse
|
||||||
|
|
||||||
@@ -53,7 +55,14 @@ class CollectiveTag(BaseModel):
|
|||||||
id: int = Field(description="Tag ID")
|
id: int = Field(description="Tag ID")
|
||||||
collectiveId: int = Field(description="Parent collective ID")
|
collectiveId: int = Field(description="Parent collective ID")
|
||||||
name: str = Field(description="Tag name")
|
name: str = Field(description="Tag name")
|
||||||
color: str = Field(description="Hex color code")
|
color: str = Field(description="Hex color code (e.g. 'FF0000')")
|
||||||
|
|
||||||
|
@field_validator("color")
|
||||||
|
@classmethod
|
||||||
|
def validate_hex_color(cls, v: str) -> str:
|
||||||
|
if not re.fullmatch(r"[0-9A-Fa-f]{3,8}", v):
|
||||||
|
raise ValueError(f"Invalid hex color: {v!r}")
|
||||||
|
return v
|
||||||
|
|
||||||
|
|
||||||
# Response Models
|
# Response Models
|
||||||
|
|||||||
@@ -34,8 +34,8 @@ logger = logging.getLogger(__name__)
|
|||||||
def _handle_collectives_error(e: OCSError | HTTPStatusError) -> McpError:
|
def _handle_collectives_error(e: OCSError | HTTPStatusError) -> McpError:
|
||||||
"""Convert OCS or HTTP errors to McpError."""
|
"""Convert OCS or HTTP errors to McpError."""
|
||||||
if isinstance(e, OCSError):
|
if isinstance(e, OCSError):
|
||||||
return McpError(ErrorData(code=e.status_code, message=e.message))
|
return McpError(ErrorData(code=-1, message=e.message))
|
||||||
return McpError(ErrorData(code=e.response.status_code, message=str(e)))
|
return McpError(ErrorData(code=-1, message=str(e)))
|
||||||
|
|
||||||
|
|
||||||
def configure_collectives_tools(mcp: FastMCP):
|
def configure_collectives_tools(mcp: FastMCP):
|
||||||
@@ -120,7 +120,7 @@ def configure_collectives_tools(mcp: FastMCP):
|
|||||||
if page.filePath:
|
if page.filePath:
|
||||||
parts.append(page.filePath)
|
parts.append(page.filePath)
|
||||||
parts.append(page.fileName)
|
parts.append(page.fileName)
|
||||||
webdav_path = "/".join(parts)
|
webdav_path = "/".join(p.strip("/") for p in parts)
|
||||||
try:
|
try:
|
||||||
file_bytes, _ = await client.webdav.read_file(webdav_path)
|
file_bytes, _ = await client.webdav.read_file(webdav_path)
|
||||||
content = file_bytes.decode("utf-8")
|
content = file_bytes.decode("utf-8")
|
||||||
@@ -243,11 +243,13 @@ def configure_collectives_tools(mcp: FastMCP):
|
|||||||
async def collectives_update_collective(
|
async def collectives_update_collective(
|
||||||
ctx: Context, collective_id: int, emoji: str | None = None
|
ctx: Context, collective_id: int, emoji: str | None = None
|
||||||
) -> CollectiveOperationResponse:
|
) -> CollectiveOperationResponse:
|
||||||
"""Update a Nextcloud Collective (emoji)
|
"""Update a Nextcloud Collective (emoji).
|
||||||
|
|
||||||
|
At least one field must be provided.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
collective_id: ID of the collective
|
collective_id: ID of the collective
|
||||||
emoji: New emoji for the collective
|
emoji: New emoji for the collective (required)
|
||||||
"""
|
"""
|
||||||
client = await get_client(ctx)
|
client = await get_client(ctx)
|
||||||
try:
|
try:
|
||||||
@@ -340,7 +342,7 @@ def configure_collectives_tools(mcp: FastMCP):
|
|||||||
@mcp.tool(
|
@mcp.tool(
|
||||||
title="Trash Collective Page",
|
title="Trash Collective Page",
|
||||||
annotations=ToolAnnotations(
|
annotations=ToolAnnotations(
|
||||||
destructiveHint=True, idempotentHint=True, openWorldHint=True
|
destructiveHint=True, idempotentHint=False, openWorldHint=True
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
@require_scopes("collectives:write")
|
@require_scopes("collectives:write")
|
||||||
|
|||||||
@@ -263,7 +263,7 @@ async def test_create_tag(mocker):
|
|||||||
|
|
||||||
async def test_assign_tag(mocker):
|
async def test_assign_tag(mocker):
|
||||||
"""Test assigning a tag to a page."""
|
"""Test assigning a tag to a page."""
|
||||||
mock_response = create_mock_response(status_code=200, json_data={})
|
mock_response = _ocs_response({})
|
||||||
mock_request = mocker.patch.object(
|
mock_request = mocker.patch.object(
|
||||||
CollectivesClient, "_make_request", return_value=mock_response
|
CollectivesClient, "_make_request", return_value=mock_response
|
||||||
)
|
)
|
||||||
@@ -278,7 +278,7 @@ async def test_assign_tag(mocker):
|
|||||||
|
|
||||||
async def test_remove_tag(mocker):
|
async def test_remove_tag(mocker):
|
||||||
"""Test removing a tag from a page."""
|
"""Test removing a tag from a page."""
|
||||||
mock_response = create_mock_response(status_code=200, json_data={})
|
mock_response = _ocs_response({})
|
||||||
mock_request = mocker.patch.object(
|
mock_request = mocker.patch.object(
|
||||||
CollectivesClient, "_make_request", return_value=mock_response
|
CollectivesClient, "_make_request", return_value=mock_response
|
||||||
)
|
)
|
||||||
@@ -327,6 +327,25 @@ async def test_restore_page(mocker):
|
|||||||
# --- Error Handling ---
|
# --- Error Handling ---
|
||||||
|
|
||||||
|
|
||||||
|
async def test_ocs_missing_data_returns_empty(mocker):
|
||||||
|
"""Test that OCS envelope without 'data' key returns empty dict."""
|
||||||
|
mock_response = create_mock_response(
|
||||||
|
status_code=200,
|
||||||
|
json_data={
|
||||||
|
"ocs": {
|
||||||
|
"meta": {"status": "ok", "statuscode": 200},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
mocker.patch.object(CollectivesClient, "_make_request", return_value=mock_response)
|
||||||
|
|
||||||
|
client = CollectivesClient(mocker.AsyncMock(spec=httpx.AsyncClient), "testuser")
|
||||||
|
# get_collectives accesses data["collectives"], which will KeyError on empty dict
|
||||||
|
# This tests that _unwrap_ocs itself doesn't crash — it returns {}
|
||||||
|
with pytest.raises(KeyError):
|
||||||
|
await client.get_collectives()
|
||||||
|
|
||||||
|
|
||||||
async def test_ocs_error_status_raises(mocker):
|
async def test_ocs_error_status_raises(mocker):
|
||||||
"""Test that OCS envelope with error statuscode raises OCSError."""
|
"""Test that OCS envelope with error statuscode raises OCSError."""
|
||||||
mock_response = create_mock_response(
|
mock_response = create_mock_response(
|
||||||
|
|||||||
Reference in New Issue
Block a user