2018-09-29 18:34:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-31 17:06:01 -04:00
|
|
|
module API
|
2020-10-14 20:08:42 -04:00
|
|
|
class Features < ::API::Base
|
2017-05-31 17:06:01 -04:00
|
|
|
before { authenticated_as_admin! }
|
|
|
|
|
2020-10-29 08:08:50 -04:00
|
|
|
feature_category :feature_flags
|
|
|
|
|
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
|
2021-11-08 22:42:22 -05:00
|
|
|
# https://github.com/jnunemaker/flipper/blob/master/lib/flipper/typecast.rb#L47
|
|
|
|
if params[:value].to_s.include?('.')
|
|
|
|
params[:value].to_f
|
|
|
|
else
|
|
|
|
params[:value].to_i
|
|
|
|
end
|
2017-06-21 10:49:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-04 14:10:20 -04:00
|
|
|
def gate_key(params)
|
|
|
|
case params[:key]
|
|
|
|
when 'percentage_of_actors'
|
|
|
|
:percentage_of_actors
|
|
|
|
else
|
|
|
|
:percentage_of_time
|
|
|
|
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
|
|
|
|
|
2020-11-18 22:08:59 -05:00
|
|
|
desc 'Get a list of all feature definitions' do
|
|
|
|
success Entities::Feature::Definition
|
|
|
|
end
|
|
|
|
get :definitions do
|
|
|
|
definitions = ::Feature::Definition.definitions.values.map(&:to_h)
|
|
|
|
|
|
|
|
present definitions, with: Entities::Feature::Definition, current_user: current_user
|
|
|
|
end
|
|
|
|
|
2017-05-31 17:06:01 -04:00
|
|
|
desc 'Set the gate value for the given feature' do
|
|
|
|
success Entities::Feature
|
|
|
|
end
|
|
|
|
params do
|
2021-11-08 22:42:22 -05:00
|
|
|
requires :value, type: String, desc: '`true` or `false` to enable/disable, a float for percentage of time'
|
2020-05-04 14:10:20 -04:00
|
|
|
optional :key, type: String, desc: '`percentage_of_actors` or the default `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-02-07 15:27:03 -05:00
|
|
|
optional :group, type: String, desc: "A GitLab group's path, such as 'gitlab-org'"
|
2019-01-07 05:07:14 -05:00
|
|
|
optional :project, type: String, desc: 'A projects path, like gitlab-org/gitlab-ce'
|
2020-11-18 22:08:59 -05:00
|
|
|
optional :force, type: Boolean, desc: 'Skip feature flag validation checks, ie. YAML definition'
|
2020-05-04 14:10:20 -04:00
|
|
|
|
|
|
|
mutually_exclusive :key, :feature_group
|
|
|
|
mutually_exclusive :key, :user
|
|
|
|
mutually_exclusive :key, :group
|
|
|
|
mutually_exclusive :key, :project
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|
|
|
|
post ':name' do
|
2020-11-18 22:08:59 -05:00
|
|
|
validate_feature_flag_name!(params[:name]) unless params[:force]
|
2020-10-28 08:08:40 -04:00
|
|
|
|
2017-07-05 15:55:27 -04:00
|
|
|
targets = gate_targets(params)
|
2017-06-21 10:49:51 -04:00
|
|
|
value = gate_value(params)
|
2020-05-04 14:10:20 -04:00
|
|
|
key = gate_key(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)
|
2020-12-16 10:10:18 -05:00
|
|
|
targets.each { |target| Feature.enable(params[:name], target) }
|
2017-07-05 15:55:27 -04:00
|
|
|
else
|
2020-12-16 10:10:18 -05:00
|
|
|
Feature.enable(params[:name])
|
2017-07-05 15:55:27 -04:00
|
|
|
end
|
2017-06-21 10:49:51 -04:00
|
|
|
when false
|
2019-01-07 05:07:14 -05:00
|
|
|
if gate_specified?(params)
|
2020-12-16 10:10:18 -05:00
|
|
|
targets.each { |target| Feature.disable(params[:name], target) }
|
2017-07-05 15:55:27 -04:00
|
|
|
else
|
2020-12-16 10:10:18 -05:00
|
|
|
Feature.disable(params[:name])
|
2017-07-05 15:55:27 -04:00
|
|
|
end
|
2017-05-31 17:06:01 -04:00
|
|
|
else
|
2020-05-04 14:10:20 -04:00
|
|
|
if key == :percentage_of_actors
|
2020-12-16 10:10:18 -05:00
|
|
|
Feature.enable_percentage_of_actors(params[:name], value)
|
2020-05-04 14:10:20 -04:00
|
|
|
else
|
2020-12-16 10:10:18 -05:00
|
|
|
Feature.enable_percentage_of_time(params[:name], value)
|
2020-05-04 14:10:20 -04:00
|
|
|
end
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|
|
|
|
|
2020-12-16 10:10:18 -05:00
|
|
|
present Feature.get(params[:name]), # rubocop:disable Gitlab/AvoidFeatureGet
|
|
|
|
with: Entities::Feature, current_user: current_user
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|
2018-04-03 07:35:26 -04:00
|
|
|
|
|
|
|
desc 'Remove the gate value for the given feature'
|
|
|
|
delete ':name' do
|
2020-06-02 08:08:33 -04:00
|
|
|
Feature.remove(params[:name])
|
2018-04-03 07:35:26 -04:00
|
|
|
|
2020-01-16 13:08:46 -05:00
|
|
|
no_content!
|
2018-04-03 07:35:26 -04:00
|
|
|
end
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|
2020-10-28 08:08:40 -04:00
|
|
|
|
|
|
|
helpers do
|
|
|
|
def validate_feature_flag_name!(name)
|
|
|
|
# no-op
|
|
|
|
end
|
|
|
|
end
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|
|
|
|
end
|
2020-10-28 08:08:40 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
API::Features.prepend_mod_with('API::Features')
|