krz/hutch-stats
Server-side utility for calculating contributions for sourcehut users.
clone: git clone https://gitbay.org/krz/hutch-stats.git
main: src/srht_contrib/utils/repositories.py · raw
1from __future__ import annotations
2
3from fastapi import HTTPException, status
4
5
6def canonicalize_repository_name(actor: str, repo_name: str) -> str:
7 normalized_actor = actor.strip()
8 normalized_repo_name = repo_name.strip()
9
10 if not normalized_actor:
11 raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_CONTENT, detail="Actor must not be blank.")
12 if not normalized_repo_name:
13 raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_CONTENT, detail="Repository name must not be blank.")
14
15 if "/" in normalized_repo_name:
16 owner, name = normalized_repo_name.split("/", 1)
17 owner = owner.strip()
18 name = name.strip()
19 if not owner or not name or "/" in name:
20 raise HTTPException(
21 status_code=status.HTTP_422_UNPROCESSABLE_CONTENT,
22 detail="Repository name must be `name` or `~owner/name`.",
23 )
24 canonical_owner = owner if owner.startswith("~") else f"~{owner}"
25 return f"{canonical_owner}/{name}"
26
27 if "/" in normalized_actor:
28 raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_CONTENT, detail="Actor must be a canonical sr.ht user.")
29
30 canonical_actor = normalized_actor if normalized_actor.startswith("~") else f"~{normalized_actor}"
31 return f"{canonical_actor}/{normalized_repo_name}"