From 0c50ef99654e0dafeed2862561033a812ded02aa Mon Sep 17 00:00:00 2001 From: sama Pyb Date: Tue, 1 Dec 2026 22:48:43 +0800 Subject: [PATCH] sources: match gitlab.com by parsed host instead of URL substring --- src/repo2rlenv/sources.py | 21 +++++++++++++++++---- tests/test_sources.py | 16 ++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/repo2rlenv/sources.py b/src/repo2rlenv/sources.py index ccdfb552..0703a00e 100644 --- a/src/repo2rlenv/sources.py +++ b/src/repo2rlenv/sources.py @@ -20,6 +20,7 @@ from __future__ import annotations from enum import StrEnum +from urllib.parse import urlparse class Capability(StrEnum): @@ -50,17 +51,29 @@ class SourceKind(StrEnum): } +def _remote_host(url: str) -> str: + """Return the lowercased hostname from a scheme or SCP-style git URL.""" + if "://" in url: + return (urlparse(url).hostname or "").lower() + if url.startswith("git@"): + host, sep, _rest = url[len("git@") :].partition(":") + if sep: + return host.lower() + return "" + + def detect_source_kind(url: str) -> SourceKind: """Classify a (normalized) repo URL into a source kind. `RepoSpec.normalize_url` canonicalizes local paths to ``file://`` and - leaves github/gitlab URLs intact, so detection is a simple prefix/host - check. A bare ``owner/name`` has already been expanded to a github URL. + leaves github/gitlab URLs intact, so detection inspects the scheme/host + rather than matching hostnames as arbitrary substrings. A bare + ``owner/name`` has already been expanded to a github URL. """ u = url.strip() - if u.startswith("file://"): + if u.lower().startswith("file://"): return SourceKind.LOCAL - if "gitlab.com" in u or u.startswith("git@gitlab.com"): + if _remote_host(u) == "gitlab.com": return SourceKind.GITLAB return SourceKind.GITHUB diff --git a/tests/test_sources.py b/tests/test_sources.py index c375e41c..ed8871e4 100644 --- a/tests/test_sources.py +++ b/tests/test_sources.py @@ -121,9 +121,25 @@ def test_issues_capability_present_on_remotes_only(): def test_detect_source_kind(): assert detect_source_kind("https://github.com/a/b") == SourceKind.GITHUB assert detect_source_kind("https://gitlab.com/a/b") == SourceKind.GITLAB + assert detect_source_kind("https://GITLAB.COM/a/b") == SourceKind.GITLAB + assert detect_source_kind("git@gitlab.com:a/b") == SourceKind.GITLAB + assert detect_source_kind("ssh://git@gitlab.com/a/b") == SourceKind.GITLAB assert detect_source_kind("file:///tmp/a") == SourceKind.LOCAL +@pytest.mark.parametrize( + "url", + [ + "https://github.com/myorg/gitlab.com-mirror", + "git@github.com:myorg/gitlab.com-mirror", + "https://mygitlab.com/org/repo", + "git@gitlab.com.example.org:org/repo", + ], +) +def test_detect_source_kind_ignores_non_gitlab_hosts_containing_gitlab_substring(url): + assert detect_source_kind(url) == SourceKind.GITHUB + + def _allowed(name: str, kind: SourceKind) -> bool: req = getattr(PIPELINES[name], "required_capabilities", frozenset()) return req <= capabilities_for(kind)