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

Restore local path #2429

Merged
merged 1 commit into from
Jan 2, 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
29 changes: 11 additions & 18 deletions custom_components/hacs/repositories/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ async def validate_repository(self):
self.data.manifest_name = manifest["name"]
self.data.config_flow = manifest.get("config_flow", False)

# Set local path
self.content.path.local = self.localpath

except KeyError as exception:
self.validate.errors.append(
f"Missing expected key '{exception}' in { RepositoryFile.MAINIFEST_JSON}"
Expand All @@ -90,6 +87,9 @@ async def validate_repository(self):
"Missing expected key '%s' in '%s'", exception, RepositoryFile.MAINIFEST_JSON
)

# Set local path
self.content.path.local = self.localpath

# Handle potential errors
if self.validate.errors:
for error in self.validate.errors:
Expand Down Expand Up @@ -118,9 +118,6 @@ async def update_repository(self, ignore_issues=False, force=False):
self.data.manifest_name = manifest["name"]
self.data.config_flow = manifest.get("config_flow", False)

# Set local path
self.content.path.local = self.localpath

except KeyError as exception:
self.validate.errors.append(
f"Missing expected key '{exception}' in { RepositoryFile.MAINIFEST_JSON}"
Expand Down Expand Up @@ -150,15 +147,11 @@ async def async_get_integration_manifest(self, ref: str = None) -> dict[str, Any
if not manifest_path in (x.full_path for x in self.tree):
raise HacsException(f"No {RepositoryFile.MAINIFEST_JSON} file found '{manifest_path}'")

try:
response = await self.hacs.async_github_api_method(
method=self.hacs.githubapi.repos.contents.get,
raise_exception=False,
repository=self.data.full_name,
path=manifest_path,
**{"params": {"ref": ref or version_to_download(self)}},
)
if response:
return json.loads(decode_content(response.data.content))
except BaseException: # lgtm [py/catch-base-exception] pylint: disable=broad-except
pass
response = await self.hacs.async_github_api_method(
method=self.hacs.githubapi.repos.contents.get,
repository=self.data.full_name,
path=manifest_path,
**{"params": {"ref": ref or version_to_download(self)}},
)
if response:
return json.loads(decode_content(response.data.content))
5 changes: 5 additions & 0 deletions custom_components/hacs/utils/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from ..enums import HacsGitHubRepo
from ..repositories.base import HacsManifest, HacsRepository
from .logger import get_hacs_logger
from .path import is_safe
from .store import (
async_load_from_store,
async_save_to_store,
Expand Down Expand Up @@ -229,6 +230,10 @@ def async_restore_repository(self, entry, repository_data):
repository_data.get("repository_manifest", {})
)

if repository.localpath is not None and is_safe(self.hacs, repository.localpath):
# Set local path
repository.content.path.local = repository.localpath

if repository.data.installed:
repository.status.first_install = False

Expand Down
12 changes: 12 additions & 0 deletions tests/stories/test_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from tests.sample_data import (
category_test_treefiles,
integration_manifest,
release_data,
repository_data,
response_rate_limit_header,
Expand Down Expand Up @@ -58,6 +59,17 @@ async def test_registration(
),
)

content = base64.b64encode(json.dumps(integration_manifest).encode("utf-8"))
aresponses.add(
"api.github.com",
"/repos/test/test/contents/custom_components/test/manifest.json",
"get",
aresponses.Response(
body=json.dumps({"content": content.decode("utf-8")}),
headers=response_rate_limit_header,
),
)

assert hacs.repositories.get_by_full_name("test/test") is None

await hacs.async_register_repository("test/test", category, check=True)
Expand Down