Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions custom_components/hacs/repositories/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from aiogithubapi import (
AIOGitHubAPIException,
AIOGitHubAPINotModifiedException,
GitHubException,
GitHubReleaseModel,
)
from aiogithubapi.objects.repository import AIOGitHubAPIRepository
Expand Down Expand Up @@ -46,7 +47,7 @@
version_left_higher_or_equal_then_right,
version_left_higher_then_right,
)
from ..utils.workarounds import DOMAIN_OVERRIDES
from ..utils.workarounds import DOMAIN_OVERRIDES, LegacyTreeFile

if TYPE_CHECKING:
from ..base import HacsBase
Expand Down Expand Up @@ -1020,14 +1021,17 @@ async def async_get_legacy_repository_object(
def update_filenames(self) -> None:
"""Get the filename to target."""

async def get_tree(self, ref: str):
async def get_tree(self, ref: str) -> list[LegacyTreeFile] | None:
"""Return the repository tree."""
if self.repository_object is None:
raise HacsException("No repository_object")
try:
tree = await self.repository_object.get_tree(ref)
return tree
except (ValueError, AIOGitHubAPIException) as exception:
response = await self.hacs.async_github_api_method(
method=self.hacs.githubapi.repos.git.get_tree,
repository=self.data.full_name,
tree_sha=ref,
kwargs={"recursive": True},
)
return response.data.tree
except GitHubException as exception:
raise HacsException(exception) from exception

async def get_releases(self, prerelease=False, returnlimit=5) -> list[GitHubReleaseModel]:
Expand Down Expand Up @@ -1144,13 +1148,18 @@ async def common_update_data(
)

try:
self.tree = await self.get_tree(self.ref)
if not self.tree:
tree = await self.get_tree(self.ref)
if not tree:
raise HacsException("No files in tree")
self.tree = [
LegacyTreeFile(entry, repository=self.data.full_name, ref=self.ref)
for entry in tree
]

self.treefiles = []
for treefile in self.tree:
self.treefiles.append(treefile.full_path)
except (AIOGitHubAPIException, HacsException) as exception:
except HacsException as exception:
if (
not retry
and self.ref is not None
Expand Down
26 changes: 26 additions & 0 deletions custom_components/hacs/utils/workarounds.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Workarounds."""

from aiogithubapi.models.git_tree import GitHubGitTreeEntryModel
from homeassistant.core import HomeAssistant

DOMAIN_OVERRIDES = {
Expand Down Expand Up @@ -35,3 +36,28 @@ async def async_register_static_path(
https://developers.home-assistant.io/blog/2024/06/18/async_register_static_paths/
"""
hass.http.register_static_path(url_path, path, cache_headers)


class LegacyTreeFile:
"""Legacy TreeFile representation

This serves as a compatibility layer for code expecting
the older TreeFile structure.
"""

def __init__(self, model: GitHubGitTreeEntryModel, repository: str, ref: str):
"""Initialize."""
self.model = model
self.repository = repository
self.ref = ref

# Calculated attributes
split_path = self.model.path.rsplit("/", 1)
self.full_path = self.model.path
self.is_directory = self.model.type == "tree"
self.path = split_path[0] if "/" in self.model.path else ""
self.filename = split_path[-1] if "/" in self.model.path else self.model.path
self.url = self.model.url
self.download_url = (
f"https://raw.githubusercontent.com/{self.repository}/{self.ref}/{self.model.path}"
)
Loading