-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add license validator for HACS action #5219
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
Copilot
wants to merge
10
commits into
main
Choose a base branch
from
copilot/extend-hacs-action-license-validation
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
10 commits
Select commit
Hold shift + click to select a range
4e0f24e
Add license validator for HACS action
Copilot 69ae9ff
Refactor license validation logic for improved readability
ludeeus 86dc63b
Fix tests and update snapshots
ludeeus 1836d41
Merge branch 'main' into copilot/extend-hacs-action-license-validation
ludeeus 58b315a
Validate license key against recognized open source licenses list
Copilot 3c35390
Add comment to OPEN_SOURCE_LICENSES constant
Copilot 0459bd2
Apply suggestion from @ludeeus
ludeeus e5f8041
Add missing snapshot files for license validator tests
Copilot 9467c8e
Reduce parametrized license test to first and last items only
Copilot e4c3116
fix snapshots
ludeeus 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,50 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| from .base import ActionValidationBase, ValidationException | ||
|
|
||
| if TYPE_CHECKING: | ||
| from ..repositories.base import HacsRepository | ||
|
|
||
| # GitHub license keys for commonly recognised open source licenses. | ||
| OPEN_SOURCE_LICENSES = { | ||
| "agpl-3.0", | ||
| "apache-2.0", | ||
| "bsd-2-clause", | ||
| "bsd-3-clause", | ||
| "bsl-1.0", | ||
| "cc0-1.0", | ||
| "epl-2.0", | ||
| "gpl-2.0", | ||
| "gpl-3.0", | ||
| "lgpl-2.1", | ||
| "mit", | ||
| "mpl-2.0", | ||
| "unlicense", | ||
| } | ||
|
|
||
|
|
||
| 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_key := license_info.get("key", "unknown")) not in OPEN_SOURCE_LICENSES: | ||
| raise ValidationException( | ||
| "The repository has no recognized open source license " | ||
| f"(license key is '{license_key}')" | ||
| ) | ||
| self.repository.logger.debug( | ||
| "The repository has a valid license: %s", license_info.get("name") | ||
| ) |
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
9 changes: 9 additions & 0 deletions
9
...ots/api-usage/tests/validate/test_repository_license_checktest-repository-no-license.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_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 | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
...pi-usage/tests/validate/test_repository_license_checktest-repository-non-oss-license.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_repository_license_check.py::test_repository_non_oss_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 | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
...age/tests/validate/test_repository_license_checktest-repository-unrecognized-license.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_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 | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
...e/tests/validate/test_repository_license_checktest-repository-valid-license-agpl-3-0.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_repository_license_check.py::test_repository_valid_license[agpl-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 | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
.../tests/validate/test_repository_license_checktest-repository-valid-license-unlicense.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_repository_license_check.py::test_repository_valid_license[unlicense]": { | ||
| "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,44 @@ | ||
| from unittest.mock import MagicMock | ||
|
|
||
| import pytest | ||
|
|
||
| from custom_components.hacs.validate.license import OPEN_SOURCE_LICENSES, 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_non_oss_license(repository): | ||
| repository.repository_object = MagicMock() | ||
| repository.repository_object.attributes = { | ||
| "license": {"key": "cc-by-nc-4.0", "name": "Creative Commons Attribution Non Commercial 4.0", "spdx_id": "CC-BY-NC-4.0"}, | ||
| } | ||
| check = Validator(repository) | ||
| await check.execute_validation() | ||
| assert check.failed | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("license_key", [sorted(OPEN_SOURCE_LICENSES)[0], sorted(OPEN_SOURCE_LICENSES)[-1]]) | ||
| async def test_repository_valid_license(repository, license_key): | ||
| repository.repository_object = MagicMock() | ||
| repository.repository_object.attributes = { | ||
| "license": {"key": license_key, "name": f"License {license_key}", "spdx_id": license_key.upper()}, | ||
| } | ||
| 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.