48 lines
1.5 KiB
Python
48 lines
1.5 KiB
Python
import re
|
|
from typing import Optional, Tuple
|
|
import pytest
|
|
|
|
|
|
def parse_repo_url(url: str) -> Tuple[Optional[str], Optional[str]]:
|
|
"""Parses a Git repository URL and returns platform and repo id."""
|
|
github_match = re.match(
|
|
r"^(?:https?://)?(?:www\.)?github\.com/([\w.-]+/[\w.-]+)(?:\.git)?/?$",
|
|
url,
|
|
)
|
|
if github_match:
|
|
return "github", github_match.group(1)
|
|
|
|
gitlab_match = re.match(
|
|
r"^(?:https?://)?(?:www\.)?gitlab\.com/([\w.-]+(?:/[\w.-]+)+)(?:\.git)?/?$",
|
|
url,
|
|
)
|
|
if gitlab_match:
|
|
return "gitlab", gitlab_match.group(1)
|
|
return None, None
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"url,expected",
|
|
[
|
|
(
|
|
"https://github.com/Slipstreamm/discordbot",
|
|
("github", "Slipstreamm/discordbot"),
|
|
),
|
|
(
|
|
"http://github.com/Slipstreamm/discordbot",
|
|
("github", "Slipstreamm/discordbot"),
|
|
),
|
|
("github.com/Slipstreamm/discordbot", ("github", "Slipstreamm/discordbot")),
|
|
("www.github.com/Slipstreamm/discordbot", ("github", "Slipstreamm/discordbot")),
|
|
("https://github.com/Slipstreamm/git", ("github", "Slipstreamm/git")),
|
|
("https://gitlab.com/group/project", ("gitlab", "group/project")),
|
|
(
|
|
"https://gitlab.com/group/subgroup/project",
|
|
("gitlab", "group/subgroup/project"),
|
|
),
|
|
("invalid-url", (None, None)),
|
|
],
|
|
)
|
|
def test_parse_repo_url(url: str, expected: Tuple[Optional[str], Optional[str]]):
|
|
assert parse_repo_url(url) == expected
|