Class: Gitlab::WorkItems::Instrumentation::EventMappings

Inherits:
Object
  • Object
show all
Includes:
EventActions
Defined in:
lib/gitlab/work_items/instrumentation/event_mappings.rb

Constant Summary collapse

ATTRIBUTE_MAPPINGS =
[
  { event: EventActions::TITLE_UPDATE, key: 'title' },
  { event: EventActions::DESCRIPTION_UPDATE, key: 'description' },
  { event: EventActions::MILESTONE_UPDATE, key: 'milestone_id' },
  { event: EventActions::WEIGHT_UPDATE, key: 'weight' },
  { event: EventActions::ITERATION_UPDATE, key: 'sprint_id' },
  { event: EventActions::HEALTH_STATUS_UPDATE, key: 'health_status' },
  { event: EventActions::START_DATE_UPDATE, key: 'start_date' },
  { event: EventActions::DUE_DATE_UPDATE, key: 'due_date' },
  { event: EventActions::TIME_ESTIMATE_UPDATE, key: 'time_estimate' },
  {
    event: ->(change) {
      _old_value, new_value = change
      new_value ? EventActions::LOCK : EventActions::UNLOCK
    },
    key: 'discussion_locked'
  }
].freeze
ASSOCIATION_MAPPINGS =
[
  {
    event: EventActions::ASSIGNEES_UPDATE,
    key: :assignees,
    compare: ->(old, new) { old != new }
  },
  {
    event: EventActions::CONFIDENTIALITY_ENABLE,
    key: :confidential,
    compare: ->(old_value, new_value) { old_value == false && new_value == true }
  },
  {
    event: EventActions::CONFIDENTIALITY_DISABLE,
    key: :confidential,
    compare: ->(old_value, new_value) { old_value == true && new_value == false }
  },
  {
    event: EventActions::LABELS_UPDATE,
    key: :labels,
    compare: ->(old, new) { old != new }
  },
  {
    event: EventActions::TIME_SPENT_UPDATE,
    key: :total_time_spent,
    compare: ->(old, new) { old != new }
  },
  {
    event: EventActions::MARKED_AS_DUPLICATE,
    key: :status,
    compare: ->(old, new) {
      old_name = old&.name
      new_name = new&.name
      old_name != 'Duplicate' && new_name == 'Duplicate'
    },
    accessor: ->(work_item) { work_item.current_status&.status }
  }
].freeze

Constants included from EventActions

