Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
54 changes: 54 additions & 0 deletions custom_components/hacs/validate/license.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from ..utils.json import json_loads
from .base import ActionValidationBase, ValidationException

if TYPE_CHECKING:
from ..repositories.base import HacsRepository

# renovate: datasource=github-tags depName=spdx/license-list-data
SPDX_LICENSE_LIST_COMMIT = "c4a7237ec8f4654e867546f9f409749300f1bf4c" # v3.28.0

SPDX_LICENSE_LIST_URL = (
"https://raw.githubusercontent.com/spdx/license-list-data/"
f"{SPDX_LICENSE_LIST_COMMIT}/json/licenses.json"
)


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-license"
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")

result = await self.hacs.async_download_file(SPDX_LICENSE_LIST_URL, handle_rate_limit=True)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this cached?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not currently, but we can store a set of IDs.

if result is None:
raise ValidationException("Could not fetch the SPDX license list")
Comment thread
ludeeus marked this conversation as resolved.

osi_approved = {
entry["licenseId"]
for entry in json_loads(result)["licenses"]
if entry.get("isOsiApproved")
}
Comment thread
ludeeus marked this conversation as resolved.

spdx_id = license_info.get("spdx_id")
if not spdx_id or spdx_id == "NOASSERTION" or spdx_id not in osi_approved:
raise ValidationException(
f"The repository does not have an OSI-approved license (detected: '{spdx_id}')"
)
Comment thread
ludeeus marked this conversation as resolved.
Outdated

self.repository.logger.debug(
"The repository has an OSI-approved 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
@@ -0,0 +1,24 @@
{
"licenseListVersion": "3.28.0",
"licenses": [
{
"licenseId": "CC0-1.0",
"name": "Creative Commons Zero v1.0 Universal",
"isDeprecatedLicenseId": false,
"isOsiApproved": false
},
{
"licenseId": "GPL-3.0",
"name": "GNU General Public License v3.0 only",
"isDeprecatedLicenseId": true,
"isOsiApproved": true
},
{
"licenseId": "MIT",
"name": "MIT License",
"isDeprecatedLicenseId": false,
"isOsiApproved": true
}
],
"releaseDate": "2025-05-30"
}
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
Expand Up @@ -7,6 +7,7 @@
"https://api.github.com/repos/hacs-test-org/integration-basic/releases": 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,
"https://raw.githubusercontent.com/hacs-test-org/integration-basic/main/hacs.json": 1
"https://raw.githubusercontent.com/hacs-test-org/integration-basic/main/hacs.json": 1,
"https://raw.githubusercontent.com/spdx/license-list-data/c4a7237ec8f4654e867546f9f409749300f1bf4c/json/licenses.json": 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"https://api.github.com/repos/hacs-test-org/integration-basic/releases": 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,
"https://raw.githubusercontent.com/hacs-test-org/integration-basic/main/hacs.json": 1
"https://raw.githubusercontent.com/hacs-test-org/integration-basic/main/hacs.json": 1,
"https://raw.githubusercontent.com/spdx/license-list-data/c4a7237ec8f4654e867546f9f409749300f1bf4c/json/licenses.json": 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"https://api.github.com/repos/hacs-test-org/integration-basic/releases": 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,
"https://raw.githubusercontent.com/hacs-test-org/integration-basic/main/hacs.json": 1
"https://raw.githubusercontent.com/hacs-test-org/integration-basic/main/hacs.json": 1,
"https://raw.githubusercontent.com/spdx/license-list-data/c4a7237ec8f4654e867546f9f409749300f1bf4c/json/licenses.json": 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"https://api.github.com/repos/hacs-test-org/integration-basic/releases": 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,
"https://raw.githubusercontent.com/hacs-test-org/integration-basic/main/hacs.json": 1
"https://raw.githubusercontent.com/hacs-test-org/integration-basic/main/hacs.json": 1,
"https://raw.githubusercontent.com/spdx/license-list-data/c4a7237ec8f4654e867546f9f409749300f1bf4c/json/licenses.json": 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"https://api.github.com/repos/hacs-test-org/integration-basic/releases": 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,
"https://raw.githubusercontent.com/hacs-test-org/integration-basic/main/hacs.json": 1
"https://raw.githubusercontent.com/hacs-test-org/integration-basic/main/hacs.json": 1,
"https://raw.githubusercontent.com/spdx/license-list-data/c4a7237ec8f4654e867546f9f409749300f1bf4c/json/licenses.json": 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"tests/validate/test_license.py::test_repository_missing_spdx_id": {
"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://raw.githubusercontent.com/spdx/license-list-data/c4a7237ec8f4654e867546f9f409749300f1bf4c/json/licenses.json": 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"tests/validate/test_license.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,10 @@
{
"tests/validate/test_license.py::test_repository_non_osi_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,
"https://raw.githubusercontent.com/spdx/license-list-data/c4a7237ec8f4654e867546f9f409749300f1bf4c/json/licenses.json": 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"tests/validate/test_license.py::test_repository_osi_approved_license[GPL-3.0]": {
"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://raw.githubusercontent.com/spdx/license-list-data/c4a7237ec8f4654e867546f9f409749300f1bf4c/json/licenses.json": 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"tests/validate/test_license.py::test_repository_osi_approved_license[MIT]": {
"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://raw.githubusercontent.com/spdx/license-list-data/c4a7237ec8f4654e867546f9f409749300f1bf4c/json/licenses.json": 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"tests/validate/test_license.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,
"https://raw.githubusercontent.com/spdx/license-list-data/c4a7237ec8f4654e867546f9f409749300f1bf4c/json/licenses.json": 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"tests/validate/test_license.py::test_spdx_license_list_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://raw.githubusercontent.com/spdx/license-list-data/c4a7237ec8f4654e867546f9f409749300f1bf4c/json/licenses.json": 1
}
}
75 changes: 75 additions & 0 deletions tests/validate/test_license.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from unittest.mock import MagicMock

import pytest

from custom_components.hacs.validate.license import SPDX_LICENSE_LIST_URL, Validator

from tests.common import MockedResponse, ResponseMocker


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_missing_spdx_id(repository):
repository.repository_object = MagicMock()
repository.repository_object.attributes = {
"license": {"key": "other", "name": "Other"},
}
check = Validator(repository)
await check.execute_validation()
assert check.failed


async def test_repository_non_osi_license(repository):
repository.repository_object = MagicMock()
repository.repository_object.attributes = {
"license": {
"key": "cc0-1.0",
"name": "Creative Commons Zero v1.0 Universal",
"spdx_id": "CC0-1.0",
},
}
check = Validator(repository)
await check.execute_validation()
assert check.failed


async def test_spdx_license_list_fetch_failure(repository, response_mocker: ResponseMocker):
response_mocker.add(SPDX_LICENSE_LIST_URL, MockedResponse(status=500))
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 check.failed


@pytest.mark.parametrize("spdx_id", ["MIT", "GPL-3.0"])
async def test_repository_osi_approved_license(repository, spdx_id):
repository.repository_object = MagicMock()
repository.repository_object.attributes = {
"license": {
"key": spdx_id.lower(),
"name": f"License {spdx_id}",
"spdx_id": spdx_id,
},
}
check = Validator(repository)
await check.execute_validation()
assert not check.failed
Loading