Skip to content

Commit a3c5bce

Browse files
committed
feat(web): update navbar version display
1 parent 8f83d07 commit a3c5bce

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

src/server/server_config.py

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
# Minimum number of parts expected in branch-commit format (e.g., "main-abc1234")
3030
MIN_BRANCH_COMMIT_PARTS = 2
3131

32+
# Minimum length for a git commit hash
33+
MIN_COMMIT_HASH_LENGTH = 6
34+
35+
# Minimum number of parts in PR format to include commit hash (pr-number-commit)
36+
MIN_PR_PARTS_WITH_COMMIT = 2
37+
3238

3339
def get_version_info() -> dict[str, str]:
3440
"""Get version information including display version and link.
@@ -41,37 +47,60 @@ def get_version_info() -> dict[str, str]:
4147
"""
4248
version = VERSION
4349
repo_url = REPOSITORY_URL.rstrip("/")
50+
display_version = version
51+
version_link = f"{repo_url}/tree/main" # Default fallback
52+
53+
def _looks_like_commit_hash(text: str) -> bool:
54+
"""Check if text looks like a git commit hash (alphanumeric, 6+ chars)."""
55+
return len(text) >= MIN_COMMIT_HASH_LENGTH and text.isalnum() and any(c.isalpha() for c in text)
4456

45-
# Check if version contains PR number (e.g., "pr-123", "pull-456")
57+
# Check if version contains dashes
4658
if version != "unknown" and ("-" in version):
4759
parts = version.split("-")
4860
if len(parts) >= MIN_BRANCH_COMMIT_PARTS:
4961
# Check if first part indicates a PR
5062
if parts[0].lower() in ("pr", "pull"):
51-
# Extract PR number from the second part
63+
# Extract PR number and commit hash from the parts
5264
try:
5365
pr_number = int(parts[1])
54-
version_link = f"{repo_url}/pull/{pr_number}"
66+
display_version = f"pr-{pr_number}"
67+
# If there's a commit hash after the PR number, link to the commit in the PR
68+
if len(parts) > MIN_PR_PARTS_WITH_COMMIT:
69+
commit_hash = parts[-1]
70+
version_link = f"{repo_url}/pull/{pr_number}/commits/{commit_hash}"
71+
else:
72+
# No commit hash, link to the PR page
73+
version_link = f"{repo_url}/pull/{pr_number}"
5574
except (ValueError, IndexError):
5675
# If PR number is invalid, fallback to main branch
76+
display_version = version
5777
version_link = f"{repo_url}/tree/main"
58-
else:
78+
elif _looks_like_commit_hash(parts[-1]):
5979
# This looks like branch-commit format (e.g., "main-abc1234")
60-
# Take the last part as commit hash
80+
# Display only the branch name, link to the commit
81+
branch_name = parts[0]
6182
commit_hash = parts[-1]
83+
display_version = branch_name
6284
version_link = f"{repo_url}/commit/{commit_hash}"
85+
else:
86+
# This looks like a tag version with dashes (e.g., "release-2.1.0")
87+
display_version = version
88+
version_link = f"{repo_url}/releases/tag/{version}"
6389
else:
6490
# Fallback to main branch
91+
display_version = version
6592
version_link = f"{repo_url}/tree/main"
6693
elif version != "unknown":
6794
# This looks like a tag version
95+
display_version = version
6896
version_link = f"{repo_url}/releases/tag/{version}"
6997
else:
7098
# Unknown version, link to main branch
99+
display_version = "unknown"
71100
version_link = f"{repo_url}/tree/main"
72101

73102
return {
74-
"version": version,
103+
"version": display_version,
75104
"version_link": version_link,
76105
}
77106

0 commit comments

Comments
 (0)