diff --git a/ddtrace/internal/ci_visibility/recorder.py b/ddtrace/internal/ci_visibility/recorder.py index 595e0688996..f3d70d6b58f 100644 --- a/ddtrace/internal/ci_visibility/recorder.py +++ b/ddtrace/internal/ci_visibility/recorder.py @@ -3,6 +3,7 @@ import json import os from pathlib import Path +import re import socket from typing import TYPE_CHECKING # noqa:F401 from typing import NamedTuple # noqa:F401 @@ -102,12 +103,17 @@ class CIVisibilityAuthenticationException(Exception): pass -def _extract_repository_name_from_url(repository_url): - # type: (str) -> str +def _extract_repository_name_from_url(repository_url: str) -> str: + _REPO_NAME_REGEX = r".*/(?P.*?)(\.git)?$" + try: - return parse.urlparse(repository_url).path.rstrip(".git").rpartition("/")[-1] + url_path = parse.urlparse(repository_url).path + matches = re.match(_REPO_NAME_REGEX, url_path, flags=re.IGNORECASE) + if matches: + return matches.group("repo_name") + log.warning("Cannot extract repository name from unexpected URL path: %s", url_path) + return repository_url except ValueError: - # In case of parsing error, default to repository url log.warning("Repository name cannot be parsed from repository_url: %s", repository_url) return repository_url diff --git a/releasenotes/notes/ci_visibility-fix_properly_strip_dotgit_from_repo_url-523a908075aea559.yaml b/releasenotes/notes/ci_visibility-fix_properly_strip_dotgit_from_repo_url-523a908075aea559.yaml new file mode 100644 index 00000000000..10cb457166d --- /dev/null +++ b/releasenotes/notes/ci_visibility-fix_properly_strip_dotgit_from_repo_url-523a908075aea559.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + CI Visibility: Fixes a bug where ``.git`` was incorrectly being stripped from repository URLs when extracting service names, + resulting in ``g``, ``i``, or ``t`` being removed (eg: ``test-environment.git`` incorrectly becoming ``test-environmen``) diff --git a/tests/ci_visibility/test_ci_visibility.py b/tests/ci_visibility/test_ci_visibility.py index 527ca3c59fa..430ce6203a7 100644 --- a/tests/ci_visibility/test_ci_visibility.py +++ b/tests/ci_visibility/test_ci_visibility.py @@ -311,6 +311,16 @@ def test_ci_visibility_service_disable(): ("git+git://github.com/org/repo-name.git", "repo-name"), ("git+ssh://github.com/org/repo-name.git", "repo-name"), ("git+https://github.com/org/repo-name.git", "repo-name"), + ("https://github.com/fastapi/fastapi.git", "fastapi"), + ("git@github.com:fastapi/fastapi.git", "fastapi"), + ("git@github.com:fastapi/fastapi.gitttttt", "fastapi.gitttttt"), + ("git@github.com:fastapi/fastapiiiititititi.git", "fastapiiiititititi"), + ("https://github.com/fastapi/fastapitttttt.git", "fastapitttttt"), + ("this is definitely not a valid git repo URL", "this is definitely not a valid git repo URL"), + ("git@github.com:fastapi/FastAPI.GiT", "FastAPI"), + ("git+https://github.com/org/REPO-NAME.GIT", "REPO-NAME"), + ("https://github.com/DataDog/DD-TRACE-py", "DD-TRACE-py"), + ("https://github.com/DataDog/dd-trace-py.GIT", "dd-trace-py"), ], ) def test_repository_name_extracted(repository_url, repository_name):