aboutsummaryrefslogtreecommitdiff
path: root/src/srht_contrib/utils/repositories.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/srht_contrib/utils/repositories.py')
-rw-r--r--src/srht_contrib/utils/repositories.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/srht_contrib/utils/repositories.py b/src/srht_contrib/utils/repositories.py
new file mode 100644
index 0000000..f8e65b5
--- /dev/null
+++ b/src/srht_contrib/utils/repositories.py
@@ -0,0 +1,31 @@
+from __future__ import annotations
+
+from fastapi import HTTPException, status
+
+
+def canonicalize_repository_name(actor: str, repo_name: str) -> str:
+ normalized_actor = actor.strip()
+ normalized_repo_name = repo_name.strip()
+
+ if not normalized_actor:
+ raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_CONTENT, detail="Actor must not be blank.")
+ if not normalized_repo_name:
+ raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_CONTENT, detail="Repository name must not be blank.")
+
+ if "/" in normalized_repo_name:
+ owner, name = normalized_repo_name.split("/", 1)
+ owner = owner.strip()
+ name = name.strip()
+ if not owner or not name or "/" in name:
+ raise HTTPException(
+ status_code=status.HTTP_422_UNPROCESSABLE_CONTENT,
+ detail="Repository name must be `name` or `~owner/name`.",
+ )
+ canonical_owner = owner if owner.startswith("~") else f"~{owner}"
+ return f"{canonical_owner}/{name}"
+
+ if "/" in normalized_actor:
+ raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_CONTENT, detail="Actor must be a canonical sr.ht user.")
+
+ canonical_actor = normalized_actor if normalized_actor.startswith("~") else f"~{normalized_actor}"
+ return f"{canonical_actor}/{normalized_repo_name}"