Skip to content

Commit

Permalink
ref(dependencies): Adding more context to sentry (#181)
Browse files Browse the repository at this point in the history
* ref(dependencies): Adding more context to sentry

* Fixing test

* Fixing test
  • Loading branch information
IanWoodard authored Dec 18, 2024
1 parent 0cbaae9 commit a2a6f3e
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
49 changes: 49 additions & 0 deletions devservices/utils/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from typing import TextIO
from typing import TypeGuard

from sentry_sdk import set_context

from devservices.configs.service_config import Dependency
from devservices.configs.service_config import load_service_config_from_file
from devservices.configs.service_config import RemoteConfig
Expand All @@ -36,6 +38,17 @@
from devservices.utils.services import Service
from devservices.utils.state import State

RELEVANT_GIT_CONFIG_KEYS = [
"init.defaultbranch",
"core.sparsecheckout",
"remote.origin.url",
"remote.origin.fetch",
"remote.origin.promisor",
"remote.origin.partialclonefilter",
"protocol.version",
"extensions.partialclone",
]


class DependencyGraph:
def __init__(self) -> None:
Expand Down Expand Up @@ -149,6 +162,28 @@ def ensure_config(self) -> None:
if self.sparse_pattern:
self.sparse_checkout_manager.set_sparse_checkout(self.sparse_pattern)

def get_relevant_config(self) -> dict[str, str]:
"""
Get the relevant git config entries (to avoid logging sensitive information)
"""
git_config = (
subprocess.check_output(
["git", "config", "--list"],
cwd=self.repo_dir,
stderr=subprocess.PIPE,
)
.decode()
.strip()
)
git_config_dict = dict()
for line in git_config.split("\n"):
if not line:
continue
key, value = line.split("=")
if key in RELEVANT_GIT_CONFIG_KEYS:
git_config_dict[key] = value
return git_config_dict

def _set_config(self, key: str, value: str) -> None:
"""
Set a git config option for the repo
Expand Down Expand Up @@ -411,12 +446,15 @@ def _update_dependency(
repo_link=dependency.repo_link,
branch=dependency.branch,
) from e

try:
_run_command(
["git", "fetch", "origin", dependency.branch, "--filter=blob:none"],
cwd=dependency_repo_dir,
)
except subprocess.CalledProcessError as e:
# Try to set the git config context to help with debugging
_try_set_git_config_context(git_config_manager)
raise DependencyError(
repo_name=dependency.repo_name,
repo_link=dependency.repo_link,
Expand Down Expand Up @@ -536,6 +574,17 @@ def _run_command(
subprocess.run(cmd, cwd=cwd, check=True, stdout=stdout, stderr=subprocess.DEVNULL)


def _try_set_git_config_context(
git_config_manager: GitConfigManager,
) -> None:
try:
git_config = git_config_manager.get_relevant_config()
set_context("git_config", git_config)
except subprocess.CalledProcessError as e:
logger = logging.getLogger(LOGGER_NAME)
logger.exception(e)


def get_remote_dependency_config(remote_config: RemoteConfig) -> ServiceConfig:
dependency_repo_dir = os.path.join(
DEVSERVICES_DEPENDENCIES_CACHE_DIR,
Expand Down
48 changes: 48 additions & 0 deletions tests/utils/test_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,54 @@ def test_git_config_manager_ensure_config_sparse_checkout_overwrite(
)


def test_git_config_manager_get_relevant_config_mostly_empty(tmp_path: Path) -> None:
repo_dir = tmp_path / "test-repo"
create_mock_git_repo("basic_repo", repo_dir)
git_config_manager = GitConfigManager(
str(repo_dir),
{
"init.defaultbranch": "main",
},
)
git_config_manager.ensure_config()
relevant_configs = git_config_manager.get_relevant_config()
assert relevant_configs == {
"init.defaultbranch": "main",
}


def test_git_config_manager_get_relevant_config_populated(tmp_path: Path) -> None:
repo_dir = tmp_path / "test-repo"
create_mock_git_repo("basic_repo", repo_dir)
git_config_manager = GitConfigManager(
str(repo_dir),
{
"init.defaultbranch": "main",
"core.sparsecheckout": "true",
"remote.origin.url": "test-url",
"remote.origin.fetch": "test-fetch",
"remote.origin.promisor": "true",
"remote.origin.partialclonefilter": "blob:none",
"protocol.version": "2",
"extensions.partialclone": "true",
"core.filemode": "true", # We don't care about this one
},
f"{DEVSERVICES_DIR_NAME}/",
)
git_config_manager.ensure_config()
relevant_configs = git_config_manager.get_relevant_config()
assert relevant_configs == {
"init.defaultbranch": "main",
"core.sparsecheckout": "true",
"remote.origin.url": "test-url",
"remote.origin.fetch": "test-fetch",
"remote.origin.promisor": "true",
"remote.origin.partialclonefilter": "blob:none",
"protocol.version": "2",
"extensions.partialclone": "true",
}


def test_verify_local_dependencies_no_dependencies(tmp_path: Path) -> None:
with mock.patch(
"devservices.utils.dependencies.DEVSERVICES_DEPENDENCIES_CACHE_DIR",
Expand Down

0 comments on commit a2a6f3e

Please sign in to comment.