Skip to content

Commit 04ac548

Browse files
author
Alan Christie
committed
feat: Add support to check validity of collection and job names
1 parent 948f764 commit 04ac548

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

decoder/decoder.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import enum
99
import os
1010
import re
11-
from typing import Any, Dict, List, Optional, Pattern, Set, Tuple
11+
from typing import Any, Dict, List, Optional, Set, Tuple
1212

1313
import jsonschema
1414
import yaml
@@ -46,7 +46,11 @@
4646
REPO_TYPE_GITLAB: str = "gitlab"
4747
_REPO_TYPES: List[str] = [REPO_TYPE_GITHUB, REPO_TYPE_GITLAB]
4848

49-
_GITHUB_REF_RE: Pattern[str] = re.compile(r"/([^/]+)/data-manager/")
49+
_GITHUB_REF_RE: re.Pattern = re.compile(r"/([^/]+)/data-manager/")
50+
51+
# Patterns for Collection and Job names
52+
_COLLECTION_NAME_RE: re.Pattern = re.compile(r"([a-z]{1}[a-z0-9-]{0,79})")
53+
_JOB_NAME_RE: re.Pattern = re.compile(r"([a-z]{1}[a-z0-9-]{0,79})")
5054

5155
_JOB_KEY_DELIMITER: str = "|"
5256

@@ -57,6 +61,16 @@ class TextEncoding(enum.Enum):
5761
JINJA2_3_0 = 1 # Encoding that complies with Jinja2 v3.0.x
5862

5963

64+
def is_valid_collection_name(collection: str) -> bool:
65+
"""Returns True if the collection name is valid"""
66+
return _COLLECTION_NAME_RE.fullmatch(collection) is not None
67+
68+
69+
def is_valid_job_name(job: str) -> bool:
70+
"""Returns True if the collection name is valid"""
71+
return _JOB_NAME_RE.fullmatch(job) is not None
72+
73+
6074
def get_job_key(*, collection: str, job: str) -> str:
6175
"""Returns the job Key, a string formed from "<collection>|<job>."""
6276
return f"{collection}{_JOB_KEY_DELIMITER}{job}"

tests/test_decoder.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,43 @@ def test_jinja2_3_0_decode_without_variables():
7676
# Assert
7777
assert success
7878
assert rendered == expected_text
79+
80+
81+
def test_is_valid_collection_name_when_not_valid():
82+
# Arrange
83+
84+
# Act
85+
success = decoder.is_valid_collection_name("blob.yaml")
86+
87+
# Assert
88+
assert not success
89+
90+
91+
def test_is_valid_collection_name_when_valid():
92+
# Arrange
93+
94+
# Act
95+
success = decoder.is_valid_collection_name("collection-1")
96+
97+
# Assert
98+
assert success
99+
100+
101+
def test_is_valid_job_name_when_not_valid():
102+
# Arrange
103+
104+
# Act
105+
success = decoder.is_valid_job_name("blob.yaml")
106+
107+
# Assert
108+
assert not success
109+
110+
111+
def test_is_valid_job_name_when_valid():
112+
# Arrange
113+
114+
# Act
115+
success = decoder.is_valid_job_name("job-1")
116+
117+
# Assert
118+
assert success

0 commit comments

Comments
 (0)