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

gitaly.rb « feature « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fd798862fa8c4ff2ac42f3b1fd7d25957af55008 (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
# frozen_string_literal: true

module Feature
  class Gitaly
    PREFIX = "gitaly_"

    # Wrapper for feature flag actor to avoid unnecessarily SQL queries
    class ActorWrapper
      def initialize(klass, id)
        @klass = klass
        @id = id
      end

      def flipper_id
        "#{@klass.name}:#{@id}"
      end
    end

    class << self
      def enabled_for_any?(feature_flag, *actors)
        return false unless Feature::FlipperFeature.table_exists?

        actors = actors.compact
        return Feature.enabled?(feature_flag, type: :undefined, default_enabled_if_undefined: false) if actors.empty?

        actors.any? do |actor|
          Feature.enabled?(feature_flag, actor, type: :undefined, default_enabled_if_undefined: false)
        end
      rescue ActiveRecord::NoDatabaseError, PG::ConnectionBad
        false
      end

      def server_feature_flags(repository: nil, user: nil, project: nil, group: nil)
        # We need to check that both the DB connection and table exists
        return {} unless FlipperFeature.database.cached_table_exists?

        # The order of actors here is significant. Percentage-based actor selection may not work as expected if this
        # order changes. We want repository actor to take highest precedence.
        actors = [repository, user, project, group].compact

        Feature.persisted_names
          .select { |f| f.start_with?(PREFIX) }
          .to_h do |f|
            ["gitaly-feature-#{f.delete_prefix(PREFIX).tr('_', '-')}", enabled_for_any?(f, *actors).to_s]
          end
      end

      def user_actor(user = nil)
        return ::Feature::Gitaly::ActorWrapper.new(::User, user.id) if user.is_a?(::User)

        user_id = Gitlab::ApplicationContext.current_context_attribute(:user_id)
        ::Feature::Gitaly::ActorWrapper.new(::User, user_id) if user_id
      end

      def project_actor(container)
        ::Feature::Gitaly::ActorWrapper.new(::Project, container.id) if container.is_a?(::Project)
      end

      def group_actor(container)
        ::Feature::Gitaly::ActorWrapper.new(::Group, container.namespace_id) if container.is_a?(::Project)
      end
    end
  end
end