Welcome to mirror list, hosted at ThFree Co, Russian Federation.

feature_flags.rb « yaml_processor « ci « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 50d37f6e4a009a40f466e22f59de38b6203da2df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# frozen_string_literal: true

module Gitlab
  module Ci
    class YamlProcessor
      module FeatureFlags
        ACTOR_KEY = 'ci_yaml_processor_feature_flag_actor'
        CORRECT_USAGE_KEY = 'ci_yaml_processor_feature_flag_correct_usage'
        NO_ACTOR_VALUE = :no_actor
        NO_ACTOR_MESSAGE = "Actor not set. Ensure to call `enabled?` inside `with_actor` block"
        NoActorError = Class.new(StandardError)

        class << self
          # Cache a feature flag actor as thread local variable so
          # we can have it available later with #enabled?
          def with_actor(actor)
            previous = Thread.current[ACTOR_KEY]

            # When actor is `nil` the method `Thread.current[]=` does not
            # create the ACTOR_KEY. Instead, we want to still save an explicit
            # value to know that we are within the `with_actor` block.
            Thread.current[ACTOR_KEY] = actor || NO_ACTOR_VALUE

            yield
          ensure
            Thread.current[ACTOR_KEY] = previous
          end

          # Use this to check if a feature flag is enabled
          def enabled?(feature_flag)
            ::Feature.enabled?(feature_flag, current_actor)
          end

          def ensure_correct_usage
            previous = Thread.current[CORRECT_USAGE_KEY]
            Thread.current[CORRECT_USAGE_KEY] = true

            yield
          ensure
            Thread.current[CORRECT_USAGE_KEY] = previous
          end

          private

          def current_actor
            value = Thread.current[ACTOR_KEY] || (raise NoActorError, NO_ACTOR_MESSAGE)
            return if value == NO_ACTOR_VALUE

            value
          rescue NoActorError => e
            handle_missing_actor(e)

            nil
          end

          def handle_missing_actor(exception)
            if ensure_correct_usage?
              Gitlab::ErrorTracking.track_and_raise_for_dev_exception(exception)
            else
              Gitlab::ErrorTracking.track_exception(exception)
            end
          end

          def ensure_correct_usage?
            Thread.current[CORRECT_USAGE_KEY] == true
          end
        end
      end
    end
  end
end