Skip to content

Commit 87549e0

Browse files
[PATCH] Correctly handle URLs for self-managed gitlab (#25)
Fixes #24
1 parent 0948627 commit 87549e0

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

gitlab_submodule/submodule_to_project.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
from typing import List, Optional, Union
2+
13
import logging
4+
import re
25
from posixpath import join, normpath
3-
from typing import Optional
46

57
from gitlab.exceptions import GitlabGetError
68
from gitlab.v4.objects import Project, ProjectManager
@@ -15,7 +17,8 @@
1517
def submodule_to_project(
1618
submodule: Submodule,
1719
project_manager: ProjectManager,
18-
self_managed_gitlab_host: Optional[str] = None) -> Optional[Project]:
20+
self_managed_gitlab_host: Optional[Union[str, List[str]]] = None
21+
) -> Optional[Project]:
1922
submodule_project_path_with_namespace = \
2023
_submodule_url_to_path_with_namespace(submodule.url,
2124
submodule.parent_project,
@@ -37,7 +40,8 @@ def submodule_to_project(
3740
def _submodule_url_to_path_with_namespace(
3841
url: str,
3942
parent_project: Project,
40-
self_managed_gitlab_host: Optional[str] = None) -> Optional[str]:
43+
self_managed_gitlab_host: Optional[Union[str, List[str]]] = None
44+
) -> Optional[str]:
4145
"""Returns a path pointing to a Gitlab project, or None if the submodule
4246
is hosted elsewhere
4347
"""
@@ -59,13 +63,17 @@ def _submodule_url_to_path_with_namespace(
5963
# it can still use submodules hosted on gitlab.com
6064
gitlab_hosts = ['gitlab']
6165
if self_managed_gitlab_host:
62-
gitlab_hosts.append(self_managed_gitlab_host)
66+
if isinstance(self_managed_gitlab_host, str):
67+
gitlab_hosts.append(self_managed_gitlab_host)
68+
else:
69+
gitlab_hosts.extend(self_managed_gitlab_host)
6370

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

tests/test_submodule_to_project.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from unittest import TestCase
2+
from unittest.mock import Mock
3+
4+
from gitlab_submodule.submodule_to_project import \
5+
_submodule_url_to_path_with_namespace
6+
7+
8+
class TestSubmoduleToProject(TestCase):
9+
def test__submodule_url_to_path_with_namespace(self):
10+
# Normal gitlab host
11+
path_with_namespace = _submodule_url_to_path_with_namespace(
12+
'https://gitlab.com/namespace/repo.git',
13+
Mock())
14+
self.assertEqual(path_with_namespace, 'namespace/repo')
15+
16+
# Self-managed gitlab URL without self_managed_gitlab_host
17+
path_with_namespace = _submodule_url_to_path_with_namespace(
18+
'https://custom-gitlab/namespace/repo.git',
19+
Mock())
20+
self.assertEqual(path_with_namespace, None)
21+
22+
# Self-managed gitlab URL with self_managed_gitlab_host
23+
path_with_namespace = _submodule_url_to_path_with_namespace(
24+
'https://custom-gitlab/namespace/repo.git',
25+
Mock(),
26+
self_managed_gitlab_host='custom-gitlab')
27+
self.assertEqual(path_with_namespace, 'namespace/repo')

0 commit comments

Comments
 (0)