-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add validator to prevent integration domain overrides of core #5442
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
Open
ludeeus
wants to merge
2
commits into
main
Choose a base branch
from
claude/core-domain-override-validator-hs911z
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| from ..enums import HacsCategory | ||
| from ..utils.json import json_loads | ||
| from .base import ActionValidationBase, ValidationException | ||
|
|
||
| if TYPE_CHECKING: | ||
| from ..repositories.base import HacsRepository | ||
| from ..repositories.integration import HacsIntegrationRepository | ||
|
|
||
| # The `next` site is generated from the upcoming Home Assistant release, so domains | ||
| # landing in the next release are caught before they ship, unlike `www`. | ||
| CORE_INTEGRATIONS_URL = "https://next.home-assistant.io/integrations.json" | ||
|
|
||
|
|
||
| async def async_setup_validator(repository: HacsRepository) -> Validator: | ||
| """Set up this validator.""" | ||
| return Validator(repository=repository) | ||
|
|
||
|
|
||
| class Validator(ActionValidationBase): | ||
| """Validate the repository.""" | ||
|
|
||
| repository: HacsIntegrationRepository | ||
| more_info = "https://hacs.xyz/docs/publish/include#check-core-domain-override" | ||
| categories = (HacsCategory.INTEGRATION,) | ||
|
|
||
| async def async_validate(self) -> None: | ||
| """Validate the repository.""" | ||
| if not (domain := self.repository.data.domain): | ||
| # A missing or invalid manifest is reported by the integration_manifest check. | ||
| return | ||
|
|
||
| result = await self.hacs.async_download_file(CORE_INTEGRATIONS_URL, handle_rate_limit=True) | ||
| if result is None: | ||
| raise ValidationException("Could not fetch the core integrations list") | ||
|
|
||
| try: | ||
| core_domains = json_loads(result) | ||
| except Exception as err: | ||
| raise ValidationException("Could not parse the core integrations list") from err | ||
|
|
||
| if not isinstance(core_domains, dict): | ||
| raise ValidationException("Core integrations list has an unexpected format") | ||
|
|
||
| if domain in core_domains: | ||
| raise ValidationException( | ||
| f"The integration overrides the core integration domain '{domain}'" | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...e/tests/validate/test_core_domain_override_checktest-core-integrations-fetch-failure.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "tests/validate/test_core_domain_override_check.py::test_core_integrations_fetch_failure": { | ||
| "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, | ||
| "https://next.home-assistant.io/integrations.json": 1 | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
...ge/tests/validate/test_core_domain_override_checktest-core-integrations-invalid-json.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "tests/validate/test_core_domain_override_check.py::test_core_integrations_invalid_json": { | ||
| "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, | ||
| "https://next.home-assistant.io/integrations.json": 1 | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
...hots/api-usage/tests/validate/test_core_domain_override_checktest-domain-not-in-core.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "tests/validate/test_core_domain_override_check.py::test_domain_not_in_core": { | ||
| "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, | ||
| "https://next.home-assistant.io/integrations.json": 1 | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
...sage/tests/validate/test_core_domain_override_checktest-domain-overrides-core-domain.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "tests/validate/test_core_domain_override_check.py::test_domain_overrides_core_domain": { | ||
| "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, | ||
| "https://next.home-assistant.io/integrations.json": 1 | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
...s/api-usage/tests/validate/test_core_domain_override_checktest-no-domain-skips-fetch.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "tests/validate/test_core_domain_override_check.py::test_no_domain_skips_fetch": { | ||
| "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 | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import json | ||
|
|
||
| from custom_components.hacs.validate.core_domain_override import ( | ||
| CORE_INTEGRATIONS_URL, | ||
| Validator, | ||
| ) | ||
|
|
||
| from tests.common import MockedResponse, ResponseMocker | ||
|
|
||
|
|
||
| async def test_domain_not_in_core(repository, response_mocker: ResponseMocker): | ||
| response_mocker.add( | ||
| CORE_INTEGRATIONS_URL, | ||
| MockedResponse(content=json.dumps({"hue": {}})), | ||
| ) | ||
| repository.data.domain = "test" | ||
| check = Validator(repository) | ||
| await check.execute_validation() | ||
| assert not check.failed | ||
|
|
||
|
|
||
| async def test_domain_overrides_core_domain(repository, response_mocker: ResponseMocker): | ||
| response_mocker.add( | ||
| CORE_INTEGRATIONS_URL, | ||
| MockedResponse(content=json.dumps({"test": {}})), | ||
| ) | ||
| repository.data.domain = "test" | ||
| check = Validator(repository) | ||
| await check.execute_validation() | ||
| assert check.failed | ||
|
|
||
|
|
||
| async def test_core_integrations_fetch_failure(repository, response_mocker: ResponseMocker): | ||
| response_mocker.add(CORE_INTEGRATIONS_URL, MockedResponse(status=500)) | ||
| repository.data.domain = "test" | ||
| check = Validator(repository) | ||
| await check.execute_validation() | ||
| assert check.failed | ||
|
|
||
|
|
||
| async def test_core_integrations_invalid_json(repository, response_mocker: ResponseMocker): | ||
| response_mocker.add(CORE_INTEGRATIONS_URL, MockedResponse(content="not json")) | ||
| repository.data.domain = "test" | ||
| check = Validator(repository) | ||
| await check.execute_validation() | ||
| assert check.failed | ||
|
|
||
|
|
||
| async def test_no_domain_skips_fetch(repository, response_mocker: ResponseMocker): | ||
| # Without a domain there is nothing to compare, the missing manifest is | ||
| # reported by the integration_manifest check instead. | ||
| response_mocker.add(CORE_INTEGRATIONS_URL, MockedResponse(status=500)) | ||
| repository.data.domain = None | ||
| check = Validator(repository) | ||
| await check.execute_validation() | ||
| assert not check.failed |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.