diff --git a/posthog/api/instance_status.py b/posthog/api/instance_status.py index 88cb8e2bfcd53..7e800dacfee8c 100644 --- a/posthog/api/instance_status.py +++ b/posthog/api/instance_status.py @@ -12,7 +12,7 @@ from posthog.async_migrations.status import async_migrations_ok from posthog.cloud_utils import is_cloud -from posthog.git import get_git_commit +from posthog.git import get_git_commit_short from posthog.permissions import SingleTenancyOrAdmin from posthog.storage import object_storage from posthog.utils import ( @@ -42,7 +42,9 @@ def list(self, request: Request) -> Response: metrics: List[Dict[str, Union[str, bool, int, float, Dict[str, Any]]]] = [] - metrics.append({"key": "posthog_git_sha", "metric": "PostHog Git SHA", "value": get_git_commit() or "unknown"}) + metrics.append( + {"key": "posthog_git_sha", "metric": "PostHog Git SHA", "value": get_git_commit_short() or "unknown"} + ) helm_info = get_helm_info_env() if len(helm_info) > 0: diff --git a/posthog/apps.py b/posthog/apps.py index 710ee74bf67e8..cd5a06f91fdca 100644 --- a/posthog/apps.py +++ b/posthog/apps.py @@ -5,7 +5,7 @@ from django.apps import AppConfig from django.conf import settings from posthoganalytics.client import Client -from posthog.git import get_git_branch, get_git_commit +from posthog.git import get_git_branch, get_git_commit_short from posthog.settings import SELF_CAPTURE, SKIP_ASYNC_MIGRATIONS_SETUP from posthog.tasks.tasks import sync_all_organization_available_features @@ -43,7 +43,7 @@ def ready(self): phcloud_client.capture( get_machine_id(), "development server launched", - {"git_rev": get_git_commit(), "git_branch": get_git_branch()}, + {"git_rev": get_git_commit_short(), "git_branch": get_git_branch()}, ) local_api_key = get_self_capture_api_token(None) diff --git a/posthog/git.py b/posthog/git.py index 501b648ca3e9c..57f0c9b273e66 100644 --- a/posthog/git.py +++ b/posthog/git.py @@ -11,13 +11,26 @@ pass -def get_git_commit() -> Optional[str]: - """Return the short hash of the last commit. +def get_git_commit_full() -> Optional[str]: + """Return the full hash of the last commit. - Example: get_git_commit() => "4ff54c8d" + Example: get_git_commit_full() => "86a3c3b529d18a1d7400f0f4203bf6b508ba3b8e" """ if _git_commit_baked_in: return _git_commit_baked_in + try: + return subprocess.check_output(["git", "rev-parse", "HEAD"]).decode("utf-8").strip() + except Exception: + return None + + +def get_git_commit_short() -> Optional[str]: + """Return the short hash of the last commit. + + Example: get_git_commit_short() => "86a3c3b529" + """ + if _git_commit_baked_in: + return _git_commit_baked_in[:10] # 10 characters is almost guaranteed to identify a commit uniquely try: return subprocess.check_output(["git", "rev-parse", "--short", "HEAD"]).decode("utf-8").strip() except Exception: diff --git a/posthog/settings/sentry.py b/posthog/settings/sentry.py index bc60e13646706..545d4ed94db2e 100644 --- a/posthog/settings/sentry.py +++ b/posthog/settings/sentry.py @@ -9,7 +9,7 @@ from sentry_sdk.integrations.django import DjangoIntegration from sentry_sdk.integrations.logging import LoggingIntegration from sentry_sdk.integrations.redis import RedisIntegration -from posthog.git import get_git_commit +from posthog.git import get_git_commit_full from posthog.settings import get_from_env from posthog.settings.base_variables import TEST @@ -144,7 +144,7 @@ def sentry_init() -> None: sentry_logging = LoggingIntegration(level=sentry_logging_level, event_level=None) profiles_sample_rate = get_from_env("SENTRY_PROFILES_SAMPLE_RATE", type_cast=float, default=0.0) - release = get_git_commit() + release = get_git_commit_full() sentry_sdk.init( send_default_pii=send_pii, diff --git a/posthog/utils.py b/posthog/utils.py index 097d5c12beccf..3d2d0a5288bad 100644 --- a/posthog/utils.py +++ b/posthog/utils.py @@ -52,7 +52,7 @@ from posthog.cloud_utils import get_cached_instance_license, is_cloud from posthog.constants import AvailableFeature from posthog.exceptions import RequestParsingError -from posthog.git import get_git_branch, get_git_commit +from posthog.git import get_git_branch, get_git_commit_short from posthog.metrics import KLUDGES_COUNTER from posthog.redis import get_client @@ -294,7 +294,7 @@ def render_template( if sentry_environment := os.environ.get("SENTRY_ENVIRONMENT"): context["sentry_environment"] = sentry_environment - context["git_rev"] = get_git_commit() # Include commit in prod for the `console.info()` message + context["git_rev"] = get_git_commit_short() # Include commit in prod for the `console.info()` message if settings.DEBUG and not settings.TEST: context["debug"] = True context["git_branch"] = get_git_branch() @@ -345,7 +345,7 @@ def render_template( "preflight": json.loads(preflight_check(request).getvalue()), "default_event_name": "$pageview", "switched_team": getattr(request, "switched_team", None), - "commit_sha": get_git_commit(), + "commit_sha": context["git_rev"], **posthog_app_context, }