2020-09-19 11:09:58 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module FeatureFlags
|
|
|
|
class UpdateService < FeatureFlags::BaseService
|
2021-09-02 02:09:10 -04:00
|
|
|
AUDITABLE_STRATEGY_ATTRIBUTES_HUMAN_NAMES = {
|
|
|
|
'scopes' => 'environment scopes',
|
|
|
|
'parameters' => 'parameters'
|
2020-09-19 11:09:58 -04:00
|
|
|
}.freeze
|
|
|
|
|
|
|
|
def execute(feature_flag)
|
|
|
|
return error('Access Denied', 403) unless can_update?(feature_flag)
|
2020-12-07 16:10:08 -05:00
|
|
|
return error('Not Found', 404) unless valid_user_list_ids?(feature_flag, user_list_ids(params))
|
2020-09-19 11:09:58 -04:00
|
|
|
|
2021-07-23 08:09:05 -04:00
|
|
|
ApplicationRecord.transaction do
|
2020-09-19 11:09:58 -04:00
|
|
|
feature_flag.assign_attributes(params)
|
|
|
|
|
|
|
|
feature_flag.strategies.each do |strategy|
|
|
|
|
if strategy.name_changed? && strategy.name_was == ::Operations::FeatureFlags::Strategy::STRATEGY_GITLABUSERLIST
|
|
|
|
strategy.user_list = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
audit_event = audit_event(feature_flag)
|
|
|
|
|
2020-10-28 11:08:49 -04:00
|
|
|
if feature_flag.active_changed?
|
|
|
|
feature_flag.execute_hooks(current_user)
|
|
|
|
end
|
|
|
|
|
2020-09-19 11:09:58 -04:00
|
|
|
if feature_flag.save
|
|
|
|
save_audit_event(audit_event)
|
|
|
|
|
|
|
|
success(feature_flag: feature_flag)
|
|
|
|
else
|
|
|
|
error(feature_flag.errors.full_messages, :bad_request)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def audit_message(feature_flag)
|
|
|
|
changes = changed_attributes_messages(feature_flag)
|
2021-09-02 02:09:10 -04:00
|
|
|
changes += changed_strategies_messages(feature_flag)
|
2020-09-19 11:09:58 -04:00
|
|
|
|
|
|
|
return if changes.empty?
|
|
|
|
|
2021-07-01 17:08:38 -04:00
|
|
|
"Updated feature flag #{feature_flag.name}. " + changes.join(" ")
|
2020-09-19 11:09:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def changed_attributes_messages(feature_flag)
|
|
|
|
feature_flag.changes.slice(*AUDITABLE_ATTRIBUTES).map do |attribute_name, changes|
|
|
|
|
"Updated #{attribute_name} "\
|
2021-07-01 17:08:38 -04:00
|
|
|
"from \"#{changes.first}\" to "\
|
|
|
|
"\"#{changes.second}\"."
|
2020-09-19 11:09:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-02 02:09:10 -04:00
|
|
|
def changed_strategies_messages(feature_flag)
|
|
|
|
feature_flag.strategies.map do |strategy|
|
|
|
|
if strategy.new_record?
|
|
|
|
created_strategy_message(strategy)
|
|
|
|
elsif strategy.marked_for_destruction?
|
|
|
|
deleted_strategy_message(strategy)
|
2020-09-19 11:09:58 -04:00
|
|
|
else
|
2021-09-02 02:09:10 -04:00
|
|
|
updated_strategy_message(strategy)
|
2020-09-19 11:09:58 -04:00
|
|
|
end
|
2021-09-02 02:09:10 -04:00
|
|
|
end.compact # updated_strategy_message can return nil if nothing has been changed
|
2020-09-19 11:09:58 -04:00
|
|
|
end
|
|
|
|
|
2021-09-02 02:09:10 -04:00
|
|
|
def deleted_strategy_message(strategy)
|
|
|
|
scopes = strategy.scopes.map { |scope| scope.environment_scope }.join(', ')
|
|
|
|
"Deleted strategy #{strategy.name} with environment scopes #{scopes}."
|
2020-09-19 11:09:58 -04:00
|
|
|
end
|
|
|
|
|
2021-09-02 02:09:10 -04:00
|
|
|
def updated_strategy_message(strategy)
|
|
|
|
changes = strategy.changes.slice(*AUDITABLE_STRATEGY_ATTRIBUTES_HUMAN_NAMES.keys)
|
2020-09-19 11:09:58 -04:00
|
|
|
return if changes.empty?
|
|
|
|
|
2021-09-02 02:09:10 -04:00
|
|
|
message = "Updated strategy #{strategy.name} "
|
2020-09-19 11:09:58 -04:00
|
|
|
message += changes.map do |attribute_name, change|
|
2021-09-02 02:09:10 -04:00
|
|
|
name = AUDITABLE_STRATEGY_ATTRIBUTES_HUMAN_NAMES[attribute_name]
|
2021-07-01 17:08:38 -04:00
|
|
|
"#{name} from #{change.first} to #{change.second}"
|
2020-09-19 11:09:58 -04:00
|
|
|
end.join(' ')
|
|
|
|
|
|
|
|
message + '.'
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_update?(feature_flag)
|
|
|
|
Ability.allowed?(current_user, :update_feature_flag, feature_flag)
|
|
|
|
end
|
2020-12-07 16:10:08 -05:00
|
|
|
|
|
|
|
def user_list_ids(params)
|
|
|
|
params.fetch(:strategies_attributes, [])
|
|
|
|
.select { |s| s[:user_list_id].present? }
|
|
|
|
.map { |s| s[:user_list_id] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_user_list_ids?(feature_flag, user_list_ids)
|
|
|
|
user_list_ids.empty? || ::Operations::FeatureFlags::UserList.belongs_to?(feature_flag.project_id, user_list_ids)
|
|
|
|
end
|
2020-09-19 11:09:58 -04:00
|
|
|
end
|
|
|
|
end
|