From acbff854f2da96bddcaede1385e7fefeba0fb34b Mon Sep 17 00:00:00 2001 From: Christian Cleberg Date: Thu, 9 Apr 2026 19:45:45 -0500 Subject: initial commit --- src/srht_contrib/utils/repositories.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/srht_contrib/utils/repositories.py (limited to 'src/srht_contrib/utils/repositories.py') 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}" -- cgit v1.2.3