Skip to content

Commit

Permalink
feat: add support for nested namespaces in gitlab
Browse files Browse the repository at this point in the history
Signed-off-by: Jennifer Power <[email protected]>
  • Loading branch information
jpower432 committed Aug 12, 2023
1 parent bafffec commit 41dfeea
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
18 changes: 18 additions & 0 deletions tests/trestlebot/test_gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ def test_parse_repository_with_server_url(repo_url: str) -> None:
assert repo_name == "repo"


@pytest.mark.parametrize(
"repo_url",
[
"https://mygitlab.com/group/owner/repo",
"https://mygitlab.com/group/owner/repo.git",
"mygitlab.com/group/owner/repo.git",
],
)
def test_parse_repository_with_group(repo_url: str) -> None:
"""Test an invalid url input"""
gl = GitLab("fake", "https://mygitlab.com")

owner, repo_name = gl.parse_repository(repo_url)

assert owner == "group/owner"
assert repo_name == "repo"


def test_parse_repository_integration(tmp_repo: Tuple[str, Repo]) -> None:
"""Tests integration with git remote get-url"""
repo_path, repo = tmp_repo
Expand Down
5 changes: 3 additions & 2 deletions trestlebot/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self, api_token: str, server_url: str = "https://gitlab.com"):
self._gitlab_client = gitlab.Gitlab(server_url, private_token=api_token)

stripped_url = re.sub(r"^(https?://)?", "", server_url)
self.pattern = r"^(?:https?://)?{0}/([^/]+)/([^/.]+)".format(
self.pattern = r"^(?:https?://)?{0}(/.+)/([^/.]+)(\.git)?$".format(
re.escape(stripped_url)
)

Expand All @@ -46,12 +46,13 @@ def parse_repository(self, repo_url: str) -> Tuple[str, str]:
Returns:
Owner and project name in a tuple, respectively
"""

match = re.match(self.pattern, repo_url)

if not match:
raise GitProviderException(f"{repo_url} is an invalid repo URL")

owner = match.group(1)
owner = match.group(1)[1:] # Removing the leading slash
repo = match.group(2)
return (owner, repo)

Expand Down

0 comments on commit 41dfeea

Please sign in to comment.