2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ResourceEvents
|
|
|
|
class ChangeLabelsService
|
|
|
|
attr_reader :resource, :user
|
|
|
|
|
|
|
|
def initialize(resource, user)
|
2021-04-29 21:17:54 +05:30
|
|
|
@resource = resource
|
|
|
|
@user = user
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def execute(added_labels: [], removed_labels: [])
|
|
|
|
label_hash = {
|
|
|
|
resource_column(resource) => resource.id,
|
|
|
|
user_id: user.id,
|
2019-07-31 22:56:46 +05:30
|
|
|
created_at: resource.system_note_timestamp
|
2018-11-18 11:00:15 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
ids = ApplicationRecord.legacy_bulk_insert(ResourceLabelEvent.table_name, labels, return_ids: true) # rubocop:disable Gitlab/BulkInsert
|
2022-08-27 11:52:29 +05:30
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
if resource.is_a?(Issue)
|
|
|
|
events = ResourceLabelEvent.id_in(ids)
|
|
|
|
events.first.trigger_note_subscription_create(events: events.to_a) if events.any?
|
|
|
|
end
|
2022-08-27 11:52:29 +05:30
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
create_timeline_events_from(added_labels: added_labels, removed_labels: removed_labels)
|
2018-11-20 20:47:30 +05:30
|
|
|
resource.expire_note_etag_cache
|
2021-02-22 17:27:13 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
return unless resource.is_a?(Issue)
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
Gitlab::UsageDataCounters::IssueActivityUniqueCounter.track_issue_label_changed_action(
|
|
|
|
author: user, project: resource.project)
|
|
|
|
|
|
|
|
events
|
2018-11-18 11:00:15 +05:30
|
|
|
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
|
2022-08-27 11:52:29 +05:30
|
|
|
|
|
|
|
def create_timeline_events_from(added_labels: [], removed_labels: [])
|
|
|
|
return unless resource.incident?
|
|
|
|
|
|
|
|
IncidentManagement::TimelineEvents::CreateService.change_labels(
|
|
|
|
resource,
|
|
|
|
user,
|
|
|
|
added_labels: added_labels,
|
|
|
|
removed_labels: removed_labels
|
|
|
|
)
|
|
|
|
end
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
ResourceEvents::ChangeLabelsService.prepend_mod_with('ResourceEvents::ChangeLabelsService')
|