diff --git a/galaxy_importer/constants.py b/galaxy_importer/constants.py index 92d278a4..22bf164c 100644 --- a/galaxy_importer/constants.py +++ b/galaxy_importer/constants.py @@ -59,6 +59,11 @@ # uppercase letters, numbers, underscores, and hyphens with # a length in the inclusive range [1, 55]. LEGACY_ROLE_NAME_REGEXP = re.compile("^[a-zA-Z0-9-_]{1,55}$") +# Matches namespaces: Namespace names should match any valid +# GitHub username. Username may only contain alphanumeric characters +# or single hyphens, and cannot begin or end with a hyphen +# Maximum of 39 char tested in the validator +LEGACY_NAMESPACE_REGEXP = re.compile("^([a-zA-Z0-9]+-?)+[a-zA-Z0-9]$") class ContentCategory(enum.Enum): diff --git a/galaxy_importer/schema.py b/galaxy_importer/schema.py index bd7b4e36..ad1c6f16 100644 --- a/galaxy_importer/schema.py +++ b/galaxy_importer/schema.py @@ -30,6 +30,7 @@ MAX_LENGTH_AUTHOR = 64 MAX_LENGTH_LICENSE = 32 MAX_LENGTH_NAME = 64 +MAX_LENGTH_NAMESPACE = 39 MAX_LENGTH_TAG = 64 MAX_LENGTH_URL = 2000 MAX_LENGTH_VERSION = 128 @@ -388,6 +389,7 @@ class LegacyGalaxyInfo(object): """Represents legacy role metadata galaxy_info field.""" role_name = attr.ib(default=None) + namespace = attr.ib(default=None) author = attr.ib(default=None) description = attr.ib(default=None) company = attr.ib(default=None) @@ -400,6 +402,7 @@ class LegacyGalaxyInfo(object): galaxy_tags = attr.ib(factory=list) @role_name.validator + @namespace.validator @author.validator @description.validator @company.validator @@ -439,6 +442,15 @@ def _validate_role_name(self, attribute, value): if value is not None and not constants.LEGACY_ROLE_NAME_REGEXP.match(value): raise exc.LegacyRoleSchemaError(f"role name {value} is invalid") + @namespace.validator + def _validate_namespace(self, attribute, value): + """Ensure namespace matches the regular expression.""" + + if value is not None and ( + not constants.LEGACY_NAMESPACE_REGEXP.match(value) or len(value) > MAX_LENGTH_NAMESPACE + ): + raise exc.LegacyRoleSchemaError(f"namespace {value} is invalid") + @author.validator def _validate_author(self, attribute, value): """Ensure the author value is not too long.""" diff --git a/tests/unit/test_loader_legacy.py b/tests/unit/test_loader_legacy.py index 2c94b323..578fb6b9 100644 --- a/tests/unit/test_loader_legacy.py +++ b/tests/unit/test_loader_legacy.py @@ -18,6 +18,7 @@ galaxy_info: role_name: my_role + namespace: my_namespace author: John Doe description: Some generic role description platforms: @@ -96,6 +97,7 @@ def test_load_values(populated_role_root): galaxy_info = data.metadata.galaxy_info assert galaxy_info.role_name == "my_role" + assert galaxy_info.namespace == "my_namespace" assert galaxy_info.author == "John Doe" assert galaxy_info.description == "Some generic role description" assert galaxy_info.platforms == [