diff --git a/custom_components/hacs/repositories/base.py b/custom_components/hacs/repositories/base.py index 7805c4bf990..b04fc87bd02 100644 --- a/custom_components/hacs/repositories/base.py +++ b/custom_components/hacs/repositories/base.py @@ -11,8 +11,7 @@ from typing import TYPE_CHECKING, Any, List, Optional import zipfile -from aiogithubapi import AIOGitHubAPIException, AIOGitHubAPINotModifiedException, GitHub -from aiogithubapi.const import ACCEPT_HEADERS +from aiogithubapi import AIOGitHubAPIException, AIOGitHubAPINotModifiedException, GitHubReleaseModel from aiogithubapi.objects.repository import AIOGitHubAPIRepository import attr from homeassistant.helpers.json import JSONEncoder @@ -259,7 +258,7 @@ class RepositoryReleases: last_release_object = None last_release_object_downloads = None published_tags = [] - objects = [] + objects: list[GitHubReleaseModel] = [] releases = False downloads = None @@ -890,16 +889,8 @@ async def async_get_legacy_repository_object( ) -> tuple[AIOGitHubAPIRepository, Any | None]: """Return a repository object.""" try: - github = GitHub( - self.hacs.configuration.token, - self.hacs.session, - headers={ - "User-Agent": f"HACS/{self.hacs.version}", - "Accept": ACCEPT_HEADERS["preview"], - }, - ) - repository = await github.get_repo(self.data.full_name, etag) - return repository, github.client.last_response.etag + repository = await self.hacs.github.get_repo(self.data.full_name, etag) + return repository, self.hacs.github.client.last_response.etag except AIOGitHubAPINotModifiedException as exception: raise HacsNotModifiedException(exception) from exception except (ValueError, AIOGitHubAPIException, Exception) as exception: @@ -918,15 +909,20 @@ async def get_tree(self, ref: str): except (ValueError, AIOGitHubAPIException) as exception: raise HacsException(exception) from exception - async def get_releases(self, prerelease=False, returnlimit=5): + async def get_releases(self, prerelease=False, returnlimit=5) -> list[GitHubReleaseModel]: """Return the repository releases.""" - if self.repository_object is None: - raise HacsException("No repository_object") - try: - releases = await self.repository_object.get_releases(prerelease, returnlimit) - return releases - except (ValueError, AIOGitHubAPIException) as exception: - raise HacsException(exception) from exception + response = await self.hacs.async_github_api_method( + method=self.hacs.githubapi.repos.releases.list, + repository=self.data.full_name, + ) + releases = [] + for release in response.data or []: + if len(releases) == returnlimit: + break + if release.draft or (release.prerelease and not prerelease): + continue + releases.append(release) + return releases async def common_update_data(self, ignore_issues: bool = False, force: bool = False) -> None: """Common update data.""" @@ -974,11 +970,11 @@ async def common_update_data(self, ignore_issues: bool = False, force: bool = Fa ) if releases: self.data.releases = True - self.releases.objects = [x for x in releases if not x.draft] + self.releases.objects = releases self.data.published_tags = [x.tag_name for x in self.releases.objects] self.data.last_version = next(iter(self.data.published_tags)) - except (AIOGitHubAPIException, HacsException): + except HacsException: self.data.releases = False if not self.force_branch: @@ -986,9 +982,8 @@ async def common_update_data(self, ignore_issues: bool = False, force: bool = Fa if self.data.releases: for release in self.releases.objects or []: if release.tag_name == self.ref: - assets = release.assets - if assets: - downloads = next(iter(assets)).attributes.get("download_count") + if assets := release.assets: + downloads = next(iter(assets)).download_count self.data.downloads = downloads self.hacs.log.debug("%s Running checks against %s", self, self.ref.replace("tags/", "")) diff --git a/custom_components/hacs/tasks/setup_websocket_api.py b/custom_components/hacs/tasks/setup_websocket_api.py index 7a1f3e3fb30..c072caa68a0 100644 --- a/custom_components/hacs/tasks/setup_websocket_api.py +++ b/custom_components/hacs/tasks/setup_websocket_api.py @@ -364,9 +364,9 @@ async def hacs_repository(hass, connection, msg): elif action == "release_notes": data = [ { - "name": x.attributes["name"], - "body": x.attributes["body"], - "tag": x.attributes["tag_name"], + "name": x.name, + "body": x.body, + "tag": x.tag_name, } for x in repository.releases.objects ] diff --git a/tests/conftest.py b/tests/conftest.py index e1519e53674..e8222e629d2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,7 +6,8 @@ from pathlib import Path from unittest.mock import AsyncMock -from aiogithubapi import GitHubAPI +from aiogithubapi import GitHubAPI, GitHub +from aiogithubapi.const import ACCEPT_HEADERS from awesomeversion import AwesomeVersion from homeassistant.config_entries import ConfigEntry from homeassistant.const import __version__ as HAVERSION @@ -130,6 +131,16 @@ async def hacs(hass: HomeAssistant): hacs_obj.version = hacs_obj.integration.version hacs_obj.configuration.token = TOKEN + ## Old GitHub client + hacs_obj.github = GitHub( + token=hacs_obj.configuration.token, + session=hacs_obj.session, + headers={ + "User-Agent": "HACS/pytest", + "Accept": ACCEPT_HEADERS["preview"], + }, + ) + ## New GitHub client hacs_obj.githubapi = GitHubAPI( token=hacs_obj.configuration.token,