debian-mirror-gitlab/app/services/feature_flags/update_service.rb

125 lines
4 KiB
Ruby
Raw Normal View History

2021-01-03 14:25:43 +05:30
# frozen_string_literal: true
module FeatureFlags
class UpdateService < FeatureFlags::BaseService
2021-11-11 11:23:49 +05:30
AUDITABLE_STRATEGY_ATTRIBUTES_HUMAN_NAMES = {
'scopes' => 'environment scopes',
'parameters' => 'parameters'
2021-01-03 14:25:43 +05:30
}.freeze
2021-11-18 22:05:49 +05:30
def success(**args)
execute_hooks_after_commit(args[:feature_flag])
super
end
2021-01-03 14:25:43 +05:30
def execute(feature_flag)
return error('Access Denied', 403) unless can_update?(feature_flag)
return error('Not Found', 404) unless valid_user_list_ids?(feature_flag, user_list_ids(params))
2021-10-27 15:23:28 +05:30
ApplicationRecord.transaction do
2021-01-03 14:25:43 +05:30
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
2023-05-27 22:25:52 +05:30
# We generate the audit context before the feature flag is saved as #changed_strategies_messages depends on the strategies' states before save
saved_audit_context = audit_context feature_flag
2021-01-03 14:25:43 +05:30
if feature_flag.save
2022-08-13 15:12:31 +05:30
update_last_feature_flag_updated_at!
2023-05-27 22:25:52 +05:30
success(feature_flag: feature_flag, audit_context: saved_audit_context)
2021-01-03 14:25:43 +05:30
else
error(feature_flag.errors.full_messages, :bad_request)
end
end
end
private
2021-11-18 22:05:49 +05:30
def execute_hooks_after_commit(feature_flag)
return unless feature_flag.active_previously_changed?
# The `current_user` method (defined in `BaseService`) is not available within the `run_after_commit` block
user = current_user
feature_flag.run_after_commit do
HookService.new(feature_flag, user).execute
end
end
2023-05-27 22:25:52 +05:30
def audit_context(feature_flag)
{
name: 'feature_flag_updated',
message: audit_message(feature_flag),
author: current_user,
scope: feature_flag.project,
target: feature_flag
}
end
2021-01-03 14:25:43 +05:30
def audit_message(feature_flag)
changes = changed_attributes_messages(feature_flag)
2021-11-11 11:23:49 +05:30
changes += changed_strategies_messages(feature_flag)
2021-01-03 14:25:43 +05:30
return if changes.empty?
2021-07-02 01:05:55 +05:30
"Updated feature flag #{feature_flag.name}. " + changes.join(" ")
2021-01-03 14:25:43 +05:30
end
def changed_attributes_messages(feature_flag)
feature_flag.changes.slice(*AUDITABLE_ATTRIBUTES).map do |attribute_name, changes|
"Updated #{attribute_name} "\
2021-07-02 01:05:55 +05:30
"from \"#{changes.first}\" to "\
"\"#{changes.second}\"."
2021-01-03 14:25:43 +05:30
end
end
2021-11-11 11:23:49 +05:30
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)
2021-01-03 14:25:43 +05:30
else
2021-11-11 11:23:49 +05:30
updated_strategy_message(strategy)
2021-01-03 14:25:43 +05:30
end
2021-11-11 11:23:49 +05:30
end.compact # updated_strategy_message can return nil if nothing has been changed
2021-01-03 14:25:43 +05:30
end
2021-11-11 11:23:49 +05:30
def deleted_strategy_message(strategy)
scopes = strategy.scopes.map { |scope| scope.environment_scope }.join(', ')
"Deleted strategy #{strategy.name} with environment scopes #{scopes}."
2021-01-03 14:25:43 +05:30
end
2021-11-11 11:23:49 +05:30
def updated_strategy_message(strategy)
changes = strategy.changes.slice(*AUDITABLE_STRATEGY_ATTRIBUTES_HUMAN_NAMES.keys)
2021-01-03 14:25:43 +05:30
return if changes.empty?
2021-11-11 11:23:49 +05:30
message = "Updated strategy #{strategy.name} "
2021-01-03 14:25:43 +05:30
message += changes.map do |attribute_name, change|
2021-11-11 11:23:49 +05:30
name = AUDITABLE_STRATEGY_ATTRIBUTES_HUMAN_NAMES[attribute_name]
2021-07-02 01:05:55 +05:30
"#{name} from #{change.first} to #{change.second}"
2021-01-03 14:25:43 +05:30
end.join(' ')
message + '.'
end
def can_update?(feature_flag)
Ability.allowed?(current_user, :update_feature_flag, feature_flag)
end
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
end
end