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
12 changes: 9 additions & 3 deletions custom_components/hacs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,7 @@ def register(self, repository: HacsRepository, default: bool = False) -> None:
if registered_repo.data.full_name == repository.data.full_name:
return

self.unregister(registered_repo)

registered_repo.data.full_name = repository.data.full_name
self.rename(registered_repo, repository.data.full_name)
registered_repo.data.new = False
repository = registered_repo

Expand Down Expand Up @@ -269,6 +267,14 @@ def unregister(self, repository: HacsRepository) -> None:
self._repositories_by_id.pop(repo_id, None)
self._repositories_by_full_name.pop(repository.data.full_name_lower, None)

def rename(self, repository: HacsRepository, new_full_name: str) -> None:
"""Rename a repository, keeping the name index in sync."""
self._repositories_by_full_name.pop(repository.data.full_name_lower, None)
repository.data.full_name = new_full_name

if repository in self._repositories:
self._repositories_by_full_name[repository.data.full_name_lower] = repository

def mark_default(self, repository: HacsRepository) -> None:
"""Mark a repository as default."""
repo_id = str(repository.data.id)
Expand Down
1 change: 0 additions & 1 deletion custom_components/hacs/repositories/appdaemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def __init__(self, hacs: HacsBase, full_name: str):
"""Initialize."""
super().__init__(hacs=hacs)
self.data.full_name = full_name
self.data.full_name_lower = full_name.lower()
self.data.category = HacsCategory.APPDAEMON
self.content.path.local = self.localpath
self.content.path.remote = "apps"
Expand Down
13 changes: 12 additions & 1 deletion custom_components/hacs/repositories/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,15 @@ def name(self):
return self.domain
return self.full_name.split("/")[-1]

@property
def full_name_lower(self) -> str:
"""Return the lowercase full name.

Derived from full_name so it can never go stale when
a repository is renamed.
"""
return self.full_name.lower()

def to_json(self):
"""Export to json."""
return attr.asdict(self, filter=lambda attr, value: attr.name != "last_fetched")
Expand Down Expand Up @@ -523,7 +532,9 @@ async def common_update(self, ignore_issues=False, force=False, skip_releases=Fa
skip_releases=skip_releases,
)
except HacsRepositoryExistException:
self.data.full_name = self.hacs.common.renamed_repositories[self.data.full_name]
self.hacs.repositories.rename(
self, self.hacs.common.renamed_repositories[self.data.full_name]
)
await self.common_update_data(ignore_issues=ignore_issues, force=force)

except HacsException:
Expand Down
1 change: 0 additions & 1 deletion custom_components/hacs/repositories/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def __init__(self, hacs: HacsBase, full_name: str):
"""Initialize."""
super().__init__(hacs=hacs)
self.data.full_name = full_name
self.data.full_name_lower = full_name.lower()
self.data.category = HacsCategory.INTEGRATION
self.content.path.remote = "custom_components"
self.content.path.local = self.localpath
Expand Down
1 change: 0 additions & 1 deletion custom_components/hacs/repositories/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def __init__(self, hacs: HacsBase, full_name: str):
"""Initialize."""
super().__init__(hacs=hacs)
self.data.full_name = full_name
self.data.full_name_lower = full_name.lower()
self.data.file_name = None
self.data.category = HacsCategory.PLUGIN
self.content.path.local = self.localpath
Expand Down
1 change: 0 additions & 1 deletion custom_components/hacs/repositories/python_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def __init__(self, hacs: HacsBase, full_name: str):
"""Initialize."""
super().__init__(hacs=hacs)
self.data.full_name = full_name
self.data.full_name_lower = full_name.lower()
self.data.category = HacsCategory.PYTHON_SCRIPT
self.content.path.remote = "python_scripts"
self.content.path.local = self.localpath
Expand Down
1 change: 0 additions & 1 deletion custom_components/hacs/repositories/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def __init__(self, hacs: HacsBase, full_name: str):
"""Initialize."""
super().__init__(hacs=hacs)
self.data.full_name = full_name
self.data.full_name_lower = full_name.lower()
self.data.category = HacsCategory.TEMPLATE
self.content.path.remote = ""
self.content.path.local = self.localpath
Expand Down
1 change: 0 additions & 1 deletion custom_components/hacs/repositories/theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def __init__(self, hacs: HacsBase, full_name: str):
"""Initialize."""
super().__init__(hacs=hacs)
self.data.full_name = full_name
self.data.full_name_lower = full_name.lower()
self.data.category = HacsCategory.THEME
self.content.path.remote = "themes"
self.content.path.local = self.localpath
Expand Down
1 change: 0 additions & 1 deletion tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ def dummy_repository_base(hacs, repository=None):
if repository is None:
repository = HacsRepository(hacs)
repository.data.full_name = "test/test"
repository.data.full_name_lower = "test/test"
repository.hacs = hacs
repository.hacs.hass = hacs.hass
repository.hacs.core.config_path = hacs.hass.config.path()
Expand Down
44 changes: 44 additions & 0 deletions tests/hacsbase/test_hacs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from custom_components.hacs.base import HacsRepositories
from custom_components.hacs.enums import HacsCategory
from custom_components.hacs.repositories.base import HacsRepository


