Skip to content

[PATCH] Correctly handle URLs for self-managed gitlab #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
Loading