Skip to content
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

fix(ci_visibility): properly strip .git from repo URL when getting repo name #10556

Merged
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
14 changes: 10 additions & 4 deletions ddtrace/internal/ci_visibility/recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 Any # noqa:F401
Expand Down Expand Up @@ -103,12 +104,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<repo_name>.*?)(\.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

Expand Down
Original file line number Diff line number Diff line change
@@ -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``)
10 changes: 10 additions & 0 deletions tests/ci_visibility/test_ci_visibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,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):
Expand Down
Loading