feat(agent/telemetry): Distinguish between production and dev environment based on VCS state

- Added a helper function `.app.utils.vcs_state_diverges_from_master()`. This function determines whether the relevant part of the codebase diverges from our `master`.
- Updated `.app.telemetry._setup_sentry()` to determine the default environment name using `vcs_state_diverges_from_master`.
This commit is contained in:
Reinier van der Leer
2024-02-15 16:00:30 +01:00
parent b7f08cd0f7
commit fd5730b04a
2 changed files with 55 additions and 2 deletions

View File

@@ -3,7 +3,12 @@ import os
import click
from colorama import Fore, Style
from .utils import env_file_exists, get_git_user_email, set_env_config_value
from .utils import (
env_file_exists,
get_git_user_email,
set_env_config_value,
vcs_state_diverges_from_master,
)
def setup_telemetry() -> None:
@@ -49,7 +54,10 @@ def _setup_sentry() -> None:
sentry_sdk.init(
dsn="https://dc266f2f7a2381194d1c0fa36dff67d8@o4505260022104064.ingest.sentry.io/4506739844710400", # noqa
enable_tracing=True,
environment=os.getenv("TELEMETRY_ENVIRONMENT"),
environment=os.getenv(
"TELEMETRY_ENVIRONMENT",
"production" if not vcs_state_diverges_from_master() else "dev",
),
)
# Allow Sentry to distinguish between users

View File

@@ -1,3 +1,4 @@
import contextlib
import logging
import os
import re
@@ -84,6 +85,50 @@ def get_current_git_branch() -> str:
return ""
def vcs_state_diverges_from_master() -> bool:
"""
Returns whether a git repo is present and contains changes that are not in `master`.
"""
paths_we_care_about = "autogpts/autogpt/autogpt/**/*.py"
try:
repo = Repo(search_parent_directories=True)
# Check for uncommitted changes in the specified path
uncommitted_changes = repo.index.diff(None, paths=paths_we_care_about)
if uncommitted_changes:
return True
# Find OG AutoGPT remote
for remote in repo.remotes:
if remote.url.endswith(
tuple(
# All permutations of old/new repo name and HTTP(S)/Git URLs
f"{prefix}{path}"
for prefix in ("://github.com/", "git@github.com:")
for path in (
f"Significant-Gravitas/{n}.git" for n in ("AutoGPT", "Auto-GPT")
)
)
):
og_remote = remote
break
else:
# Original AutoGPT remote is not configured: assume local codebase diverges
return True
master_branch = og_remote.refs.master
with contextlib.suppress(StopIteration):
next(repo.iter_commits(f"HEAD..{master_branch}", paths=paths_we_care_about))
# Local repo is one or more commits ahead of OG AutoGPT master branch
return True
# Relevant part of the codebase is on master
return False
except InvalidGitRepositoryError:
# No git repo present: assume codebase is a clean download
return False
def get_git_user_email() -> str:
try:
repo = Repo(search_parent_directories=True)