Skip to content
33 changes: 33 additions & 0 deletions custom_components/hacs/validate/license.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from .base import ActionValidationBase, ValidationException

if TYPE_CHECKING:
from ..repositories.base import HacsRepository


async def async_setup_validator(repository: HacsRepository) -> Validator:
"""Set up this validator."""
return Validator(repository=repository)


class Validator(ActionValidationBase):
"""Validate the repository."""

more_info = "https://hacs.xyz/docs/publish/include#check-repository"
allow_fork = False

async def async_validate(self) -> None:
"""Validate the repository."""
if (license_info := self.repository.repository_object.attributes.get("license")) is None:
raise ValidationException("The repository has no license")
if license_info.get("key") == "other":
raise ValidationException(
"The repository has no recognized license "
f"(license name is '{license_info.get('name', 'unknown')}')"
)
Comment thread
ludeeus marked this conversation as resolved.
Outdated
self.repository.logger.debug(
"The repository has a valid license: %s", license_info.get("name")
)
2 changes: 1 addition & 1 deletion tests/action/test_hacs_action_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ async def test_hacs_action_integration(
await preflight()

assert (
"All (8) checks passed" if test_case["succeed"] else "1/8 checks failed") in caplog.text
"All (9) checks passed" if test_case["succeed"] else "1/9 checks failed") in caplog.text

splitlines = [f"<{line.rsplit(' <')[1]}" for line in caplog.text.split(
"\n") if " <" in line]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
<Validation information> completed
<Validation integration_manifest> failed: invalid url for dictionary value @ data['documentation']. Got None (More info: https://hacs.xyz/docs/publish/include#check-manifest )
<Validation issues> completed
<Validation license> completed
<Validation topics> completed
<Integration hacs-test-org/integration-basic> 1/8 checks failed
<Integration hacs-test-org/integration-basic> 1/9 checks failed
<Integration hacs-test-org/integration-basic> Validation completed
::group::data
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
<Validation information> completed
<Validation integration_manifest> failed: invalid url for dictionary value @ data['issue_tracker']. Got None (More info: https://hacs.xyz/docs/publish/include#check-manifest )
<Validation issues> completed
<Validation license> completed
<Validation topics> completed
<Integration hacs-test-org/integration-basic> 1/8 checks failed
<Integration hacs-test-org/integration-basic> 1/9 checks failed
<Integration hacs-test-org/integration-basic> Validation completed
::group::data
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
<Validation information> completed
<Validation integration_manifest> completed
<Validation issues> completed
<Validation license> completed
<Validation topics> completed
<Integration hacs-test-org/integration-basic> All (8) checks passed
<Integration hacs-test-org/integration-basic> All (9) checks passed
<Integration hacs-test-org/integration-basic> Validation completed
::group::data
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
<Validation information> completed
<Validation integration_manifest> completed
<Validation issues> completed
<Validation license> completed
<Validation topics> completed
<Integration hacs-test-org/integration-basic> All (8) checks passed
<Integration hacs-test-org/integration-basic> All (9) checks passed
<Integration hacs-test-org/integration-basic> Validation completed
::group::data
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
<Validation information> completed
<Validation integration_manifest> completed
<Validation issues> completed
<Validation license> completed
<Validation topics> completed
<Integration hacs-test-org/integration-basic> All (8) checks passed
<Integration hacs-test-org/integration-basic> All (9) checks passed
<Integration hacs-test-org/integration-basic> Validation completed
::group::data
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"tests/validate/test_repository_license_check.py::test_repository_no_license": {
"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/validate/test_repository_license_check.py::test_repository_unrecognized_license": {
"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/validate/test_repository_license_check.py::test_repository_valid_license": {
"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
}
}
31 changes: 31 additions & 0 deletions tests/validate/test_repository_license_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from unittest.mock import MagicMock

from custom_components.hacs.validate.license import Validator


async def test_repository_no_license(repository):
repository.repository_object = MagicMock()
repository.repository_object.attributes = {"license": None}
check = Validator(repository)
await check.execute_validation()
assert check.failed


async def test_repository_unrecognized_license(repository):
repository.repository_object = MagicMock()
repository.repository_object.attributes = {
"license": {"key": "other", "name": "Other", "spdx_id": "NOASSERTION"},
}
check = Validator(repository)
await check.execute_validation()
assert check.failed


async def test_repository_valid_license(repository):
repository.repository_object = MagicMock()
repository.repository_object.attributes = {
"license": {"key": "mit", "name": "MIT License", "spdx_id": "MIT"},
}
check = Validator(repository)
await check.execute_validation()
assert not check.failed
Loading