2018-08-01 08:58:49 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ResourceEvents
|
|
|
|
class ChangeLabelsService
|
|
|
|
attr_reader :resource, :user
|
|
|
|
|
|
|
|
def initialize(resource, user)
|
2021-04-19 21:09:27 +00:00
|
|
|
@resource = resource
|
|
|
|
@user = user
|
2018-08-01 08:58:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute(added_labels: [], removed_labels: [])
|
|
|
|
label_hash = {
|
|
|
|
resource_column(resource) => resource.id,
|
|
|
|
user_id: user.id,
|
2019-04-08 15:33:30 +00:00
|
|
|
created_at: resource.system_note_timestamp
|
2018-08-01 08:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
labels = added_labels.map do |label|
|
|
|
|
label_hash.merge(label_id: label.id, action: ResourceLabelEvent.actions['add'])
|
|
|
|
end
|
|
|
|
labels += removed_labels.map do |label|
|
|
|
|
label_hash.merge(label_id: label.id, action: ResourceLabelEvent.actions['remove'])
|
|
|
|
end
|
|
|
|
|
2021-11-15 15:10:57 +00:00
|
|
|
ApplicationRecord.legacy_bulk_insert(ResourceLabelEvent.table_name, labels) # rubocop:disable Gitlab/BulkInsert
|
2018-09-07 13:39:20 +00:00
|
|
|
resource.expire_note_etag_cache
|
2020-12-10 12:09:43 +00:00
|
|
|
|
|
|
|
Gitlab::UsageDataCounters::IssueActivityUniqueCounter.track_issue_label_changed_action(author: user) if resource.is_a?(Issue)
|
2018-08-01 08:58:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def resource_column(resource)
|
|
|
|
case resource
|
|
|
|
when Issue
|
|
|
|
:issue_id
|
|
|
|
when MergeRequest
|
|
|
|
:merge_request_id
|
|
|
|
else
|
|
|
|
raise ArgumentError, "Unknown resource type #{resource.class.name}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
2021-05-11 21:10:21 +00:00
|
|
|
ResourceEvents::ChangeLabelsService.prepend_mod_with('ResourceEvents::ChangeLabelsService')
|