async def test_hacs(hacs, repository, tmpdir):
Expand Down Expand Up @@ -57,3 +58,46 @@ async def test_add_remove_repository(hacs, repository, tmpdir):

# Verify second removal does not raise
hacs.repositories.unregister(repository)


async def test_register_renamed_repository(hacs, repository, tmpdir):
"""Registering a known id under a new name must update the name index."""
hacs.hass.config.config_dir = tmpdir

hacs.repositories = HacsRepositories()

repository.data.id = "1337"
hacs.repositories.register(repository, True)

renamed = HacsRepository(hacs)
renamed.data.id = "1337"
renamed.data.full_name = "test/renamed"

hacs.repositories.register(renamed)

assert repository.data.full_name == "test/renamed"
assert repository.data.full_name_lower == "test/renamed"
assert hacs.repositories.get_by_full_name("test/renamed") is repository
assert hacs.repositories.get_by_full_name("test/test") is None
assert hacs.repositories.get_by_id("1337") is repository

# The repository keeps its default status through the rename
assert hacs.repositories.is_default("1337")


async def test_rename_repository(hacs, repository, tmpdir):
"""Renaming a repository keeps the name index in sync."""
hacs.hass.config.config_dir = tmpdir

hacs.repositories = HacsRepositories()

repository.data.id = "1337"
hacs.repositories.register(repository)

hacs.repositories.rename(repository, "Test/Renamed")

assert repository.data.full_name == "Test/Renamed"
assert repository.data.full_name_lower == "test/renamed"
assert hacs.repositories.get_by_full_name("Test/Renamed") is repository
assert hacs.repositories.get_by_full_name("test/renamed") is repository
assert hacs.repositories.get_by_full_name("test/test") is None
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"tests/hacsbase/test_hacs.py::test_register_renamed_repository": {
"https://api.github.com/repos/hacs/integration": 1,
"https://api.github.com/repos/hacs/integration/contents/custom_components/hacs/manifest.json": 1,
"https://api.github.com/repos/hacs/integration/contents/hacs.json": 1,
"https://api.github.com/repos/hacs/integration/git/trees/main": 1,
"https://api.github.com/repos/hacs/integration/releases": 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"tests/hacsbase/test_hacs.py::test_rename_repository": {
"https://api.github.com/repos/hacs/integration": 1,
"https://api.github.com/repos/hacs/integration/contents/custom_components/hacs/manifest.json": 1,
"https://api.github.com/repos/hacs/integration/contents/hacs.json": 1,
"https://api.github.com/repos/hacs/integration/git/trees/main": 1,
"https://api.github.com/repos/hacs/integration/releases": 1
}
}
Loading