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

Replace version_to_install with utils.version.version_to_download #2358

Merged
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
6 changes: 2 additions & 4 deletions custom_components/hacs/helpers/classes/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@
common_update_data,
common_validate,
)
from custom_components.hacs.helpers.functions.version_to_install import (
version_to_install,
)
from custom_components.hacs.share import get_hacs
from custom_components.hacs.utils.logger import getLogger
from custom_components.hacs.utils.path import is_safe
from custom_components.hacs.utils.queue_manager import QueueManager
from custom_components.hacs.utils.version import version_to_download


class RepositoryVersions:
Expand Down Expand Up @@ -368,7 +366,7 @@ async def get_repository_manifest_content(self):
if self.hacs.system.action:
self.logger.info("%s Found hacs.json", self)

self.ref = version_to_install(self)
self.ref = version_to_download(self)

try:
manifest = await self.repository_object.get_contents("hacs.json", self.ref)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
get_repository,
get_tree,
)
from custom_components.hacs.helpers.functions.version_to_install import (
version_to_install,
)

from custom_components.hacs.share import get_hacs, is_removed
from custom_components.hacs.utils.version import version_to_download

if TYPE_CHECKING:
from custom_components.hacs.helpers.classes.repository import HacsRepository
Expand Down Expand Up @@ -96,7 +95,7 @@ async def common_update_data(repository: HacsRepository, ignore_issues=False, fo
repository.data.releases = False

if not repository.force_branch:
repository.ref = version_to_install(repository)
repository.ref = version_to_download(repository)
if repository.data.releases:
for release in repository.releases.objects or []:
if release.tag_name == repository.ref:
Expand Down
20 changes: 0 additions & 20 deletions custom_components/hacs/helpers/functions/version_to_install.py

This file was deleted.

7 changes: 3 additions & 4 deletions custom_components/hacs/helpers/methods/installation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@

from custom_components.hacs.exceptions import HacsException
from custom_components.hacs.helpers.functions.download import download_content
from custom_components.hacs.helpers.functions.version_to_install import (
version_to_install,
)

from custom_components.hacs.operational.backup import Backup, BackupNetDaemon
from custom_components.hacs.share import get_hacs
from custom_components.hacs.utils.version import version_to_download


class RepositoryMethodPreInstall(ABC):
Expand Down Expand Up @@ -58,7 +57,7 @@ async def async_install_repository(repository):
if not repository.can_install:
raise HacsException("The version of Home Assistant is not compatible with this version")

version = version_to_install(repository)
version = version_to_download(repository)
if version == repository.data.default_branch:
repository.ref = version
else:
Expand Down
26 changes: 24 additions & 2 deletions custom_components/hacs/utils/version.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""Version utils."""


from __future__ import annotations
from functools import lru_cache
from typing import TYPE_CHECKING

from awesomeversion import AwesomeVersion, AwesomeVersionException

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


@lru_cache(maxsize=1024)
def version_left_higher_then_right(left: str, right: str) -> bool:
Expand All @@ -23,3 +26,22 @@ def version_left_higher_or_equal_then_right(left: str, right: str) -> bool:
return version_left_higher_then_right(left, right)
except (AwesomeVersionException, AttributeError):
return False


def version_to_download(repository: HacsRepository) -> str:
"""Determine which version to download."""
if repository.data.last_version is not None:
if repository.data.selected_tag is not None:
if repository.data.selected_tag == repository.data.last_version:
repository.data.selected_tag = None
return repository.data.last_version
return repository.data.selected_tag
return repository.data.last_version

if repository.data.selected_tag is not None:
if repository.data.selected_tag == repository.data.default_branch:
return repository.data.default_branch
if repository.data.selected_tag in repository.data.published_tags:
return repository.data.selected_tag

return repository.data.default_branch or "main"
7 changes: 3 additions & 4 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
from homeassistant.util.unit_system import METRIC_SYSTEM

from custom_components.hacs.helpers.classes.repository import HacsRepository
from custom_components.hacs.helpers.functions.version_to_install import (
version_to_install,
)

from custom_components.hacs.utils.logger import getLogger
from custom_components.hacs.utils.version import version_to_download

from tests.async_mock import AsyncMock, Mock, patch

Expand Down Expand Up @@ -48,7 +47,7 @@ def dummy_repository_base(hacs, repository=None):
repository.data.domain = "test"
repository.data.last_version = "3"
repository.data.selected_tag = "3"
repository.ref = version_to_install(repository)
repository.ref = version_to_download(repository)
repository.integration_manifest = {"config_flow": False, "domain": "test"}
repository.data.published_tags = ["1", "2", "3"]
repository.data.update_data(fixture("repository_data.json"))
Expand Down
7 changes: 3 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
from custom_components.hacs.const import DOMAIN
from custom_components.hacs.hacsbase.hacs import Hacs
from custom_components.hacs.helpers.classes.repository import HacsRepository
from custom_components.hacs.helpers.functions.version_to_install import (
version_to_install,
)

from custom_components.hacs.repositories import (
HacsAppdaemonRepository,
HacsIntegrationRepository,
Expand All @@ -35,6 +33,7 @@
)
from custom_components.hacs.share import SHARE
from custom_components.hacs.tasks.manager import HacsTaskManager
from custom_components.hacs.utils.version import version_to_download

from tests.async_mock import MagicMock
from tests.common import (
Expand Down Expand Up @@ -137,7 +136,7 @@ def repository(hacs):
repository_obj.data.domain = "test"
repository_obj.data.last_version = "3"
repository_obj.data.selected_tag = "3"
repository_obj.ref = version_to_install(repository_obj)
repository_obj.ref = version_to_download(repository_obj)
repository_obj.integration_manifest = {"config_flow": False, "domain": "test"}
repository_obj.data.published_tags = ["1", "2", "3"]
repository_obj.data.update_data(fixture("repository_data.json"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,44 @@
"""Helpers: Install: version_to_install."""
# pylint: disable=missing-docstring
from custom_components.hacs.helpers.functions.version_to_install import (
version_to_install,
)
from custom_components.hacs.utils import version


def test_version_to_install(repository):
def test_version_to_download(repository):
repository.data.selected_tag = "main"
assert version_to_install(repository) == "main"
assert version.version_to_download(repository) == "main"

repository.data.default_branch = None
repository.data.last_version = None
repository.data.selected_tag = None
assert version_to_install(repository) == "main"
assert version.version_to_download(repository) == "main"

repository.data.selected_tag = "2"
assert version_to_install(repository) == "2"
assert version.version_to_download(repository) == "2"

repository.data.selected_tag = None
repository.data.last_version = "3"
assert version_to_install(repository) == "3"
assert version.version_to_download(repository) == "3"

repository.data.selected_tag = None
repository.data.last_version = None
assert version_to_install(repository) == "main"
assert version.version_to_download(repository) == "main"

repository.data.selected_tag = "2"
repository.data.last_version = None
assert version_to_install(repository) == "2"
assert version.version_to_download(repository) == "2"

repository.data.selected_tag = "main"
repository.data.last_version = None
assert version_to_install(repository) == "main"
assert version.version_to_download(repository) == "main"

repository.data.selected_tag = "3"
repository.data.last_version = "3"
version_to_install(repository)
version.version_to_download(repository)
assert repository.data.selected_tag is None

repository.data.default_branch = "dev"
repository.data.last_version = None
repository.data.selected_tag = None
assert version_to_install(repository) == "dev"
assert version.version_to_download(repository) == "dev"

repository.data.default_branch = "main"
repository.data.last_version = "2"
assert version_to_install(repository) == "2"
assert version.version_to_download(repository) == "2"