Gitlab::WorkItems::Instrumentation::EventActions::ALL_EVENTS, Gitlab::WorkItems::Instrumentation::EventActions::ASSIGNEES_UPDATE, Gitlab::WorkItems::Instrumentation::EventActions::BLOCKED_BY_ITEM_ADD, Gitlab::WorkItems::Instrumentation::EventActions::BLOCKED_BY_ITEM_REMOVE, Gitlab::WorkItems::Instrumentation::EventActions::BLOCKING_ITEM_ADD, Gitlab::WorkItems::Instrumentation::EventActions::BLOCKING_ITEM_REMOVE, Gitlab::WorkItems::Instrumentation::EventActions::CLONE, Gitlab::WorkItems::Instrumentation::EventActions::CLOSE, Gitlab::WorkItems::Instrumentation::EventActions::CONFIDENTIALITY_DISABLE, Gitlab::WorkItems::Instrumentation::EventActions::CONFIDENTIALITY_ENABLE, Gitlab::WorkItems::Instrumentation::EventActions::CREATE, Gitlab::WorkItems::Instrumentation::EventActions::CREATE_CHILD_ITEMS_WIDGET, Gitlab::WorkItems::Instrumentation::EventActions::CREATE_GLOBAL_NAV, Gitlab::WorkItems::Instrumentation::EventActions::CREATE_VULNERABILITY, Gitlab::WorkItems::Instrumentation::EventActions::CREATE_WORK_ITEM_LIST, Gitlab::WorkItems::Instrumentation::EventActions::DESCRIPTION_UPDATE, Gitlab::WorkItems::Instrumentation::EventActions::DESIGN_CREATE, Gitlab::WorkItems::Instrumentation::EventActions::DESIGN_DESTROY, Gitlab::WorkItems::Instrumentation::EventActions::DESIGN_NOTE_CREATE, Gitlab::WorkItems::Instrumentation::EventActions::DESIGN_NOTE_DESTROY, Gitlab::WorkItems::Instrumentation::EventActions::DESIGN_UPDATE, Gitlab::WorkItems::Instrumentation::EventActions::DUE_DATE_UPDATE, Gitlab::WorkItems::Instrumentation::EventActions::HEALTH_STATUS_UPDATE, Gitlab::WorkItems::Instrumentation::EventActions::ITERATION_UPDATE, Gitlab::WorkItems::Instrumentation::EventActions::LABELS_UPDATE, Gitlab::WorkItems::Instrumentation::EventActions::LOCK, Gitlab::WorkItems::Instrumentation::EventActions::MARKED_AS_DUPLICATE, Gitlab::WorkItems::Instrumentation::EventActions::MILESTONE_UPDATE, Gitlab::WorkItems::Instrumentation::EventActions::MOVE, Gitlab::WorkItems::Instrumentation::EventActions::NOTE_CREATE, Gitlab::WorkItems::Instrumentation::EventActions::NOTE_DESTROY, Gitlab::WorkItems::Instrumentation::EventActions::NOTE_UPDATE, Gitlab::WorkItems::Instrumentation::EventActions::REFERENCE_ADD, Gitlab::WorkItems::Instrumentation::EventActions::RELATED_ITEM_ADD, Gitlab::WorkItems::Instrumentation::EventActions::RELATED_ITEM_REMOVE, Gitlab::WorkItems::Instrumentation::EventActions::REOPEN, Gitlab::WorkItems::Instrumentation::EventActions::START_DATE_UPDATE, Gitlab::WorkItems::Instrumentation::EventActions::TIME_ESTIMATE_UPDATE, Gitlab::WorkItems::Instrumentation::EventActions::TIME_SPENT_UPDATE, Gitlab::WorkItems::Instrumentation::EventActions::TITLE_UPDATE, Gitlab::WorkItems::Instrumentation::EventActions::UNLOCK, Gitlab::WorkItems::Instrumentation::EventActions::WEIGHT_UPDATE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EventActions

link_event, valid_event?

Constructor Details

#initialize(work_item, old_associations) ⇒ EventMappings



71
72
73
74
# File 'lib/gitlab/work_items/instrumentation/event_mappings.rb', line 71

def initialize(work_item, old_associations)
  @work_item = work_item
  @old_associations = old_associations
end

Class Method Details

.events_for(work_item:, old_associations:) ⇒ Object



67
68
69
# File 'lib/gitlab/work_items/instrumentation/event_mappings.rb', line 67

def self.events_for(work_item:, old_associations:)
  new(work_item, old_associations).events_to_track
end

Instance Method Details

#events_to_trackObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/gitlab/work_items/instrumentation/event_mappings.rb', line 76

def events_to_track
  events = []

  ATTRIBUTE_MAPPINGS.each do |mapping|
    change = @work_item.previous_changes[mapping[:key]]
    next unless change

    event = mapping[:event]
    if event.respond_to?(:call)
      event_name = event.call(change)
      events << event_name if event_name
    else
      events << event
    end
  end

  ASSOCIATION_MAPPINGS.each do |mapping|
    next unless @old_associations.key?(mapping[:key])

    old_value = @old_associations[mapping[:key]]
    # rubocop:disable GitlabSecurity/PublicSend -- model attribute, not user input
    new_value = if mapping[:accessor]
                  mapping[:accessor].call(@work_item)
                else
                  @work_item.public_send(mapping[:key])
                end
    # rubocop:enable GitlabSecurity/PublicSend

    events << mapping[:event] if mapping[:compare].call(old_value, new_value)
  end

  events
end