Skip to content

Commit

Permalink
fix(internal): GitHub parse polynomial regex
Browse files Browse the repository at this point in the history
  • Loading branch information
topher-lo committed Dec 9, 2024
1 parent a9db1ef commit 637717f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
32 changes: 32 additions & 0 deletions tests/unit/test_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,35 @@ async def test_registry_async_function_can_be_called(mock_package):
udf = repo.get("test.async_test_function")
for i in range(10):
assert await udf.fn(num=i) == i


@pytest.mark.parametrize(
"url,expected",
[
("git+ssh://[email protected]/org/repo", ("org", "repo", "main")),
("git+ssh://[email protected]/org/repo.git", ("org", "repo", "main")),
("git+ssh://[email protected]/org/repo@branch", ("org", "repo", "branch")),
("git+ssh://[email protected]/org/repo.git@branch", ("org", "repo", "branch")),
],
)
def test_parse_github_url_valid(url: str, expected: tuple[str, str, str]):
"""Test that valid GitHub URLs are correctly parsed."""
from tracecat.registry.repository import parse_github_url

assert parse_github_url(url) == expected


@pytest.mark.parametrize(
"invalid_url",
[
"https://github.com/org/repo",
"git+ssh://[email protected]/org",
"git+ssh://[email protected]/org/repo@branch/extra",
],
)
def test_parse_github_url_invalid(invalid_url: str):
"""Test that invalid GitHub URLs raise ValueError."""
from tracecat.registry.repository import parse_github_url

with pytest.raises(ValueError):
parse_github_url(invalid_url)
4 changes: 2 additions & 2 deletions tracecat/registry/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,8 +646,8 @@ def parse_github_url(url: str) -> tuple[str, str, str]:
Raises:
ValueError: If the URL is not a valid GitHub repository URL.
"""
# Define regex patterns
ssh_pattern = r"^git\+ssh:\/\/git@github\.com\/(?P<org>[^\/]+)\/(?P<repo>[^\/]+?)(\.git)?(@(?P<branch>[^\/]+))?$"
# Define regex patterns with atomic groups and no nested quantifiers
ssh_pattern = r"^git\+ssh://git@github\.com/(?P<org>[^/]+)/(?P<repo>[^/@]+?)(?:\.git)?(?:@(?P<branch>[^/]+))?$"

# Try matching SSH pattern
if ssh_match := re.match(ssh_pattern, url):
Expand Down

0 comments on commit 637717f

Please sign in to comment.