2018-09-29 18:34:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-31 17:06:01 -04:00
|
|
|
module API
|
|
|
|
class Features < Grape::API
|
|
|
|
before { authenticated_as_admin! }
|
|
|
|
|
2017-06-21 10:49:51 -04:00
|
|
|
helpers do
|
|
|
|
def gate_value(params)
|
|
|
|
case params[:value]
|
|
|
|
when 'true'
|
|
|
|
true
|
|
|
|
when '0', 'false'
|
|
|
|
false
|
|
|
|
else
|
|
|
|
params[:value].to_i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-05 15:55:27 -04:00
|
|
|
def gate_targets(params)
|
2019-01-07 05:07:14 -05:00
|
|
|
Feature::Target.new(params).targets
|
|
|
|
end
|
2017-07-05 15:55:27 -04:00
|
|
|
|
2019-01-07 05:07:14 -05:00
|
|
|
def gate_specified?(params)
|
|
|
|
Feature::Target.new(params).gate_specified?
|
2017-06-21 10:49:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-31 17:06:01 -04:00
|
|
|
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'
|
2017-06-28 13:29:56 -04:00
|
|
|
optional :feature_group, type: String, desc: 'A Feature group name'
|
2017-06-21 10:49:51 -04:00
|
|
|
optional :user, type: String, desc: 'A GitLab username'
|
2019-01-07 05:07:14 -05:00
|
|
|
optional :project, type: String, desc: 'A projects path, like gitlab-org/gitlab-ce'
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|
|
|
|
post ':name' do
|
|
|
|
feature = Feature.get(params[:name])
|
2017-07-05 15:55:27 -04:00
|
|
|
targets = gate_targets(params)
|
2017-06-21 10:49:51 -04:00
|
|
|
value = gate_value(params)
|
2017-05-31 17:06:01 -04:00
|
|
|
|
2017-06-21 10:49:51 -04:00
|
|
|
case value
|
|
|
|
when true
|
2019-01-07 05:07:14 -05:00
|
|
|
if gate_specified?(params)
|
2017-07-05 15:55:27 -04:00
|
|
|
targets.each { |target| feature.enable(target) }
|
|
|
|
else
|
|
|
|
feature.enable
|
|
|
|
end
|
2017-06-21 10:49:51 -04:00
|
|
|
when false
|
2019-01-07 05:07:14 -05:00
|
|
|
if gate_specified?(params)
|
2017-07-05 15:55:27 -04:00
|
|
|
targets.each { |target| feature.disable(target) }
|
|
|
|
else
|
|
|
|
feature.disable
|
|
|
|
end
|
2017-05-31 17:06:01 -04:00
|
|
|
else
|
2017-06-21 10:49:51 -04:00
|
|
|
feature.enable_percentage_of_time(value)
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
present feature, with: Entities::Feature, current_user: current_user
|
|
|
|
end
|
2018-04-03 07:35:26 -04:00
|
|
|
|
|
|
|
desc 'Remove the gate value for the given feature'
|
|
|
|
delete ':name' do
|
|
|
|
Feature.get(params[:name]).remove
|
|
|
|
|
|
|
|
status 204
|
|
|
|
end
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|