Skip to content
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
66 changes: 66 additions & 0 deletions custom_components/hacs/validate/license.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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")

spdx_id = license_info.get("spdx_id")
if not spdx_id:
raise ValidationException("The repository license is missing an SPDX ID")
if spdx_id == "NOASSERTION":
raise ValidationException(
"The repository license could not be identified (SPDX: NOASSERTION)"
)

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.

try:
licenses = json_loads(result).get("licenses", [])

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.

Same thing. Is this running for all repos or just one? Should we cache the json loading

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.

This validator is for the HACS Github action.
There would be little benefit caching this between runs.
However if the fetch is cached this load will not be here.

except Exception as err:
raise ValidationException("Could not parse the SPDX license list") from err

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

if spdx_id not in osi_approved:
raise ValidationException(
f"The repository does not have an OSI-approved license (detected: '{spdx_id}')"
)

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,9 @@
{
"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
}
}
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,9 @@
{
"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
}
}
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