@@ -3,22 +3,89 @@ declare global {
33 interface Window {
44 __DISCOURSE_GRAPH_VERSION__ ?: string ;
55 __DISCOURSE_GRAPH_BUILD_DATE__ ?: string ;
6+ __DISCOURSE_GRAPH_BUILD_COMMIT__ ?: string ;
7+ __DISCOURSE_GRAPH_BUILD_BRANCH__ ?: string ;
68 }
79}
810
9- export const getVersionWithDate = ( ) : {
11+ export type VersionMetadata = {
1012 version : string ;
1113 buildDate : string ;
12- } => {
14+ buildCommit : string ;
15+ buildBranch : string ;
16+ versionStamp : string ;
17+ } ;
18+
19+ const SHORT_COMMIT_LENGTH = 8 ;
20+ const FALLBACK_VALUE = "-" ;
21+
22+ const hasMetadataValue = ( value : string | undefined ) : value is string =>
23+ Boolean ( value ?. trim ( ) && value . trim ( ) !== FALLBACK_VALUE ) ;
24+
25+ export const normalizeBuildBranch = (
26+ buildBranch : string | undefined ,
27+ ) : string | undefined => {
28+ if ( ! hasMetadataValue ( buildBranch ) ) return undefined ;
29+
30+ const branch = buildBranch . trim ( ) ;
31+ if (
32+ ! branch ||
33+ branch === "main" ||
34+ branch === "master" ||
35+ branch === "HEAD"
36+ ) {
37+ return undefined ;
38+ }
39+
40+ const normalized = branch
41+ . replace ( / [ ^ a - z A - Z 0 - 9 . _ - ] + / g, "-" )
42+ . replace ( / ^ - + | - + $ / g, "" ) ;
43+ return normalized || undefined ;
44+ } ;
45+
46+ const getShortCommit = (
47+ buildCommit : string | undefined ,
48+ ) : string | undefined => {
49+ if ( ! hasMetadataValue ( buildCommit ) ) return undefined ;
50+ return buildCommit . trim ( ) . slice ( 0 , SHORT_COMMIT_LENGTH ) ;
51+ } ;
52+
53+ export const createVersionStamp = ( {
54+ version,
55+ buildDate,
56+ buildCommit,
57+ buildBranch,
58+ } : Omit < VersionMetadata , "versionStamp" > ) : string => {
59+ const parts = [
60+ version || FALLBACK_VALUE ,
61+ buildDate || FALLBACK_VALUE ,
62+ normalizeBuildBranch ( buildBranch ) ,
63+ getShortCommit ( buildCommit ) ,
64+ ] . filter ( hasMetadataValue ) ;
65+
66+ return parts . length > 0 ? parts . join ( "-" ) : FALLBACK_VALUE ;
67+ } ;
68+
69+ export const getVersionWithDate = ( ) : VersionMetadata => {
1370 if ( typeof window === "undefined" ) {
1471 return {
15- version : "-" ,
16- buildDate : "-" ,
72+ version : FALLBACK_VALUE ,
73+ buildDate : FALLBACK_VALUE ,
74+ buildCommit : FALLBACK_VALUE ,
75+ buildBranch : FALLBACK_VALUE ,
76+ versionStamp : FALLBACK_VALUE ,
1777 } ;
1878 }
1979
80+ const metadata = {
81+ version : window . __DISCOURSE_GRAPH_VERSION__ || FALLBACK_VALUE ,
82+ buildDate : window . __DISCOURSE_GRAPH_BUILD_DATE__ || FALLBACK_VALUE ,
83+ buildCommit : window . __DISCOURSE_GRAPH_BUILD_COMMIT__ || FALLBACK_VALUE ,
84+ buildBranch : window . __DISCOURSE_GRAPH_BUILD_BRANCH__ || FALLBACK_VALUE ,
85+ } ;
86+
2087 return {
21- version : window . __DISCOURSE_GRAPH_VERSION__ || "-" ,
22- buildDate : window . __DISCOURSE_GRAPH_BUILD_DATE__ || "-" ,
88+ ... metadata ,
89+ versionStamp : createVersionStamp ( metadata ) ,
2390 } ;
2491} ;
0 commit comments