Skip to content

Commit 78fc4f3

Browse files
fix: check git env vars before using the command on the source code integration [backport 3.13] (#14620)
Backport d2812f2 from #14613 to 3.13. The sourcecode integration was using git to retrieve the commit hash and repository url. However, we already have defined `DD_GIT_COMMIT_HASH` and `DD_GIT_REPOSITORY_URL` for these cases, so check these vars before. ## Checklist - [X] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) Signed-off-by: Juanjo Alvarez <[email protected]> Co-authored-by: Juanjo Alvarez Martinez <[email protected]>
1 parent 827192e commit 78fc4f3

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

ddtrace/sourcecode/_utils.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import os
12
import re
2-
import subprocess
33
from urllib import parse
44

55

@@ -56,15 +56,23 @@ def normalize_repository_url(url):
5656

5757

5858
def _query_git(args):
59+
import subprocess # don't import subprocess (and maybe activate the integration) if not needed
60+
5961
ver = subprocess.Popen(["git"] + args, stdout=subprocess.PIPE).communicate()[0]
6062
return ver.strip().decode("utf-8")
6163

6264

6365
def get_commit_sha():
66+
commit_sha = os.environ.get("DD_GIT_COMMIT_SHA")
67+
if commit_sha:
68+
return commit_sha
6469
return _query_git(["rev-parse", "HEAD"])
6570

6671

6772
def get_repository_url():
73+
repository_url = os.environ.get("DD_GIT_REPOSITORY_URL")
74+
if repository_url:
75+
return repository_url
6876
return _query_git(["config", "--get", "remote.origin.url"])
6977

7078

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
source code integration: check that ``DD_GIT_COMMIT_SHA`` and ``DD_GIT_REPOSITORY_URL`` are defined before
5+
using the git command.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import os
2+
from unittest import mock
3+
4+
from ddtrace.sourcecode._utils import get_commit_sha
5+
from ddtrace.sourcecode._utils import get_repository_url
6+
7+
8+
class TestSourceCodeEnvVars:
9+
def test_get_commit_sha_uses_env_var_when_present(self):
10+
test_sha = "abc123def456"
11+
with mock.patch.dict(os.environ, {"DD_GIT_COMMIT_SHA": test_sha}):
12+
with mock.patch("ddtrace.sourcecode._utils._query_git") as mock_git:
13+
result = get_commit_sha()
14+
assert result == test_sha
15+
mock_git.assert_not_called()
16+
17+
def test_get_commit_sha_calls_git_when_env_var_not_present(self):
18+
test_sha = "git_result_sha"
19+
with mock.patch.dict(os.environ, {}, clear=True):
20+
with mock.patch("ddtrace.sourcecode._utils._query_git", return_value=test_sha) as mock_git:
21+
result = get_commit_sha()
22+
assert result == test_sha
23+
mock_git.assert_called_once_with(["rev-parse", "HEAD"])
24+
25+
def test_get_commit_sha_calls_git_when_env_var_empty(self):
26+
test_sha = "git_result_sha"
27+
with mock.patch.dict(os.environ, {"DD_GIT_COMMIT_SHA": ""}):
28+
with mock.patch("ddtrace.sourcecode._utils._query_git", return_value=test_sha) as mock_git:
29+
result = get_commit_sha()
30+
assert result == test_sha
31+
mock_git.assert_called_once_with(["rev-parse", "HEAD"])
32+
33+
def test_get_repository_url_uses_env_var_when_present(self):
34+
test_url = "https://github.com/user/repo.git"
35+
with mock.patch.dict(os.environ, {"DD_GIT_REPOSITORY_URL": test_url}):
36+
with mock.patch("ddtrace.sourcecode._utils._query_git") as mock_git:
37+
result = get_repository_url()
38+
assert result == test_url
39+
mock_git.assert_not_called()
40+
41+
def test_get_repository_url_calls_git_when_env_var_not_present(self):
42+
test_url = "git_result_url"
43+
with mock.patch.dict(os.environ, {}, clear=True):
44+
with mock.patch("ddtrace.sourcecode._utils._query_git", return_value=test_url) as mock_git:
45+
result = get_repository_url()
46+
assert result == test_url
47+
mock_git.assert_called_once_with(["config", "--get", "remote.origin.url"])
48+
49+
def test_get_repository_url_calls_git_when_env_var_empty(self):
50+
test_url = "git_result_url"
51+
with mock.patch.dict(os.environ, {"DD_GIT_REPOSITORY_URL": ""}):
52+
with mock.patch("ddtrace.sourcecode._utils._query_git", return_value=test_url) as mock_git:
53+
result = get_repository_url()
54+
assert result == test_url
55+
mock_git.assert_called_once_with(["config", "--get", "remote.origin.url"])

0 commit comments

Comments
 (0)