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 register_repository to HacsBase #2388

Merged
merged 2 commits into from
Dec 26, 2021
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
3 changes: 1 addition & 2 deletions action/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from custom_components.hacs.const import HACS_ACTION_GITHUB_API_HEADERS
from custom_components.hacs.exceptions import HacsException
from custom_components.hacs.utils.logger import getLogger
from custom_components.hacs.utils.register_repository import register_repository

TOKEN = os.getenv("INPUT_GITHUB_TOKEN")
GITHUB_WORKSPACE = os.getenv("GITHUB_WORKSPACE")
Expand Down Expand Up @@ -137,7 +136,7 @@ async def validate_repository(repository, category, ref=None):
headers=HACS_ACTION_GITHUB_API_HEADERS,
)
try:
await register_repository(hacs, repository, category, ref=ref)
await hacs.async_register_repository(repository_full_name=repository, category=category, ref=ref)
except HacsException as exception:
error(exception)

Expand Down
87 changes: 80 additions & 7 deletions custom_components/hacs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from typing import TYPE_CHECKING, Any, Awaitable, Callable

from aiogithubapi import (
AIOGitHubAPIException,
GitHub,
GitHubAPI,
GitHubAuthenticationException,
Expand All @@ -38,13 +39,15 @@
)
from .exceptions import (
HacsException,
HacsExpectedException,
HacsNotModifiedException,
HacsRepositoryArchivedException,
HacsRepositoryExistException,
)
from .repositories import RERPOSITORY_CLASSES
from .utils.decode import decode_content
from .utils.logger import getLogger
from .utils.queue_manager import QueueManager
from .utils.register_repository import register_repository
from .utils.store import async_load_from_store, async_save_to_store

if TYPE_CHECKING:
Expand All @@ -56,6 +59,8 @@

@dataclass
class RemovedRepository:
"""Removed repository."""

