From 5440c9f03b415de0fc267d363efc24743429941d Mon Sep 17 00:00:00 2001 From: zurdi Date: Fri, 18 Sep 2026 12:51:00 +0200 Subject: [PATCH 1/6] feat(about): show current git branch instead of "development" Local checkouts have no release tag, so heartbeat.SYSTEM.VERSION always falls back to the literal string "development". Add a GIT_BRANCH field (backend-resolved via git, display-only, not used in the version string that also feeds outbound User-Agent headers) and use it in the About dialog and the auth-screen VersionTag so a dev build shows the branch being worked on instead. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01TDMqXzXexRgC89YxeNjn9q --- backend/endpoints/heartbeat.py | 6 ++- backend/endpoints/responses/heartbeat.py | 1 + backend/tests/endpoints/test_heartbeat.py | 5 ++- backend/tests/utils/test_git_branch.py | 37 +++++++++++++++++++ backend/utils/__init__.py | 30 +++++++++++++++ .../src/__generated__/models/SystemDict.ts | 1 + frontend/src/stores/heartbeat.ts | 1 + .../src/v2/components/Dialogs/AboutDialog.vue | 8 ++-- .../src/v2/components/shared/VersionTag.vue | 16 +++----- .../v2/composables/useVersionDisplay/index.ts | 28 ++++++++++++++ 10 files changed, 115 insertions(+), 18 deletions(-) create mode 100644 backend/tests/utils/test_git_branch.py create mode 100644 frontend/src/v2/composables/useVersionDisplay/index.ts diff --git a/backend/endpoints/heartbeat.py b/backend/endpoints/heartbeat.py index 60acc35a2..074af1e5a 100644 --- a/backend/endpoints/heartbeat.py +++ b/backend/endpoints/heartbeat.py @@ -59,7 +59,7 @@ from handler.metadata import ( from handler.redis_handler import sync_cache from handler.scan_handler import MetadataSource from logger.logger import log -from utils import get_version +from utils import get_git_branch, get_version from utils.platforms import get_supported_platforms from utils.rate_limit import enforce_rate_limit, get_client_ip from utils.router import APIRouter @@ -109,9 +109,11 @@ async def heartbeat() -> HeartbeatResponse: tgdb_enabled = meta_tgdb_handler.is_enabled() libretro_enabled = meta_libretro_handler.is_enabled() + version = get_version() return { "SYSTEM": { - "VERSION": get_version(), + "VERSION": version, + "GIT_BRANCH": get_git_branch() if version == "development" else None, "SHOW_SETUP_WIZARD": len(db_user_handler.get_admin_users()) == 0 and not DISABLE_SETUP_WIZARD, }, diff --git a/backend/endpoints/responses/heartbeat.py b/backend/endpoints/responses/heartbeat.py index 6479afa51..4803d1205 100644 --- a/backend/endpoints/responses/heartbeat.py +++ b/backend/endpoints/responses/heartbeat.py @@ -3,6 +3,7 @@ from typing import TypedDict class SystemDict(TypedDict): VERSION: str + GIT_BRANCH: str | None SHOW_SETUP_WIZARD: bool diff --git a/backend/tests/endpoints/test_heartbeat.py b/backend/tests/endpoints/test_heartbeat.py index 3b46d44bb..d17c80192 100644 --- a/backend/tests/endpoints/test_heartbeat.py +++ b/backend/tests/endpoints/test_heartbeat.py @@ -10,7 +10,7 @@ from endpoints.heartbeat import METADATA_HEARTBEAT_RATE_LIMIT from exceptions.fs_exceptions import PlatformAlreadyExistsException from handler.metadata.launchbox_handler.handler import LaunchboxHandler from handler.redis_handler import sync_cache -from utils import get_version +from utils import get_git_branch, get_version def test_heartbeat(client): @@ -22,6 +22,9 @@ def test_heartbeat(client): assert "SYSTEM" in heartbeat system = heartbeat["SYSTEM"] assert system["VERSION"] == get_version() + assert system["GIT_BRANCH"] == ( + get_git_branch() if system["VERSION"] == "development" else None + ) assert isinstance(system["SHOW_SETUP_WIZARD"], bool) assert "METADATA_SOURCES" in heartbeat diff --git a/backend/tests/utils/test_git_branch.py b/backend/tests/utils/test_git_branch.py new file mode 100644 index 000000000..3b261f112 --- /dev/null +++ b/backend/tests/utils/test_git_branch.py @@ -0,0 +1,37 @@ +import subprocess +from unittest.mock import patch + +from utils import get_git_branch + + +def test_get_git_branch_returns_current_branch(): + get_git_branch.cache_clear() + try: + with patch("utils.subprocess.run") as mock_run: + mock_run.return_value = subprocess.CompletedProcess( + args=[], returncode=0, stdout="feature/foo\n" + ) + assert get_git_branch() == "feature/foo" + finally: + get_git_branch.cache_clear() + + +def test_get_git_branch_returns_none_when_git_unavailable(): + get_git_branch.cache_clear() + try: + with patch("utils.subprocess.run", side_effect=FileNotFoundError): + assert get_git_branch() is None + finally: + get_git_branch.cache_clear() + + +def test_get_git_branch_returns_none_on_detached_head(): + get_git_branch.cache_clear() + try: + with patch("utils.subprocess.run") as mock_run: + mock_run.return_value = subprocess.CompletedProcess( + args=[], returncode=0, stdout="HEAD\n" + ) + assert get_git_branch() is None + finally: + get_git_branch.cache_clear() diff --git a/backend/utils/__init__.py b/backend/utils/__init__.py index 180c73cb2..5b3dbbb80 100644 --- a/backend/utils/__init__.py +++ b/backend/utils/__init__.py @@ -1,5 +1,8 @@ import re +import subprocess import uuid +from functools import lru_cache +from pathlib import Path from typing import Any, Final from __version__ import __version__ @@ -7,6 +10,8 @@ from __version__ import __version__ # Embedded straight into a YouTube URL, so anything outside this must be dropped. YOUTUBE_ID_RE: Final = re.compile(r"[A-Za-z0-9_-]{11}") +_REPO_ROOT: Final = Path(__file__).resolve().parent.parent.parent + def get_version() -> str: """Returns current version tag""" @@ -16,6 +21,31 @@ def get_version() -> str: return "development" +@lru_cache(maxsize=1) +def get_git_branch() -> str | None: + """Returns the current git branch for display, or None outside a git checkout. + + Only meaningful for unreleased builds (get_version() == "development"); a + release image has no `.git` directory. Cached since the branch can't + change for the lifetime of the process. + """ + try: + result = subprocess.run( + ["git", "rev-parse", "--abbrev-ref", "HEAD"], + cwd=_REPO_ROOT, + capture_output=True, + text=True, + check=True, + timeout=2, + shell=False, # trunk-ignore(bandit/B603): git binary is fixed, args are hardcoded + ) + except (OSError, subprocess.SubprocessError): + return None + + branch = result.stdout.strip() + return branch if branch and branch != "HEAD" else None + + def is_valid_uuid(uuid_str: str) -> bool: """Check if a string is a valid UUID.""" try: diff --git a/frontend/src/__generated__/models/SystemDict.ts b/frontend/src/__generated__/models/SystemDict.ts index addfd05a4..9a682d7dd 100644 --- a/frontend/src/__generated__/models/SystemDict.ts +++ b/frontend/src/__generated__/models/SystemDict.ts @@ -4,6 +4,7 @@ /* eslint-disable */ export type SystemDict = { VERSION: string; + GIT_BRANCH: (string | null); SHOW_SETUP_WIZARD: boolean; }; diff --git a/frontend/src/stores/heartbeat.ts b/frontend/src/stores/heartbeat.ts index dd3e1d791..93b3e9980 100644 --- a/frontend/src/stores/heartbeat.ts +++ b/frontend/src/stores/heartbeat.ts @@ -15,6 +15,7 @@ export type MetadataOption = { const defaultHeartbeat: Heartbeat = { SYSTEM: { VERSION: "0.0.0", + GIT_BRANCH: null, SHOW_SETUP_WIZARD: false, }, METADATA_SOURCES: { diff --git a/frontend/src/v2/components/Dialogs/AboutDialog.vue b/frontend/src/v2/components/Dialogs/AboutDialog.vue index a16bfed76..dd2fd4424 100644 --- a/frontend/src/v2/components/Dialogs/AboutDialog.vue +++ b/frontend/src/v2/components/Dialogs/AboutDialog.vue @@ -6,13 +6,13 @@ import { RDialog, RIcon, RImg } from "@v2/lib"; import type { Emitter } from "mitt"; import { inject, onBeforeUnmount, ref } from "vue"; import { useI18n } from "vue-i18n"; -import storeHeartbeat from "@/stores/heartbeat"; import type { Events } from "@/types/emitter"; +import { useVersionDisplay } from "@/v2/composables/useVersionDisplay"; defineOptions({ inheritAttrs: false }); const { t } = useI18n(); -const heartbeatStore = storeHeartbeat(); +const { version, href: versionHref } = useVersionDisplay(); const emitter = inject>("emitter"); const show = ref(false); @@ -38,8 +38,8 @@ const links: Link[] = [ { isotipo: true, label: t("common.about-version"), - value: heartbeatStore.value.SYSTEM.VERSION, - href: `https://github.com/rommapp/romm/releases/tag/${heartbeatStore.value.SYSTEM.VERSION}`, + value: version.value, + href: versionHref.value, }, { icon: "mdi-code-braces", diff --git a/frontend/src/v2/components/shared/VersionTag.vue b/frontend/src/v2/components/shared/VersionTag.vue index b7c7a3905..01d8ba1a9 100644 --- a/frontend/src/v2/components/shared/VersionTag.vue +++ b/frontend/src/v2/components/shared/VersionTag.vue @@ -1,10 +1,8 @@