Skip to content

Commit

Permalink
Make distinction between short and full SHA
Browse files Browse the repository at this point in the history
  • Loading branch information
Twixes committed Feb 14, 2024
1 parent a207f56 commit 4bb954e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
6 changes: 4 additions & 2 deletions posthog/api/instance_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions posthog/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 16 additions & 3 deletions posthog/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions posthog/settings/sentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions posthog/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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,
}

Expand Down

0 comments on commit 4bb954e

Please sign in to comment.