gitlab-experiment 1.2.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -44,6 +44,15 @@ module Gitlab
44
44
  "#{experiment.name}_id"
45
45
  end
46
46
 
47
+ # Allow forced variant assignment via query parameter and cookie.
48
+ #
49
+ # When enabled, experiments will check for a `glex_force` query
50
+ # parameter and a `_glex_force` cookie to override variant assignment.
51
+ # This is intended for QA/UAT testing in staging and production.
52
+ #
53
+ # Defaults to false (disabled).
54
+ @allow_forced_assignment = false
55
+
47
56
  # Mark experiment cookies as secure (HTTPS only).
48
57
  #
49
58
  # When set to true, cookies will have the secure flag set, meaning they
@@ -187,6 +196,7 @@ module Gitlab
187
196
  :cookie_domain,
188
197
  :cookie_name,
189
198
  :secure_cookie,
199
+ :allow_forced_assignment,
190
200
  :context_key_secret,
191
201
  :context_key_bit_length,
192
202
  :mount_at,
@@ -7,10 +7,19 @@ module Gitlab
7
7
  klass.include(self).tap { |base| base.helper_method(:experiment) if with_helper }
8
8
  end
9
9
 
10
+ # An explicit `request:` keyword argument always takes precedence; when
11
+ # present, no auto-detection runs.
12
+ #
13
+ # Otherwise, `request` is auto-detected through implicit_experiment_request in this order:
14
+ # 1. A `request` method on `self` (the controller pattern).
15
+ # 2. A `@request` instance variable on `self` (the service pattern).
16
+ #
17
+ # Callers with `request` as a pure local variable should pass
18
+ # `request: request` explicitly.
10
19
  def experiment(name, variant_name = nil, **context, &block)
11
20
  raise ArgumentError, 'name is required' if name.nil?
12
21
 
13
- context[:request] ||= request if respond_to?(:request)
22
+ context[:request] ||= implicit_experiment_request
14
23
 
15
24
  base = Configuration.base_class.constantize
16
25
  klass = base.constantize(name) || base
@@ -20,6 +29,16 @@ module Gitlab
20
29
 
21
30
  instance.context.frozen? ? instance.run : instance.tap(&:run)
22
31
  end
32
+
33
+ private
34
+
35
+ def implicit_experiment_request
36
+ if respond_to?(:request)
37
+ request
38
+ elsif instance_variable_defined?(:@request)
39
+ instance_variable_get(:@request)
40
+ end
41
+ end
23
42
  end
24
43
  end
25
44
  end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gitlab
4
+ class Experiment
5
+ module ForceAssignment
6
+ PARAM_NAME = 'glex_force'
7
+
8
+ private
9
+
10
+ def forced_variant_name
11
+ return unless Configuration.allow_forced_assignment
12
+ return unless enabled?
13
+
14
+ param = context&.request&.params.try(:[], PARAM_NAME)
15
+ return if param.blank?
16
+
17
+ experiment_name, variant_name = param.split(':', 2)
18
+ return if experiment_name.blank? || variant_name.blank?
19
+ return unless experiment_name == name
20
+
21
+ variant_sym = variant_name.to_sym
22
+ return unless behaviors.key?(variant_sym)
23
+
24
+ variant_sym
25
+ end
26
+ end
27
+ end
28
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Gitlab
4
4
  class Experiment
5
- VERSION = '1.2.0'
5
+ VERSION = '1.4.0'
6
6
  end
7
7
  end
@@ -13,6 +13,7 @@ require 'gitlab/experiment/callbacks'
13
13
  require 'gitlab/experiment/rollout'
14
14
  require 'gitlab/experiment/configuration'
15
15
  require 'gitlab/experiment/cookies'
16
+ require 'gitlab/experiment/force_assignment'
16
17
  require 'gitlab/experiment/context'
17
18
  require 'gitlab/experiment/dsl'
18
19
  require 'gitlab/experiment/middleware'
@@ -26,6 +27,7 @@ module Gitlab
26
27
  include BaseInterface
27
28
  include Cache
28
29
  include Callbacks
30
+ include ForceAssignment
29
31
  include Nestable
30
32
 
31
33
  class << self
@@ -107,6 +109,8 @@ module Gitlab
107
109
  end
108
110
 
109
111
  def assigned(value = nil)
112
+ # Skip force assignment if a variant was already set (e.g., via constructor or explicit #assigned call).
113
+ value ||= forced_variant_name unless @_assigned_variant_name
110
114
  @_assigned_variant_name = cache_variant(value) if value.present?
111
115
  return Variant.new(name: @_assigned_variant_name || :unresolved) if @_assigned_variant_name || @_resolving_variant
112
116
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gitlab-experiment
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - GitLab
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-12-16 00:00:00.000000000 Z
11
+ date: 2026-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -223,6 +223,7 @@ files:
223
223
  - lib/gitlab/experiment/dsl.rb
224
224
  - lib/gitlab/experiment/engine.rb
225
225
  - lib/gitlab/experiment/errors.rb
226
+ - lib/gitlab/experiment/force_assignment.rb
226
227
  - lib/gitlab/experiment/middleware.rb
227
228
  - lib/gitlab/experiment/nestable.rb
228
229
  - lib/gitlab/experiment/rollout.rb