Skip to content

Commit

Permalink
Add handler to use raw URL for manifest.json in validation (#3903)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus authored Jul 19, 2024
1 parent 1909ebf commit e8d6f2a
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 20 deletions.
33 changes: 30 additions & 3 deletions custom_components/hacs/repositories/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ async def validate_repository(self):
):
raise AddonRepositoryException()
raise HacsException(
f"{self.string} Repository structure for {self.ref.replace('tags/','')} is not compliant"
f"{self.string} Repository structure for {
self.ref.replace('tags/', '')} is not compliant"
)
self.content.path.remote = f"custom_components/{name}"

Expand All @@ -101,7 +102,8 @@ async def validate_repository(self):

except KeyError as exception:
self.validate.errors.append(
f"Missing expected key '{exception}' in { RepositoryFile.MAINIFEST_JSON}"
f"Missing expected key '{exception}' in {
RepositoryFile.MAINIFEST_JSON}"
)
self.hacs.log.error(
"Missing expected key '%s' in '%s'", exception, RepositoryFile.MAINIFEST_JSON
Expand Down Expand Up @@ -141,7 +143,8 @@ async def update_repository(self, ignore_issues=False, force=False):

except KeyError as exception:
self.validate.errors.append(
f"Missing expected key '{exception}' in { RepositoryFile.MAINIFEST_JSON}"
f"Missing expected key '{exception}' in {
RepositoryFile.MAINIFEST_JSON}"
)
self.hacs.log.error(
"Missing expected key '%s' in '%s'", exception, RepositoryFile.MAINIFEST_JSON
Expand Down Expand Up @@ -188,3 +191,27 @@ async def async_get_integration_manifest(self, ref: str = None) -> dict[str, Any
)
if response:
return json_loads(decode_content(response.data.content))

async def get_integration_manifest(self, *, version: str, **kwargs) -> dict[str, Any] | None:
"""Get the content of the manifest.json file."""
manifest_path = (
"manifest.json"
if self.repository_manifest.content_in_root
else f"{self.content.path.remote}/{RepositoryFile.MAINIFEST_JSON}"
)

if manifest_path not in (x.full_path for x in self.tree):
raise HacsException(f"No {RepositoryFile.MAINIFEST_JSON} file found '{manifest_path}'")

self.logger.debug("%s Getting manifest.json for version=%s", self.string, version)
try:
result = await self.hacs.async_download_file(
f"https://raw.githubusercontent.com/{
self.data.full_name}/{version}/{manifest_path}",
nolog=True,
)
if result is None:
return None
return json_loads(result)
except Exception: # pylint: disable=broad-except
return None
2 changes: 1 addition & 1 deletion custom_components/hacs/validate/integration_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async def async_validate(self) -> None:
f"The repository has no '{RepositoryFile.MAINIFEST_JSON}' file"
)

