fix(lint): extract _validate_plugin_source to reduce register_plugin statement count

This commit is contained in:
Ishaan Jaffer 2026-04-07 09:41:53 -07:00
parent e3c8867a37
commit c0101b1ab6
No known key found for this signature in database

View file

@ -139,6 +139,56 @@ _VALID_GIT_SUBDIR_PATH_RE = re.compile(
)
def _validate_plugin_source(source: Dict[str, Any]) -> None:
"""Validate plugin source format, raising HTTPException on invalid input."""
source_type = source.get("source")
if source_type == "github":
if "repo" not in source:
raise HTTPException(
status_code=400,
detail={
"error": "GitHub source must include 'repo' field (e.g., 'org/repo')"
},
)
elif source_type == "url":
if "url" not in source:
raise HTTPException(
status_code=400,
detail={
"error": "URL source must include 'url' field (e.g., 'https://github.com/org/repo.git')"
},
)
elif source_type == "git-subdir":
if not source.get("url"):
raise HTTPException(
status_code=400,
detail={
"error": "git-subdir source must include 'url' field (e.g., 'https://github.com/org/repo.git')"
},
)
if not source.get("path"):
raise HTTPException(
status_code=400,
detail={
"error": "git-subdir source must include 'path' field (e.g., 'plugins/plugin-name')"
},
)
if not _VALID_GIT_SUBDIR_PATH_RE.match(source["path"]):
raise HTTPException(
status_code=400,
detail={
"error": "git-subdir 'path' must be a relative path of the form 'segment/segment' (alphanumeric, dots, hyphens, underscores only)"
},
)
else:
raise HTTPException(
status_code=400,
detail={
"error": "source.source must be 'github', 'url', or 'git-subdir'"
},
)
@router.post(
"/claude-code/plugins",
tags=["Claude Code Marketplace"],
@ -195,53 +245,7 @@ async def register_plugin(
# Validate source format
source = request.source
source_type = source.get("source")
if source_type == "github":
if "repo" not in source:
raise HTTPException(
status_code=400,
detail={
"error": "GitHub source must include 'repo' field (e.g., 'org/repo')"
},
)
elif source_type == "url":
if "url" not in source:
raise HTTPException(
status_code=400,
detail={
"error": "URL source must include 'url' field (e.g., 'https://github.com/org/repo.git')"
},
)
elif source_type == "git-subdir":
if not source.get("url"):
raise HTTPException(
status_code=400,
detail={
"error": "git-subdir source must include 'url' field (e.g., 'https://github.com/org/repo.git')"
},
)
if not source.get("path"):
raise HTTPException(
status_code=400,
detail={
"error": "git-subdir source must include 'path' field (e.g., 'plugins/plugin-name')"
},
)
if not _VALID_GIT_SUBDIR_PATH_RE.match(source["path"]):
raise HTTPException(
status_code=400,
detail={
"error": "git-subdir 'path' must be a relative path of the form 'segment/segment' (alphanumeric, dots, hyphens, underscores only)"
},
)
else:
raise HTTPException(
status_code=400,
detail={
"error": "source.source must be 'github', 'url', or 'git-subdir'"
},
)
_validate_plugin_source(source)
# Build manifest for storage
manifest: Dict[str, Any] = {