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

181 lines
5.9 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! }
2023-01-13 00:05:48 +05:30
features_tags = %w[features]
2021-01-29 00:20:46 +05:30
feature_category :feature_flags
2022-07-16 23:28:13 +05:30
urgency :low
2021-01-29 00:20:46 +05:30
2022-11-25 23:54:43 +05:30
# TODO: remove these helpers with feature flag set_feature_flag_service
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
2021-12-11 22:18:48 +05:30
# 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-09-10 17:25:29 +05:30
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
2023-01-13 00:05:48 +05:30
desc 'List all features' do
detail 'Get a list of all persisted features, with its gate values.'
2017-09-10 17:25:29 +05:30
success Entities::Feature
2023-01-13 00:05:48 +05:30
is_array true
tags features_tags
2017-09-10 17:25:29 +05:30
end
get do
features = Feature.all
present features, with: Entities::Feature, current_user: current_user
end
2023-01-13 00:05:48 +05:30
desc 'List all feature definitions' do
detail 'Get a list of all feature definitions.'
2021-02-22 17:27:13 +05:30
success Entities::Feature::Definition
2023-01-13 00:05:48 +05:30
is_array true
tags features_tags
2021-02-22 17:27:13 +05:30
end
get :definitions do
definitions = ::Feature::Definition.definitions.values.map(&:to_h)
present definitions, with: Entities::Feature::Definition, current_user: current_user
end
2023-01-13 00:05:48 +05:30
desc 'Set or create a feature' do
detail "Set a feature's gate value. If a feature with the given name doesn't exist yet, it's created. " \
"The value can be a boolean, or an integer to indicate percentage of time."
2017-09-10 17:25:29 +05:30
success Entities::Feature
2023-01-13 00:05:48 +05:30
failure [
{ code: 400, message: 'Bad request' }
]
tags features_tags
2017-09-10 17:25:29 +05:30
end
params do
2023-01-13 00:05:48 +05:30
requires :value,
types: [String, Integer],
desc: '`true` or `false` to enable/disable, or an integer for percentage of time'
optional :key, type: String, desc: '`percentage_of_actors` or `percentage_of_time` (default)'
2017-09-10 17:25:29 +05:30
optional :feature_group, type: String, desc: 'A Feature group name'
2022-07-23 23:45:48 +05:30
optional :user, type: String, desc: 'A GitLab username or comma-separated multiple usernames'
2022-08-27 11:52:29 +05:30
optional :group,
type: String,
2023-01-13 00:05:48 +05:30
desc: "A GitLab group's path, for example `gitlab-org`, or comma-separated multiple group paths"
2022-08-27 11:52:29 +05:30
optional :namespace,
type: String,
2023-01-13 00:05:48 +05:30
desc: "A GitLab group or user namespace's path, for example `john-doe`, or comma-separated " \
"multiple namespace paths. Introduced in GitLab 15.0."
2022-08-27 11:52:29 +05:30
optional :project,
type: String,
2023-01-13 00:05:48 +05:30
desc: "A projects path, for example `gitlab-org/gitlab-foss`, or comma-separated multiple project paths"
optional :repository,
type: String,
desc: "A repository path, for example `gitlab-org/gitlab-test.git`, `gitlab-org/gitlab-test.wiki.git`, " \
"`snippets/21.git`, to name a few. Use comma to separate multiple repository paths"
optional :force, type: Boolean, desc: 'Skip feature flag validation checks, such as a YAML definition'
2020-05-24 23:13:21 +05:30
mutually_exclusive :key, :feature_group
mutually_exclusive :key, :user
mutually_exclusive :key, :group
2022-07-16 23:28:13 +05:30
mutually_exclusive :key, :namespace
2020-05-24 23:13:21 +05:30
mutually_exclusive :key, :project
2023-01-13 00:05:48 +05:30
mutually_exclusive :key, :repository
2017-09-10 17:25:29 +05:30
end
post ':name' do
2022-11-25 23:54:43 +05:30
if Feature.enabled?(:set_feature_flag_service)
flag_params = declared_params(include_missing: false)
response = ::Admin::SetFeatureFlagService
.new(feature_flag_name: params[:name], params: flag_params)
.execute
if response.success?
present response.payload[:feature_flag],
with: Entities::Feature, current_user: current_user
2017-09-10 17:25:29 +05:30
else
2022-11-25 23:54:43 +05:30
bad_request!(response.message)
2017-09-10 17:25:29 +05:30
end
else
2022-11-25 23:54:43 +05:30
validate_feature_flag_name!(params[:name]) unless params[:force]
targets = gate_targets(params)
value = gate_value(params)
key = gate_key(params)
case value
when true
if gate_specified?(params)
targets.each { |target| Feature.enable(params[:name], target) }
else
Feature.enable(params[:name])
end
when false
if gate_specified?(params)
targets.each { |target| Feature.disable(params[:name], target) }
else
Feature.disable(params[:name])
end
2020-05-24 23:13:21 +05:30
else
2022-11-25 23:54:43 +05:30
if key == :percentage_of_actors
Feature.enable_percentage_of_actors(params[:name], value)
else
Feature.enable_percentage_of_time(params[:name], value)
end
2020-05-24 23:13:21 +05:30
end
2017-09-10 17:25:29 +05:30
2022-11-25 23:54:43 +05:30
present Feature.get(params[:name]), # rubocop:disable Gitlab/AvoidFeatureGet
with: Entities::Feature, current_user: current_user
end
2022-07-23 23:45:48 +05:30
rescue Feature::Target::UnknowTargetError => e
bad_request!(e.message)
2017-09-10 17:25:29 +05:30
end
2018-05-09 12:01:36 +05:30
2023-01-13 00:05:48 +05:30
desc 'Delete a feature' do
detail "Removes a feature gate. Response is equal when the gate exists, or doesn't."
tags features_tags
end
2018-05-09 12:01:36 +05:30
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
2022-11-25 23:54:43 +05:30
# TODO: remove this helper with feature flag set_feature_flag_service
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
2021-06-08 01:23:25 +05:30
API::Features.prepend_mod_with('API::Features')