Thanks to visit codestin.com
Credit goes to Github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
35 changes: 23 additions & 12 deletions custom_components/hacs/repositories/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
from aiogithubapi import (
AIOGitHubAPIException,
AIOGitHubAPINotModifiedException,
GitHubReleaseModel,
GitHubException,
)
from aiogithubapi.objects.repository import AIOGitHubAPIRepository
import attr
from homeassistant.helpers import device_registry as dr, issue_registry as ir

Expand Down Expand Up @@ -46,9 +45,13 @@
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 aiogithubapi.models.git_tree import GitHubGitTreeEntryModel
from aiogithubapi.models.release import GitHubReleaseModel
from aiogithubapi.objects.repository import AIOGitHubAPIRepository

from ..base import HacsBase


Expand Down Expand Up @@ -1020,14 +1023,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[GitHubGitTreeEntryModel] | 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,
params={"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 +1150,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
37 changes: 37 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,39 @@ 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

# Simple calculated attributes
self.full_path = self.model.path
self.is_directory = self.model.type == "tree"
self.url = self.model.url
self.download_url = (
f"https://raw.githubusercontent.com/{self.repository}/{self.ref}/{self.full_path}"
)

@property
def path(self):
path = ""
if "/" in self.full_path:
path = self.full_path.split(f"/{self.full_path.split('/')[-1]}")[0]
return path

@property
def filename(self):
filename = self.full_path
if "/" in self.full_path:
filename = self.full_path.split("/")[-1]
return filename