summaryrefslogtreecommitdiff
path: root/app/services/feature_flags/update_service.rb
blob: c837e50b10428f050ce7a992f2922b63b2136a82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# frozen_string_literal: true

module FeatureFlags
  class UpdateService < FeatureFlags::BaseService
    AUDITABLE_SCOPE_ATTRIBUTES_HUMAN_NAMES = {
      'active' => 'active state',
      'environment_scope' => 'environment scope',
      'strategies' => 'strategies'
    }.freeze

    def execute(feature_flag)
      return error('Access Denied', 403) unless can_update?(feature_flag)

      ActiveRecord::Base.transaction do
        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)

        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)
      changes += changed_scopes_messages(feature_flag)

      return if changes.empty?

      "Updated feature flag <strong>#{feature_flag.name}</strong>. " + changes.join(" ")
    end

    def changed_attributes_messages(feature_flag)
      feature_flag.changes.slice(*AUDITABLE_ATTRIBUTES).map do |attribute_name, changes|
        "Updated #{attribute_name} "\
        "from <strong>\"#{changes.first}\"</strong> to "\
        "<strong>\"#{changes.second}\"</strong>."
      end
    end

    def changed_scopes_messages(feature_flag)
      feature_flag.scopes.map do |scope|
        if scope.new_record?
          created_scope_message(scope)
        elsif scope.marked_for_destruction?
          deleted_scope_message(scope)
        else
          updated_scope_message(scope)
        end
      end.compact # updated_scope_message can return nil if nothing has been changed
    end

    def deleted_scope_message(scope)
      "Deleted rule <strong>#{scope.environment_scope}</strong>."
    end

    def updated_scope_message(scope)
      changes = scope.changes.slice(*AUDITABLE_SCOPE_ATTRIBUTES_HUMAN_NAMES.keys)
      return if changes.empty?

      message = "Updated rule <strong>#{scope.environment_scope}</strong> "
      message += changes.map do |attribute_name, change|
        name = AUDITABLE_SCOPE_ATTRIBUTES_HUMAN_NAMES[attribute_name]
        "#{name} from <strong>#{change.first}</strong> to <strong>#{change.second}</strong>"
      end.join(' ')

      message + '.'
    end

    def can_update?(feature_flag)
      Ability.allowed?(current_user, :update_feature_flag, feature_flag)
    end
  end
end