custom_components/bahmcloud_store/core.py aktualisiert
This commit is contained in:
@@ -15,7 +15,7 @@ from homeassistant.util import yaml as ha_yaml
|
|||||||
from .storage import BCSStorage, CustomRepo
|
from .storage import BCSStorage, CustomRepo
|
||||||
from .views import StaticAssetsView, BCSApiView, BCSReadmeView
|
from .views import StaticAssetsView, BCSApiView, BCSReadmeView
|
||||||
from .custom_repo_view import BCSCustomRepoView
|
from .custom_repo_view import BCSCustomRepoView
|
||||||
from .providers import fetch_repo_info, detect_provider, RepoInfo
|
from .providers import fetch_repo_info, detect_provider, RepoInfo, fetch_readme_markdown
|
||||||
from .metadata import fetch_repo_metadata, RepoMetadata
|
from .metadata import fetch_repo_metadata, RepoMetadata
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@@ -269,93 +269,13 @@ class BCSCore:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
async def fetch_readme_markdown(self, repo_id: str) -> str | None:
|
async def fetch_readme_markdown(self, repo_id: str) -> str | None:
|
||||||
"""Fetch README markdown from GitHub, Gitea or GitLab.
|
|
||||||
|
|
||||||
Defensive behavior:
|
|
||||||
- tries multiple common filenames
|
|
||||||
- tries multiple branches (default, main, master)
|
|
||||||
- uses public raw endpoints (no tokens required for public repositories)
|
|
||||||
"""
|
|
||||||
repo = self.get_repo(repo_id)
|
repo = self.get_repo(repo_id)
|
||||||
if not repo:
|
if not repo:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
repo_url = (repo.url or "").strip()
|
return await fetch_readme_markdown(
|
||||||
if not repo_url:
|
self.hass,
|
||||||
return None
|
repo.url,
|
||||||
|
provider=repo.provider,
|
||||||
# Branch fallbacks
|
default_branch=repo.default_branch,
|
||||||
branch_candidates: list[str] = []
|
)
|
||||||
if repo.default_branch and str(repo.default_branch).strip():
|
|
||||||
branch_candidates.append(str(repo.default_branch).strip())
|
|
||||||
for b in ("main", "master"):
|
|
||||||
if b not in branch_candidates:
|
|
||||||
branch_candidates.append(b)
|
|
||||||
|
|
||||||
# Filename fallbacks
|
|
||||||
filenames = ["README.md", "readme.md", "README.MD", "README.rst", "README"]
|
|
||||||
|
|
||||||
provider = (repo.provider or "").strip().lower()
|
|
||||||
if not provider:
|
|
||||||
provider = detect_provider(repo_url) or ""
|
|
||||||
|
|
||||||
u = urlparse(repo_url.rstrip("/"))
|
|
||||||
host = (u.netloc or "").lower()
|
|
||||||
|
|
||||||
candidates: list[str] = []
|
|
||||||
|
|
||||||
if self._is_github(repo_url):
|
|
||||||
owner, name = self._split_owner_repo(repo_url)
|
|
||||||
if not owner or not name:
|
|
||||||
return None
|
|
||||||
for branch in branch_candidates:
|
|
||||||
base = f"https://raw.githubusercontent.com/{owner}/{name}/{branch}"
|
|
||||||
candidates.extend([f"{base}/{fn}" for fn in filenames])
|
|
||||||
|
|
||||||
elif provider == "gitlab" or "gitlab" in host:
|
|
||||||
# GitLab can have nested groups: /group/subgroup/repo
|
|
||||||
parts = [p for p in u.path.strip("/").split("/") if p]
|
|
||||||
if len(parts) < 2:
|
|
||||||
return None
|
|
||||||
|
|
||||||
repo_name = parts[-1].strip()
|
|
||||||
if repo_name.endswith(".git"):
|
|
||||||
repo_name = repo_name[:-4]
|
|
||||||
group_path = "/".join(parts[:-1]).strip("/")
|
|
||||||
if not group_path or not repo_name:
|
|
||||||
return None
|
|
||||||
|
|
||||||
root = f"{u.scheme}://{u.netloc}/{group_path}/{repo_name}"
|
|
||||||
for branch in branch_candidates:
|
|
||||||
bases = [
|
|
||||||
f"{root}/-/raw/{branch}",
|
|
||||||
# Some instances may expose /raw/<branch> as well
|
|
||||||
f"{root}/raw/{branch}",
|
|
||||||
]
|
|
||||||
for b in bases:
|
|
||||||
candidates.extend([f"{b}/{fn}" for fn in filenames])
|
|
||||||
|
|
||||||
elif self._is_gitea(repo_url):
|
|
||||||
owner, name = self._split_owner_repo(repo_url)
|
|
||||||
if not owner or not name:
|
|
||||||
return None
|
|
||||||
|
|
||||||
root = f"{u.scheme}://{u.netloc}/{owner}/{name}"
|
|
||||||
|
|
||||||
for branch in branch_candidates:
|
|
||||||
bases = [
|
|
||||||
f"{root}/raw/branch/{branch}",
|
|
||||||
f"{root}/raw/{branch}",
|
|
||||||
]
|
|
||||||
for b in bases:
|
|
||||||
candidates.extend([f"{b}/{fn}" for fn in filenames])
|
|
||||||
|
|
||||||
else:
|
|
||||||
return None
|
|
||||||
|
|
||||||
for url in candidates:
|
|
||||||
txt = await self._fetch_text(url)
|
|
||||||
if txt and txt.strip():
|
|
||||||
return txt
|
|
||||||
|
|
||||||
return None
|
|
||||||
|
|||||||
Reference in New Issue
Block a user