debian-mirror-gitlab/lib/api/features.rb

114 lines
3 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
module API
2021-01-03 14:25:43 +05:30
class Features < ::API::Base
2017-09-10 17:25:29 +05:30
before { authenticated_as_admin! }
2021-01-29 00:20:46 +05:30
feature_category :feature_flags
2017-09-10 17:25:29 +05:30
helpers do
def gate_value(params)
case params[:value]
when 'true'
true
when '0', 'false'
false
else
params[:value].to_i
end
end
2020-05-24 23:13:21 +05:30
def gate_key(params)
case params[:key]
when 'percentage_of_actors'
:percentage_of_actors
else
:percentage_of_time
end
end
2017-09-10 17:25:29 +05:30
def gate_targets(params)
2019-03-02 22:35:43 +05:30
Feature::Target.new(params).targets
end
2017-09-10 17:25:29 +05:30
2019-03-02 22:35:43 +05:30
def gate_specified?(params)
Feature::Target.new(params).gate_specified?
2017-09-10 17:25:29 +05:30
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'
2020-05-24 23:13:21 +05:30
optional :key, type: String, desc: '`percentage_of_actors` or the default `percentage_of_time`'
2017-09-10 17:25:29 +05:30
optional :feature_group, type: String, desc: 'A Feature group name'
optional :user, type: String, desc: 'A GitLab username'
2019-07-07 11:18:12 +05:30
optional :group, type: String, desc: "A GitLab group's path, such as 'gitlab-org'"
2019-03-02 22:35:43 +05:30
optional :project, type: String, desc: 'A projects path, like gitlab-org/gitlab-ce'
2020-05-24 23:13:21 +05:30
mutually_exclusive :key, :feature_group
mutually_exclusive :key, :user
mutually_exclusive :key, :group
mutually_exclusive :key, :project
2017-09-10 17:25:29 +05:30
end
post ':name' do
2021-01-29 00:20:46 +05:30
validate_feature_flag_name!(params[:name])
2020-06-23 00:09:42 +05:30
feature = Feature.get(params[:name]) # rubocop:disable Gitlab/AvoidFeatureGet
2017-09-10 17:25:29 +05:30
targets = gate_targets(params)
value = gate_value(params)
2020-05-24 23:13:21 +05:30
key = gate_key(params)
2017-09-10 17:25:29 +05:30
case value
when true
2019-03-02 22:35:43 +05:30
if gate_specified?(params)
2017-09-10 17:25:29 +05:30
targets.each { |target| feature.enable(target) }
else
feature.enable
end
when false
2019-03-02 22:35:43 +05:30
if gate_specified?(params)
2017-09-10 17:25:29 +05:30
targets.each { |target| feature.disable(target) }
else
feature.disable
end
else
2020-05-24 23:13:21 +05:30
if key == :percentage_of_actors
feature.enable_percentage_of_actors(value)
else
feature.enable_percentage_of_time(value)
end
2017-09-10 17:25:29 +05:30
end
present feature, with: Entities::Feature, current_user: current_user
end
2018-05-09 12:01:36 +05:30
desc 'Remove the gate value for the given feature'
delete ':name' do
2020-06-23 00:09:42 +05:30
Feature.remove(params[:name])
2018-05-09 12:01:36 +05:30
2020-03-13 15:44:24 +05:30
no_content!
2018-05-09 12:01:36 +05:30
end
2017-09-10 17:25:29 +05:30
end
2021-01-29 00:20:46 +05:30
helpers do
def validate_feature_flag_name!(name)
# no-op
end
end
2017-09-10 17:25:29 +05:30
end
end
2021-01-29 00:20:46 +05:30
API::Features.prepend_if_ee('EE::API::Features')