aboutsummaryrefslogtreecommitdiff
path: root/src/srht_contrib/utils/repositories.py
blob: f8e65b538cfcc1e6af902aef16cdcc1b6c90ebd1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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}"