Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move get_releases to new API #2508

Merged
merged 2 commits into from
Feb 13, 2022
Merged
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
47 changes: 21 additions & 26 deletions custom_components/hacs/repositories/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -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."""
Expand Down Expand Up @@ -974,21 +970,20 @@ 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:
self.ref = version_to_download(self)
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/", ""))
Expand Down
6 changes: 3 additions & 3 deletions custom_components/hacs/tasks/setup_websocket_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
Expand Down
13 changes: 12 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down