summaryrefslogtreecommitdiff
path: root/lib/api/features.rb
diff options
context:
space:
mode:
authorRémy Coutable <remy@rymai.me>2017-06-21 16:49:51 +0200
committerRémy Coutable <remy@rymai.me>2017-06-27 18:59:51 +0200
commitb4d325c80c63ee9ee2676a57a42fac472b5b20d5 (patch)
tree15e4dea85ab1ae5538ffe35d0c9b0ee2dbfcaadc /lib/api/features.rb
parentcc50decab5b22628eafb6636b3e57f99094c7926 (diff)
downloadgitlab-ce-b4d325c80c63ee9ee2676a57a42fac472b5b20d5.tar.gz
Allow the feature flags to be enabled/disabled with more granularity
This allows to enable/disable a feature flag for a given user, or a given Flipper group (must be declared statically in the `flipper.rb` initializer beforehand). Signed-off-by: Rémy Coutable <remy@rymai.me>
Diffstat (limited to 'lib/api/features.rb')
-rw-r--r--lib/api/features.rb38
1 files changed, 33 insertions, 5 deletions
diff --git a/lib/api/features.rb b/lib/api/features.rb
index cff0ba2ddff..4e10e36fce9 100644
--- a/lib/api/features.rb
+++ b/lib/api/features.rb
@@ -2,6 +2,29 @@ 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[:flipper_group]
+ Feature.group(params[:flipper_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
@@ -17,16 +40,21 @@ module API
end
params do
requires :value, type: String, desc: '`true` or `false` to enable/disable, an integer for percentage of time'
+ optional :flipper_group, type: String, desc: 'A Flipper group name'
+ optional :user, type: String, desc: 'A GitLab username'
end
post ':name' do
feature = Feature.get(params[:name])
+ target = gate_target(params)
+ value = gate_value(params)
- if %w(0 false).include?(params[:value])
- feature.disable
- elsif params[:value] == 'true'
- feature.enable
+ case value
+ when true
+ feature.enable(target)
+ when false
+ feature.disable(target)
else
- feature.enable_percentage_of_time(params[:value].to_i)
+ feature.enable_percentage_of_time(value)
end
present feature, with: Entities::Feature, current_user: current_user