summaryrefslogtreecommitdiff
path: root/lib/api/features.rb
blob: 217459164638068adbf9d6bf166c1793edcffd0e (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
module API
  class Features < Grape::API
    before { authenticated_as_admin! }

    helpers do
      def gate_value(params)
        case params[:value]
        when 'true'
          true
        when '0', 'false'
          false
        else
          params[:value].to_i
        end
      end

      def gate_target(params)
        if params[:feature_group]
          Feature.group(params[:feature_group])
        elsif params[:user]
          User.find_by_username(params[:user])
        else
          gate_value(params)
        end
      end
    end

    resource :features do
      desc 'Get a list of all features' do
        success Entities::Feature
      end
      get do
        features = Feature.all

        present features, with: Entities::Feature, current_user: current_user
      end

      desc 'Set the gate value for the given feature' do
        success Entities::Feature
      end
      params do
        requires :value, type: String, desc: '`true` or `false` to enable/disable, an integer for percentage of time'
        optional :feature_group, type: String, desc: 'A Feature group name'
        optional :user, type: String, desc: 'A GitLab username'
        mutually_exclusive :feature_group, :user
      end
      post ':name' do
        feature = Feature.get(params[:name])
        target = gate_target(params)
        value = gate_value(params)

        case value
        when true
          feature.enable(target)
        when false
          feature.disable(target)
        else
          feature.enable_percentage_of_time(value)
        end

        present feature, with: Entities::Feature, current_user: current_user
      end
    end
  end
end