content = await self.repository.async_get_integration_manifest(self.repository.ref)
content = await self.repository.get_integration_manifest(version=self.repository.ref)
try:
INTEGRATION_MANIFEST_JSON_SCHEMA(content)
except Invalid as exception:
Expand Down
8 changes: 2 additions & 6 deletions tests/action/test_hacs_action_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,10 @@ async def test_hacs_action_integration(
response=MockedResponse(status=200, content={"custom": ["example"]}),
)
response_mocker.add(
"https://api.github.com/repos/hacs-test-org/integration-basic/contents/custom_components/example/manifest.json",
"https://raw.githubusercontent.com/hacs-test-org/integration-basic/main/custom_components/example/manifest.json",
response=MockedResponse(
status=200,
content={
"content": base64.b64encode(
json.dumps({**basemanifest, **manifest}).encode("ascii"),
).decode("ascii"),
},
content=json.dumps({**basemanifest, **manifest}),
keep=True,
),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<Integration hacs-test-org/integration-basic> Checking repository.
<Integration hacs-test-org/integration-basic> Running checks against main
<Integration hacs-test-org/integration-basic> Getting manifest.json for version=main
<Validation archived> completed
<Validation brands> completed
<Validation description> completed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<Integration hacs-test-org/integration-basic> Checking repository.
<Integration hacs-test-org/integration-basic> Running checks against main
<Integration hacs-test-org/integration-basic> Getting manifest.json for version=main
<Validation archived> completed
<Validation brands> completed
<Validation description> completed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<Integration hacs-test-org/integration-basic> Checking repository.
<Integration hacs-test-org/integration-basic> Running checks against main
<Integration hacs-test-org/integration-basic> Getting manifest.json for version=main
<Validation archived> completed
<Validation brands> completed
<Validation description> completed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"tests/action/test_hacs_action_integration.py::test_hacs_action_integration[bad_documentation-manifest0-False]": {
"https://api.github.com/repos/hacs-test-org/integration-basic": 2,
"https://api.github.com/repos/hacs-test-org/integration-basic/contents/custom_components/example/manifest.json": 2,
"https://api.github.com/repos/hacs-test-org/integration-basic/contents/custom_components/example/manifest.json": 1,
"https://api.github.com/repos/hacs-test-org/integration-basic/contents/hacs.json": 2,
"https://api.github.com/repos/hacs-test-org/integration-basic/git/trees/main": 1,
"https://api.github.com/repos/hacs-test-org/integration-basic/releases": 1,
"https://brands.home-assistant.io/domains.json": 1
"https://brands.home-assistant.io/domains.json": 1,
"https://raw.githubusercontent.com/hacs-test-org/integration-basic/main/custom_components/example/manifest.json": 1
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"tests/action/test_hacs_action_integration.py::test_hacs_action_integration[bad_issue_tracker-manifest1-False]": {
"https://api.github.com/repos/hacs-test-org/integration-basic": 2,
"https://api.github.com/repos/hacs-test-org/integration-basic/contents/custom_components/example/manifest.json": 2,
"https://api.github.com/repos/hacs-test-org/integration-basic/contents/custom_components/example/manifest.json": 1,
"https://api.github.com/repos/hacs-test-org/integration-basic/contents/hacs.json": 2,
"https://api.github.com/repos/hacs-test-org/integration-basic/git/trees/main": 1,
"https://api.github.com/repos/hacs-test-org/integration-basic/releases": 1,
"https://brands.home-assistant.io/domains.json": 1
"https://brands.home-assistant.io/domains.json": 1,
"https://raw.githubusercontent.com/hacs-test-org/integration-basic/main/custom_components/example/manifest.json": 1
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"tests/action/test_hacs_action_integration.py::test_hacs_action_integration[valid_manifest1-manifest2-True]": {
"https://api.github.com/repos/hacs-test-org/integration-basic": 2,
"https://api.github.com/repos/hacs-test-org/integration-basic/contents/custom_components/example/manifest.json": 2,
"https://api.github.com/repos/hacs-test-org/integration-basic/contents/custom_components/example/manifest.json": 1,
"https://api.github.com/repos/hacs-test-org/integration-basic/contents/hacs.json": 2,
"https://api.github.com/repos/hacs-test-org/integration-basic/git/trees/main": 1,
"https://api.github.com/repos/hacs-test-org/integration-basic/releases": 1,
"https://brands.home-assistant.io/domains.json": 1
"https://brands.home-assistant.io/domains.json": 1,
"https://raw.githubusercontent.com/hacs-test-org/integration-basic/main/custom_components/example/manifest.json": 1
}
}
8 changes: 4 additions & 4 deletions tests/validate/test_integration_manifest_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async def test_integration_manifest_with_valid_manifest(repository_integration):
),
]

async def _async_get_integration_manifest(_):
async def _async_get_integration_manifest(**__):
return {
"domain": "test",
"documentation": "https://hacs.xyz",
Expand All @@ -26,7 +26,7 @@ async def _async_get_integration_manifest(_):
"version": "1.0.0",
}

repository_integration.async_get_integration_manifest = _async_get_integration_manifest
repository_integration.get_integration_manifest = _async_get_integration_manifest

check = Validator(repository_integration)
await check.execute_validation()
Expand All @@ -40,10 +40,10 @@ async def test_hacs_manifest_with_invalid_manifest(repository_integration):
),
]

async def _async_get_integration_manifest(_):
async def _async_get_integration_manifest(**__):
return {"not": "valid"}

repository_integration.async_get_integration_manifest = _async_get_integration_manifest
repository_integration.get_integration_manifest = _async_get_integration_manifest
check = Validator(repository_integration)
await check.execute_validation()
assert check.failed

0 comments on commit e8d6f2a

Please sign in to comment.