Skip to content

Commit

Permalink
[PATCH] Correctly handle URLs for self-managed gitlab (#25)
Browse files Browse the repository at this point in the history
Fixes #24
  • Loading branch information
ValentinFrancois committed Apr 26, 2024
1 parent 0948627 commit 87549e0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
20 changes: 14 additions & 6 deletions gitlab_submodule/submodule_to_project.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import List, Optional, Union

import logging
import re
from posixpath import join, normpath
from typing import Optional

from gitlab.exceptions import GitlabGetError
from gitlab.v4.objects import Project, ProjectManager
Expand All @@ -15,7 +17,8 @@
def submodule_to_project(
submodule: Submodule,
project_manager: ProjectManager,
self_managed_gitlab_host: Optional[str] = None) -> Optional[Project]:
self_managed_gitlab_host: Optional[Union[str, List[str]]] = None
) -> Optional[Project]:
submodule_project_path_with_namespace = \
_submodule_url_to_path_with_namespace(submodule.url,
submodule.parent_project,
Expand All @@ -37,7 +40,8 @@ def submodule_to_project(
def _submodule_url_to_path_with_namespace(
url: str,
parent_project: Project,
self_managed_gitlab_host: Optional[str] = None) -> Optional[str]:
self_managed_gitlab_host: Optional[Union[str, List[str]]] = None
) -> Optional[str]:
"""Returns a path pointing to a Gitlab project, or None if the submodule
is hosted elsewhere
"""
Expand All @@ -59,13 +63,17 @@ def _submodule_url_to_path_with_namespace(
# it can still use submodules hosted on gitlab.com
gitlab_hosts = ['gitlab']
if self_managed_gitlab_host:
gitlab_hosts.append(self_managed_gitlab_host)
if isinstance(self_managed_gitlab_host, str):
gitlab_hosts.append(self_managed_gitlab_host)
else:
gitlab_hosts.extend(self_managed_gitlab_host)

# giturlparse.GitUrlParsed.platform is too permissive and will be set to
# 'gitlab' for some non-gitlab urls, for instance:
# https://opensource.ncsa.illinois.edu/bitbucket/scm/u3d/3dutilities.git
if (parsed.platform != 'gitlab'
or all([host not in parsed.host for host in gitlab_hosts])):
if (parsed.platform not in ('gitlab', 'base')
or not any([re.match(fr'^{host}(\.\w+)?$', parsed.host)
for host in gitlab_hosts])):
logger.warning(f'submodule git url is not hosted on gitlab: {url}')
return None

Expand Down
27 changes: 27 additions & 0 deletions tests/test_submodule_to_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from unittest import TestCase
from unittest.mock import Mock

from gitlab_submodule.submodule_to_project import \
_submodule_url_to_path_with_namespace


class TestSubmoduleToProject(TestCase):
def test__submodule_url_to_path_with_namespace(self):
# Normal gitlab host
path_with_namespace = _submodule_url_to_path_with_namespace(
'https://gitlab.com/namespace/repo.git',
Mock())
self.assertEqual(path_with_namespace, 'namespace/repo')

# Self-managed gitlab URL without self_managed_gitlab_host
path_with_namespace = _submodule_url_to_path_with_namespace(
'https://custom-gitlab/namespace/repo.git',
Mock())
self.assertEqual(path_with_namespace, None)

# Self-managed gitlab URL with self_managed_gitlab_host
path_with_namespace = _submodule_url_to_path_with_namespace(
'https://custom-gitlab/namespace/repo.git',
Mock(),
self_managed_gitlab_host='custom-gitlab')
self.assertEqual(path_with_namespace, 'namespace/repo')

0 comments on commit 87549e0

Please sign in to comment.