repository: str | None = None
reason: str | None = None
link: str | None = None
Expand Down Expand Up @@ -256,7 +261,8 @@ def set_repository_id(self, repository, repo_id):
return
if existing_repo_id != "0":
raise ValueError(
f"The repo id for {repository.data.full_name_lower} is already set to {existing_repo_id}"
f"The repo id for {repository.data.full_name_lower} "
f"is already set to {existing_repo_id}"
)
repository.data.id = repo_id
self.register(repository)
Expand Down Expand Up @@ -479,15 +485,79 @@ async def async_semaphore_wrapper(
pass
except HacsRepositoryArchivedException as exception:
self.log.warning(exception)
except BaseException as exception:
except BaseException as exception: # pylint: disable=broad-except
self.log.error(exception)

# Due to GitHub secondary ratelimits we need to sleep a bit
await asyncio.sleep(5)

async def register_repository(self, full_name, category, check=True):
async def async_register_repository(
self,
repository_full_name: str,
category: HacsCategory,
*,
check: bool = True,
ref: str | None = None,
repository_id: str | None = None,
default: bool = False,
) -> None:
"""Register a repository."""
await register_repository(self, full_name, category, check=check)
if repository_full_name in self.common.skip:
if repository_full_name != HacsGitHubRepo.INTEGRATION:
raise HacsExpectedException(f"Skipping {repository_full_name}")

if category not in RERPOSITORY_CLASSES:
raise HacsException(f"{category} is not a valid repository category.")

if (renamed := self.common.renamed_repositories.get(repository_full_name)) is not None:
repository_full_name = renamed

repository: HacsRepository = RERPOSITORY_CLASSES[category](self, repository_full_name)
if check:
try:
await repository.async_registration(ref)
if self.status.new:
repository.data.new = False
if repository.validate.errors:
self.common.skip.append(repository.data.full_name)
if not self.status.startup:
self.log.error("Validation for %s failed.", repository_full_name)
if self.system.action:
raise HacsException(
f"::error:: Validation for {repository_full_name} failed."
)
return repository.validate.errors
if self.system.action:
repository.logger.info("%s Validation completed", repository)
else:
repository.logger.info("%s Registration completed", repository)
except HacsRepositoryExistException:
return
except AIOGitHubAPIException as exception:
self.common.skip.append(repository.data.full_name)
raise HacsException(
f"Validation for {repository_full_name} failed with {exception}."
) from None

if repository_id is not None:
repository.data.id = repository_id

if str(repository.data.id) != "0" and (
exists := self.repositories.get_by_id(repository.data.id)
):
self.repositories.unregister(exists)

else:
if self.hass is not None and ((check and repository.data.new) or self.status.new):
self.hass.bus.async_fire(
"hacs/repository",
{
"action": "registration",
"repository": repository.data.full_name,
"repository_id": repository.data.id,
},
)
self.repositories.register(repository, default)

async def startup_tasks(self, _event=None):
"""Tasks that are started after startup."""
Expand Down Expand Up @@ -583,7 +653,7 @@ async def handle_critical_repositories(self):
was_installed = True
stored["acknowledged"] = False
# Remove from HACS
critical_queue.add(repository.uninstall())
critical_queue.add(repo.uninstall())
repo.remove()

stored_critical.append(stored)
Expand Down Expand Up @@ -685,6 +755,9 @@ async def async_get_category_repositories(self, category: HacsCategory):
continue
self.queue.add(
self.async_semaphore_wrapper(
register_repository, full_name=repo, category=category, default=True
self.async_register_repository,
repository_full_name=repo,
category=category,
default=True,
)
)
9 changes: 5 additions & 4 deletions custom_components/hacs/tasks/load_hacs_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
from homeassistant.core import HomeAssistant

from ..base import HacsBase
from ..enums import HacsDisabledReason, HacsGitHubRepo, HacsStage
from ..enums import HacsCategory, HacsDisabledReason, HacsGitHubRepo, HacsStage
from ..exceptions import HacsException
from ..utils.register_repository import register_repository
from .base import HacsTask


Expand All @@ -25,8 +24,10 @@ async def async_execute(self) -> None:
try:
repository = self.hacs.repositories.get_by_full_name(HacsGitHubRepo.INTEGRATION)
if repository is None:
await register_repository(
self.hacs, HacsGitHubRepo.INTEGRATION, "integration", default=True
await self.hacs.async_register_repository(
repository_full_name=HacsGitHubRepo.INTEGRATION,
category=HacsCategory.INTEGRATION,
default=True,
)
repository = self.hacs.repositories.get_by_full_name(HacsGitHubRepo.INTEGRATION)
if repository is None:
Expand Down
10 changes: 4 additions & 6 deletions custom_components/hacs/tasks/setup_websocket_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from ..enums import HacsStage
from ..exceptions import HacsException
from ..utils import regex
from ..utils.register_repository import register_repository
from ..utils.store import async_load_from_store, async_save_to_store
from .base import HacsTask

Expand Down Expand Up @@ -223,13 +222,12 @@ async def hacs_repository_data(hass, connection, msg):

if not hacs.repositories.get_by_full_name(repo_id):
try:
registration = await register_repository(hacs, repo_id, data.lower())
registration = await hacs.async_register_repository(
repository_full_name=repo_id, category=data.lower()
)
if registration is not None:
raise HacsException(registration)
except (
Exception,
BaseException,
) as exception: # pylint: disable=broad-except
except BaseException as exception: # pylint: disable=broad-except
hass.bus.async_fire(
"hacs/error",
{
Expand Down
8 changes: 3 additions & 5 deletions custom_components/hacs/utils/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from ..enums import HacsGitHubRepo
from ..repositories.base import HacsManifest
from .logger import getLogger
from .register_repository import register_repository
from .store import (
async_load_from_store,
async_save_to_store,
Expand Down Expand Up @@ -180,12 +179,11 @@ def _load_from_storage():
async def register_unknown_repositories(self, repositories):
"""Registry any unknown repositories."""
register_tasks = [
register_repository(
hacs=self.hacs,
full_name=repo_data["full_name"],
self.hacs.async_register_repository(
repository_full_name=repo_data["full_name"],
category=repo_data["category"],
check=False,
repo_id=entry,
repository_id=entry,
)
for entry, repo_data in repositories.items()
if entry != "0" and not self.hacs.repositories.is_registered(repository_id=entry)
Expand Down
81 changes: 0 additions & 81 deletions custom_components/hacs/utils/register_repository.py

This file was deleted.

6 changes: 2 additions & 4 deletions tests/tasks/test_load_hacs_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ async def test_load_hacs_repository_register_failed(

assert not hacs.system.disabled

with patch(
"custom_components.hacs.tasks.load_hacs_repository.register_repository", AsyncMock()
):
with patch("custom_components.hacs.base.HacsBase.async_register_repository", AsyncMock()):
await task.execute_task()
assert hacs.system.disabled
assert hacs.system.disabled_reason == HacsDisabledReason.LOAD_HACS
Expand All @@ -59,7 +57,7 @@ async def test_load_hacs_repository_register_failed_rate_limit(
assert not hacs.system.disabled

with patch(
"custom_components.hacs.tasks.load_hacs_repository.register_repository",
"custom_components.hacs.base.HacsBase.async_register_repository",
side_effect=HacsException("ratelimit 403"),
):
await task.execute_task()
Expand Down