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): avoid git tracebacks when .git dir is absent [backport 2.12] #11195

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions ddtrace/internal/ci_visibility/git_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from ddtrace.ext.git import extract_commit_sha
from ddtrace.ext.git import extract_git_version
from ddtrace.ext.git import extract_remote_url
from ddtrace.ext.git import extract_workspace_path
from ddtrace.internal.agent import get_trace_url
from ddtrace.internal.compat import JSONDecodeError
from ddtrace.internal.logger import get_logger
Expand Down Expand Up @@ -94,8 +95,20 @@ def __init__(
elif self._requests_mode == REQUESTS_MODE.AGENTLESS_EVENTS:
self._base_url = "https://api.{}{}".format(os.getenv("DD_SITE", AGENTLESS_DEFAULT_SITE), GIT_API_BASE_PATH)

def _get_git_dir(self, cwd=None):
# type: (Optional[str]) -> Optional[str]
try:
return extract_workspace_path(cwd=cwd)
except ValueError:
return None

def upload_git_metadata(self, cwd=None):
# type: (Optional[str]) -> None
if not self._get_git_dir(cwd=cwd):
log.debug("Missing .git directory; skipping git metadata upload")
self._metadata_upload_status.value = METADATA_UPLOAD_STATUS.FAILED # type: ignore[attr-defined]
return

self._tags = ci.tags(cwd=cwd)
if self._worker is None:
self._worker = Process(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- CI Visibility: fixes unnecessary logging of an exception that would appear when trying to upload git metadata in
an environment without functioning git (eg: missing ``git`` binary or ``.git`` directory)
9 changes: 9 additions & 0 deletions tests/ci_visibility/test_ci_visibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,15 @@ def test_upload_git_metadata_upload_unnecessary(self, api_key, requests_mode):
git_client.upload_git_metadata()
assert git_client.wait_for_metadata_upload_status() == METADATA_UPLOAD_STATUS.UNNECESSARY

@pytest.mark.parametrize("api_key, requests_mode", api_key_requests_mode_parameters)
def test_upload_git_metadata_upload_no_git_dir(self, api_key, requests_mode):
with mock.patch.object(CIVisibilityGitClient, "_get_git_dir", mock.Mock(return_value=None)):
git_client = CIVisibilityGitClient(api_key, requests_mode)
git_client.upload_git_metadata()

# Notably, this should _not_ raise ValueError
assert git_client.wait_for_metadata_upload_status() == METADATA_UPLOAD_STATUS.FAILED


def test_get_filtered_revisions():
with mock.patch(
Expand Down