Skip to content

Commit

Permalink
Handle repository renames (#2144)
Browse files Browse the repository at this point in the history
* Handle repository renames

* Fix test
  • Loading branch information
ludeeus authored Aug 15, 2021
1 parent 7139b46 commit 9fd98d0
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 2 deletions.
1 change: 1 addition & 0 deletions custom_components/hacs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class HacsCommon:
categories: List = []
default: List = []
installed: List = []
renamed_repositories = {}
skip: List = []


Expand Down
10 changes: 10 additions & 0 deletions custom_components/hacs/hacsbase/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ async def async_write(self):

self.logger.debug("Saving data")

# Renamed repositories
await async_save_to_store(
self.hacs.hass,
"renamed_repositories",
self.hacs.common.renamed_repositories,
)

# Hacs
await async_save_to_store(
self.hacs.hass,
Expand Down Expand Up @@ -120,6 +127,9 @@ async def restore(self):
"""Restore saved data."""
hacs = await async_load_from_store(self.hacs.hass, "hacs")
repositories = await async_load_from_store(self.hacs.hass, "repositories") or {}
self.hacs.common.renamed_repositories = (
await async_load_from_store(self.hacs.hass, "renamed_repositories") or {}
)

if not hacs and not repositories:
# Assume new install
Expand Down
3 changes: 3 additions & 0 deletions custom_components/hacs/hacsbase/hacs.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class HacsCommon:
categories = []
default = []
installed = []
renamed_repositories = {}
skip = []


Expand Down Expand Up @@ -388,6 +389,8 @@ async def async_get_category_repositories(self, category: HacsCategory):
"""Get repositories from category."""
repositories = await async_get_list_from_default(category)
for repo in repositories:
if self.common.renamed_repositories.get(repo):
repo = self.common.renamed_repositories[repo]
if is_removed(repo):
continue
repository = self.get_by_name(repo)
Expand Down
4 changes: 4 additions & 0 deletions custom_components/hacs/helpers/classes/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ class HacsNotModifiedException(HacsException):

class HacsExpectedException(HacsException):
"""For stuff that are expected."""


class HacsRepositoryExistException(HacsException):
"""For repositories that are already exist."""
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
"""Register a repository."""
from __future__ import annotations
from typing import TYPE_CHECKING
from aiogithubapi import AIOGitHubAPIException

from custom_components.hacs.helpers.classes.exceptions import (
HacsException,
HacsExpectedException,
HacsRepositoryExistException,
)
from custom_components.hacs.share import get_hacs

from ...repositories import RERPOSITORY_CLASSES

if TYPE_CHECKING:
from ..classes.repository import HacsRepository

# @concurrent(15, 5)
async def register_repository(full_name, category, check=True, ref=None):
Expand All @@ -22,7 +27,7 @@ async def register_repository(full_name, category, check=True, ref=None):
if category not in RERPOSITORY_CLASSES:
raise HacsException(f"{category} is not a valid repository category.")

repository = RERPOSITORY_CLASSES[category](full_name)
repository: HacsRepository = RERPOSITORY_CLASSES[category](full_name)
if check:
try:
await repository.async_registration(ref)
Expand All @@ -39,6 +44,8 @@ async def register_repository(full_name, category, check=True, ref=None):
repository.logger.info("%s Validation completed", repository)
else:
repository.logger.info("%s Registration completed", repository)
except HacsRepositoryExistException:
return
except AIOGitHubAPIException as exception:
hacs.common.skip.append(repository.data.full_name)
raise HacsException(
Expand Down
20 changes: 19 additions & 1 deletion custom_components/hacs/helpers/functions/validate_repository.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
"""Helper to do common validation for repositories."""
from __future__ import annotations
from typing import TYPE_CHECKING

from aiogithubapi import AIOGitHubAPIException

from custom_components.hacs.helpers.classes.exceptions import (
HacsException,
HacsNotModifiedException,
HacsRepositoryArchivedException,
HacsRepositoryExistException,
)
from custom_components.hacs.helpers.functions.information import (
get_releases,
Expand All @@ -16,6 +20,9 @@
)
from custom_components.hacs.share import get_hacs, is_removed

if TYPE_CHECKING:
from custom_components.hacs.helpers.classes.repository import HacsRepository


async def common_validate(repository, ignore_issues=False):
"""Common validation steps of the repository."""
Expand All @@ -29,7 +36,9 @@ async def common_validate(repository, ignore_issues=False):
await repository.get_repository_manifest_content()


async def common_update_data(repository, ignore_issues=False, force=False):
async def common_update_data(
repository: HacsRepository, ignore_issues=False, force=False
):
"""Common update data."""
hacs = get_hacs()
releases = []
Expand All @@ -43,10 +52,19 @@ async def common_update_data(repository, ignore_issues=False, force=False):
else repository.data.etag_repository,
)
repository.repository_object = repository_object
if repository.data.full_name != repository_object.full_name:
hacs.common.renamed_repositories[
repository.data.full_name
] = repository_object.full_name
if str(repository_object.id) not in hacs.common.default:
hacs.common.default.append(str(repository_object.id))
raise HacsRepositoryExistException
repository.data.update_data(repository_object.attributes)
repository.data.etag_repository = etag
except HacsNotModifiedException:
return
except HacsRepositoryExistException:
raise HacsRepositoryExistException from None
except (AIOGitHubAPIException, HacsException) as exception:
if not hacs.status.startup:
repository.logger.error("%s %s", repository, exception)
Expand Down
2 changes: 2 additions & 0 deletions tests/hacsbase/test_hacsbase_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ async def _mocked_loads(hass, key):
}
elif key == "hacs":
return {"view": "Grid", "compact": False, "onboarding_done": True}
elif key == "renamed_repositories":
return {}
else:
raise ValueError(f"No mock for {key}")

Expand Down

0 comments on commit 9fd98d0

Please sign in to comment.