1
1
import murmur from 'imurmurhash'
2
2
import {
3
3
CONTROL_VARIATION ,
4
+ EXPERIMENTS ,
4
5
ExperimentNames ,
5
6
TREATMENT_VARIATION ,
6
7
getActiveExperiments ,
@@ -12,6 +13,7 @@ let experimentsInitialized = false
12
13
export function shouldShowExperiment (
13
14
experimentKey : ExperimentNames | { key : ExperimentNames } ,
14
15
locale : string ,
16
+ version : string ,
15
17
isStaff : boolean ,
16
18
) {
17
19
// Accept either EXPERIMENTS.<experiment_key> or EXPERIMENTS.<experiment_key>.key
@@ -40,8 +42,10 @@ export function shouldShowExperiment(
40
42
return controlGroup === TREATMENT_VARIATION
41
43
// Otherwise use the regular logic to determine if the user is in the treatment group
42
44
} else if (
43
- experiment . limitToLanguages ?. length &&
44
- experiment . limitToLanguages . includes ( locale )
45
+ ( experiment . limitToLanguages ?. length
46
+ ? experiment . limitToLanguages . includes ( locale )
47
+ : true ) &&
48
+ ( experiment . limitToVersions ?. length ? experiment . limitToVersions . includes ( version ) : true )
45
49
) {
46
50
return (
47
51
getExperimentControlGroupFromSession (
@@ -98,8 +102,8 @@ export function getExperimentControlGroupFromSession(
98
102
return modHash < percentToGetExperiment ? TREATMENT_VARIATION : CONTROL_VARIATION
99
103
}
100
104
101
- export function getExperimentVariationForContext ( locale : string ) : string {
102
- const experiments = getActiveExperiments ( locale )
105
+ export function getExperimentVariationForContext ( locale : string , version : string ) : string {
106
+ const experiments = getActiveExperiments ( locale , version )
103
107
for ( const experiment of experiments ) {
104
108
if ( experiment . includeVariationInContext ) {
105
109
return getExperimentControlGroupFromSession (
@@ -113,11 +117,38 @@ export function getExperimentVariationForContext(locale: string): string {
113
117
return ''
114
118
}
115
119
116
- export function initializeExperiments ( locale : string ) {
120
+ export function initializeExperiments (
121
+ locale : string ,
122
+ currentVersion : string ,
123
+ allVersions : { [ key : string ] : { version : string } } ,
124
+ ) {
117
125
if ( experimentsInitialized ) return
118
126
experimentsInitialized = true
119
127
120
- const experiments = getActiveExperiments ( locale )
128
+ // Replace any occurrence of 'enterprise-server@latest' with the actual latest version
129
+ for ( const [ experimentKey , experiment ] of Object . entries ( EXPERIMENTS ) ) {
130
+ if ( experiment . limitToVersions ?. includes ( 'enterprise-server@latest' ) ) {
131
+ // Sort the versions in descending order so that the latest enterprise-server version is first
132
+ const latestEnterpriseServerVersion = Object . keys ( allVersions )
133
+ . filter ( ( version ) => version . startsWith ( 'enterprise-server@' ) )
134
+ . sort ( ( a , b ) => {
135
+ const aVersion = a . split ( '@' ) [ 1 ]
136
+ const bVersion = b . split ( '@' ) [ 1 ]
137
+ return Number ( bVersion ) - Number ( aVersion )
138
+ } ) [ 0 ]
139
+ if ( latestEnterpriseServerVersion ) {
140
+ EXPERIMENTS [ experimentKey as ExperimentNames ] . limitToVersions =
141
+ experiment . limitToVersions . map ( ( version ) =>
142
+ version . replace (
143
+ 'enterprise-server@latest' ,
144
+ allVersions [ latestEnterpriseServerVersion ] . version ,
145
+ ) ,
146
+ )
147
+ }
148
+ }
149
+ }
150
+
151
+ const experiments = getActiveExperiments ( locale , currentVersion )
121
152
122
153
if ( experiments . length && process . env . NODE_ENV === 'development' ) {
123
154
console . log (
0 commit comments