Skip to content

Commit

Permalink
fix(ci_visibility): avoid git tracebacks when .git dir is absent
Browse files Browse the repository at this point in the history
  • Loading branch information
brettdh authored and Brett Higgins committed Oct 19, 2024
1 parent f405615 commit 9ca376d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
17 changes: 17 additions & 0 deletions ddtrace/internal/ci_visibility/git_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ def wait_for_metadata_upload_status(self):
self._wait_for_metadata_upload()
return self._metadata_upload_status.value # type: ignore

@classmethod
def _get_git_dir(cls, cwd=None):
# type: (Optional[str]) -> Optional[str]
if cwd is None:
cwd = os.getcwd()

git_dir = os.path.join(cwd, ".git")
if not os.path.exists(git_dir):
return None

return git_dir

@classmethod
def _run_protocol(
cls,
Expand All @@ -166,6 +178,11 @@ def _run_protocol(
log.setLevel(log_level)
_metadata_upload_status.value = METADATA_UPLOAD_STATUS.IN_PROCESS
try:
if not cls._get_git_dir(cwd=cwd):
log.debug("Missing .git directory; skipping git metadata upload")
_metadata_upload_status.value = METADATA_UPLOAD_STATUS.FAILED
return

if _tags is None:
_tags = {}
repo_url = cls._get_repository_url(tags=_tags, cwd=cwd)
Expand Down
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 @@ -843,6 +843,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

0 comments on commit 9ca376d

Please sign in to comment.