summaryrefslogtreecommitdiff
path: root/lib/gitlab/feature_categories.rb
blob: 17586a94d7eb8f91360c7c019a7b2d02f7dde85d (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
# frozen_string_literal: true

module Gitlab
  class FeatureCategories
    FEATURE_CATEGORY_DEFAULT = 'unknown'

    attr_reader :categories

    def self.default
      @default ||= self.load_from_yaml
    end

    def self.load_from_yaml
      categories = YAML.load_file(Rails.root.join('config', 'feature_categories.yml'))

      new(categories)
    end

    def initialize(categories)
      @categories = categories.to_set
    end

    # If valid, returns a feature category from the given request.
    def from_request(request)
      category = request.headers["HTTP_X_GITLAB_FEATURE_CATEGORY"].presence

      return unless category && valid?(category)

      return unless ::Gitlab::RequestForgeryProtection.verified?(request.env)

      category
    end

    def get!(feature_category)
      return feature_category if valid?(feature_category)

      raise "Unknown feature category: #{feature_category}" if Gitlab.dev_or_test_env?

      FEATURE_CATEGORY_DEFAULT
    end

    def valid?(category)
      categories.include?(category.to_s)
    end
  end
end