29
29
# Minimum number of parts expected in branch-commit format (e.g., "main-abc1234")
30
30
MIN_BRANCH_COMMIT_PARTS = 2
31
31
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
+
32
38
33
39
def get_version_info () -> dict [str , str ]:
34
40
"""Get version information including display version and link.
@@ -41,37 +47,60 @@ def get_version_info() -> dict[str, str]:
41
47
"""
42
48
version = VERSION
43
49
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 )
44
56
45
- # Check if version contains PR number (e.g., "pr-123", "pull-456")
57
+ # Check if version contains dashes
46
58
if version != "unknown" and ("-" in version ):
47
59
parts = version .split ("-" )
48
60
if len (parts ) >= MIN_BRANCH_COMMIT_PARTS :
49
61
# Check if first part indicates a PR
50
62
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
52
64
try :
53
65
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 } "
55
74
except (ValueError , IndexError ):
56
75
# If PR number is invalid, fallback to main branch
76
+ display_version = version
57
77
version_link = f"{ repo_url } /tree/main"
58
- else :
78
+ elif _looks_like_commit_hash ( parts [ - 1 ]) :
59
79
# 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 ]
61
82
commit_hash = parts [- 1 ]
83
+ display_version = branch_name
62
84
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 } "
63
89
else :
64
90
# Fallback to main branch
91
+ display_version = version
65
92
version_link = f"{ repo_url } /tree/main"
66
93
elif version != "unknown" :
67
94
# This looks like a tag version
95
+ display_version = version
68
96
version_link = f"{ repo_url } /releases/tag/{ version } "
69
97
else :
70
98
# Unknown version, link to main branch
99
+ display_version = "unknown"
71
100
version_link = f"{ repo_url } /tree/main"
72
101
73
102
return {
74
- "version" : version ,
103
+ "version" : display_version ,
75
104
"version_link" : version_link ,
76
105
}
77
106
0 commit comments