custom_components/bahmcloud_store/providers.py aktualisiert

This commit is contained in:
2026-01-15 08:32:58 +00:00
parent e0cecfcc68
commit ad9c4ea421

View File

@@ -16,6 +16,7 @@ class RepoInfo:
repo_name: str | None = None repo_name: str | None = None
description: str | None = None description: str | None = None
provider: str | None = None provider: str | None = None
default_branch: str | None = None
def _split_owner_repo(repo_url: str) -> tuple[str | None, str | None]: def _split_owner_repo(repo_url: str) -> tuple[str | None, str | None]:
@@ -27,13 +28,11 @@ def _split_owner_repo(repo_url: str) -> tuple[str | None, str | None]:
def detect_provider(repo_url: str) -> str: def detect_provider(repo_url: str) -> str:
"""Best-effort provider detection by hostname."""
host = urlparse(repo_url).netloc.lower() host = urlparse(repo_url).netloc.lower()
if "github.com" in host: if "github.com" in host:
return "github" return "github"
if "gitlab.com" in host: if "gitlab.com" in host:
return "gitlab" return "gitlab"
# Default for self-hosted setups: assume Gitea when URL looks like owner/repo
owner, repo = _split_owner_repo(repo_url) owner, repo = _split_owner_repo(repo_url)
if owner and repo: if owner and repo:
return "gitea" return "gitea"
@@ -41,14 +40,9 @@ def detect_provider(repo_url: str) -> str:
async def fetch_repo_info(hass: HomeAssistant, repo_url: str) -> RepoInfo: async def fetch_repo_info(hass: HomeAssistant, repo_url: str) -> RepoInfo:
"""
Fetch owner/name/description from a provider API if possible.
This is best-effort and must never break the store if it fails.
"""
provider = detect_provider(repo_url) provider = detect_provider(repo_url)
owner, repo = _split_owner_repo(repo_url) owner, repo = _split_owner_repo(repo_url)
info = RepoInfo(owner=owner, repo_name=repo, description=None, provider=provider, default_branch=None)
info = RepoInfo(owner=owner, repo_name=repo, description=None, provider=provider)
if not owner or not repo: if not owner or not repo:
return info return info
@@ -58,19 +52,14 @@ async def fetch_repo_info(hass: HomeAssistant, repo_url: str) -> RepoInfo:
try: try:
if provider == "github": if provider == "github":
api = f"https://api.github.com/repos/{owner}/{repo}" api = f"https://api.github.com/repos/{owner}/{repo}"
async with session.get( async with session.get(api, timeout=15, headers={"Accept": "application/vnd.github+json"}) as resp:
api,
timeout=15,
headers={"Accept": "application/vnd.github+json"},
) as resp:
if resp.status != 200: if resp.status != 200:
_LOGGER.debug("GitHub repo info failed (%s): %s", resp.status, api) _LOGGER.debug("GitHub repo info failed (%s): %s", resp.status, api)
return info return info
data = await resp.json() data = await resp.json()
info.description = data.get("description") info.description = data.get("description")
info.repo_name = data.get("name") or repo info.repo_name = data.get("name") or repo
info.default_branch = data.get("default_branch") or "main"
# Owner returned by API is canonical
if isinstance(data.get("owner"), dict) and data["owner"].get("login"): if isinstance(data.get("owner"), dict) and data["owner"].get("login"):
info.owner = data["owner"]["login"] info.owner = data["owner"]["login"]
return info return info
@@ -86,12 +75,11 @@ async def fetch_repo_info(hass: HomeAssistant, repo_url: str) -> RepoInfo:
data = await resp.json() data = await resp.json()
info.description = data.get("description") info.description = data.get("description")
info.repo_name = data.get("name") or repo info.repo_name = data.get("name") or repo
info.default_branch = data.get("default_branch") or "main"
if isinstance(data.get("owner"), dict) and data["owner"].get("login"): if isinstance(data.get("owner"), dict) and data["owner"].get("login"):
info.owner = data["owner"]["login"] info.owner = data["owner"]["login"]
return info return info
# GitLab and generic providers will be implemented later
return info return info
except Exception as e: except Exception as e: