mirror of
https://github.com/rommapp/romm.git
synced 2026-11-29 10:01:00 +01:00
Merge pull request #4617 from rommapp/claude/dev-build-git-branch
feat(about): show current git branch instead of "development"
This commit is contained in:
@@ -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,
|
||||
},
|
||||
|
||||
@@ -3,6 +3,7 @@ from typing import TypedDict
|
||||
|
||||
class SystemDict(TypedDict):
|
||||
VERSION: str
|
||||
GIT_BRANCH: str | None
|
||||
SHOW_SETUP_WIZARD: bool
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
52
backend/tests/utils/test_git_branch.py
Normal file
52
backend/tests/utils/test_git_branch.py
Normal file
@@ -0,0 +1,52 @@
|
||||
import subprocess
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from utils import _REPO_ROOT, get_git_branch
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _clear_branch_cache():
|
||||
get_git_branch.cache_clear()
|
||||
yield
|
||||
get_git_branch.cache_clear()
|
||||
|
||||
|
||||
def test_get_git_branch_returns_current_branch():
|
||||
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"
|
||||
|
||||
|
||||
def test_get_git_branch_trusts_the_repo_root_as_a_safe_directory():
|
||||
"""A host-owned .git bind-mounted into a root-run container is otherwise
|
||||
refused by git 2.35.2+ as having "dubious ownership"."""
|
||||
with patch("utils.subprocess.run") as mock_run:
|
||||
mock_run.return_value = subprocess.CompletedProcess(
|
||||
args=[], returncode=0, stdout="main\n"
|
||||
)
|
||||
get_git_branch()
|
||||
args = mock_run.call_args.args[0]
|
||||
assert f"safe.directory={_REPO_ROOT}" in args
|
||||
|
||||
|
||||
def test_get_git_branch_returns_none_when_git_unavailable():
|
||||
with patch("utils.subprocess.run", side_effect=FileNotFoundError):
|
||||
assert get_git_branch() is None
|
||||
|
||||
|
||||
def test_get_git_branch_returns_none_on_detached_head():
|
||||
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
|
||||
|
||||
|
||||
def test_get_git_branch_returns_none_when_not_a_git_repo():
|
||||
with patch("utils.subprocess.run") as mock_run:
|
||||
mock_run.side_effect = subprocess.CalledProcessError(128, ["git"])
|
||||
assert get_git_branch() is None
|
||||
@@ -1,5 +1,8 @@
|
||||
import re
|
||||
import subprocess # trunk-ignore(bandit/B404): only used by get_git_branch, argv is hardcoded
|
||||
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,36 @@ def get_version() -> str:
|
||||
return "development"
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def get_git_branch() -> str | None:
|
||||
"""Current git branch for a dev build's display, or None outside a checkout (cached for the process's life)."""
|
||||
try:
|
||||
# The Docker dev image's bind-mounted .git is host-owned while the
|
||||
# container runs as root, which git 2.35.2+ refuses as "dubious ownership".
|
||||
# trunk-ignore(bandit/B607): git is resolved from PATH by design
|
||||
result = subprocess.run(
|
||||
[
|
||||
"git",
|
||||
"-c",
|
||||
f"safe.directory={_REPO_ROOT}",
|
||||
"rev-parse",
|
||||
"--abbrev-ref",
|
||||
"HEAD",
|
||||
],
|
||||
cwd=_REPO_ROOT,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=True,
|
||||
timeout=2,
|
||||
shell=False, # trunk-ignore(bandit/B603): 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:
|
||||
|
||||
@@ -29,6 +29,9 @@ services:
|
||||
- /app/frontend/dist # Empty directory
|
||||
- ./romm_mock:/app/romm
|
||||
- ~/.vite-plugin-mkcert:/app/.vite-plugin-mkcert
|
||||
# Read-only, so the backend can report the checked-out branch (see
|
||||
# backend/utils/__init__.py get_git_branch) without git write access.
|
||||
- ./.git:/app/.git:ro
|
||||
depends_on:
|
||||
- romm-db-dev
|
||||
- romm-valkey-dev
|
||||
|
||||
1
frontend/src/__generated__/models/SystemDict.ts
generated
1
frontend/src/__generated__/models/SystemDict.ts
generated
@@ -4,6 +4,7 @@
|
||||
/* eslint-disable */
|
||||
export type SystemDict = {
|
||||
VERSION: string;
|
||||
GIT_BRANCH: (string | null);
|
||||
SHOW_SETUP_WIZARD: boolean;
|
||||
};
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ export type MetadataOption = {
|
||||
const defaultHeartbeat: Heartbeat = {
|
||||
SYSTEM: {
|
||||
VERSION: "0.0.0",
|
||||
GIT_BRANCH: null,
|
||||
SHOW_SETUP_WIZARD: false,
|
||||
},
|
||||
METADATA_SOURCES: {
|
||||
|
||||
@@ -4,15 +4,15 @@
|
||||
// panel instead of the legacy card.
|
||||
import { RDialog, RIcon, RImg } from "@v2/lib";
|
||||
import type { Emitter } from "mitt";
|
||||
import { inject, onBeforeUnmount, ref } from "vue";
|
||||
import { computed, 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<Events>>("emitter");
|
||||
const show = ref(false);
|
||||
|
||||
@@ -34,12 +34,12 @@ type Link = {
|
||||
href: string;
|
||||
};
|
||||
|
||||
const links: Link[] = [
|
||||
const links = computed<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",
|
||||
@@ -59,7 +59,7 @@ const links: Link[] = [
|
||||
value: "Discord",
|
||||
href: "https://discord.com/invite/P5HtHnhUDH",
|
||||
},
|
||||
];
|
||||
]);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
<script setup lang="ts">
|
||||
// VersionTag — renders the running RomM version from the heartbeat store.
|
||||
// Shared by AuthLayout (bottom-right, compact) and the About dialog
|
||||
// (linked to the GitHub release). Pass `link` to render as an anchor
|
||||
// to the release notes page.
|
||||
import { computed } from "vue";
|
||||
import storeHeartbeat from "@/stores/heartbeat";
|
||||
// Renders the running RomM version, shared by AuthLayout, DevicePairShell,
|
||||
// and PairShell. Pass `link` to render as an anchor to the release/branch page.
|
||||
import { useVersionDisplay } from "@/v2/composables/useVersionDisplay";
|
||||
|
||||
defineOptions({ inheritAttrs: false });
|
||||
|
||||
@@ -15,11 +12,7 @@ withDefaults(
|
||||
{ link: false },
|
||||
);
|
||||
|
||||
const heartbeatStore = storeHeartbeat();
|
||||
const version = computed(() => heartbeatStore.value.SYSTEM.VERSION);
|
||||
const releaseHref = computed(
|
||||
() => `https://github.com/rommapp/romm/releases/tag/${version.value}`,
|
||||
);
|
||||
const { version, href: releaseHref } = useVersionDisplay();
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
||||
70
frontend/src/v2/composables/useVersionDisplay/index.test.ts
Normal file
70
frontend/src/v2/composables/useVersionDisplay/index.test.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { reactive } from "vue";
|
||||
import { useVersionDisplay } from "./index";
|
||||
|
||||
function makeHeartbeatStore(system: {
|
||||
VERSION: string;
|
||||
GIT_BRANCH: string | null;
|
||||
}) {
|
||||
return reactive({ value: { SYSTEM: system } });
|
||||
}
|
||||
|
||||
let heartbeatStore = makeHeartbeatStore({ VERSION: "0.0.0", GIT_BRANCH: null });
|
||||
|
||||
vi.mock("@/stores/heartbeat", () => ({ default: () => heartbeatStore }));
|
||||
|
||||
describe("useVersionDisplay", () => {
|
||||
it("shows the release tag and its GitHub releases link for a tagged build", () => {
|
||||
heartbeatStore = makeHeartbeatStore({ VERSION: "4.5.0", GIT_BRANCH: null });
|
||||
const { version, href } = useVersionDisplay();
|
||||
expect(version.value).toBe("4.5.0");
|
||||
expect(href.value).toBe(
|
||||
"https://github.com/rommapp/romm/releases/tag/4.5.0",
|
||||
);
|
||||
});
|
||||
|
||||
it("falls back to the bare 'development' tag when no branch is known", () => {
|
||||
heartbeatStore = makeHeartbeatStore({
|
||||
VERSION: "development",
|
||||
GIT_BRANCH: null,
|
||||
});
|
||||
const { version, href } = useVersionDisplay();
|
||||
expect(version.value).toBe("development");
|
||||
expect(href.value).toBe(
|
||||
"https://github.com/rommapp/romm/releases/tag/development",
|
||||
);
|
||||
});
|
||||
|
||||
it("shows the branch and its GitHub tree link on a dev build, preserving '/' as a path separator", () => {
|
||||
heartbeatStore = makeHeartbeatStore({
|
||||
VERSION: "development",
|
||||
GIT_BRANCH: "feature/foo",
|
||||
});
|
||||
const { version, href } = useVersionDisplay();
|
||||
expect(version.value).toBe("feature/foo");
|
||||
expect(href.value).toBe("https://github.com/rommapp/romm/tree/feature/foo");
|
||||
});
|
||||
|
||||
it("ignores GIT_BRANCH on a tagged build even if the backend sent one", () => {
|
||||
heartbeatStore = makeHeartbeatStore({
|
||||
VERSION: "4.5.0",
|
||||
GIT_BRANCH: "feature/foo",
|
||||
});
|
||||
const { version, href } = useVersionDisplay();
|
||||
expect(version.value).toBe("4.5.0");
|
||||
expect(href.value).toBe(
|
||||
"https://github.com/rommapp/romm/releases/tag/4.5.0",
|
||||
);
|
||||
});
|
||||
|
||||
it("encodes URL-reserved characters within a branch segment", () => {
|
||||
heartbeatStore = makeHeartbeatStore({
|
||||
VERSION: "development",
|
||||
GIT_BRANCH: "fix/#123-crash",
|
||||
});
|
||||
const { href } = useVersionDisplay();
|
||||
expect(href.value).toBe(
|
||||
"https://github.com/rommapp/romm/tree/fix/%23123-crash",
|
||||
);
|
||||
});
|
||||
});
|
||||
31
frontend/src/v2/composables/useVersionDisplay/index.ts
Normal file
31
frontend/src/v2/composables/useVersionDisplay/index.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
// The version to display and where it should link. Shows the branch on a
|
||||
// dev build, when known, instead of the bare "development" placeholder.
|
||||
import { computed } from "vue";
|
||||
import storeHeartbeat from "@/stores/heartbeat";
|
||||
|
||||
export function useVersionDisplay() {
|
||||
const heartbeatStore = storeHeartbeat();
|
||||
|
||||
const branch = computed(() => {
|
||||
const { VERSION, GIT_BRANCH } = heartbeatStore.value.SYSTEM;
|
||||
return VERSION === "development" ? GIT_BRANCH : null;
|
||||
});
|
||||
|
||||
const version = computed(
|
||||
() => branch.value ?? heartbeatStore.value.SYSTEM.VERSION,
|
||||
);
|
||||
|
||||
const href = computed(() => {
|
||||
// Branch names commonly contain "/" (e.g. "feature/foo"), which GitHub
|
||||
// expects as literal path separators, so encode each segment on its own.
|
||||
const encodedBranch = branch.value
|
||||
?.split("/")
|
||||
.map(encodeURIComponent)
|
||||
.join("/");
|
||||
return encodedBranch
|
||||
? `https://github.com/rommapp/romm/tree/${encodedBranch}`
|
||||
: `https://github.com/rommapp/romm/releases/tag/${version.value}`;
|
||||
});
|
||||
|
||||
return { version, href };
|
||||
}
|
||||
Reference in New Issue
Block a user