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
|
# frozen_string_literal: true
module ResourceEvents
class ChangeLabelsService
attr_reader :resource, :user
def initialize(resource, user)
@resource = resource
@user = user
end
def execute(added_labels: [], removed_labels: [])
label_hash = {
resource_column(resource) => resource.id,
user_id: user.id,
created_at: resource.system_note_timestamp
}
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
ids = ApplicationRecord.legacy_bulk_insert(ResourceLabelEvent.table_name, labels, return_ids: true) # rubocop:disable Gitlab/BulkInsert
if resource.is_a?(Issue)
events = ResourceLabelEvent.id_in(ids)
events.first.trigger_note_subscription_create(events: events.to_a) if events.any?
end
create_timeline_events_from(added_labels: added_labels, removed_labels: removed_labels)
resource.broadcast_notes_changed
return unless resource.is_a?(Issue)
Gitlab::UsageDataCounters::IssueActivityUniqueCounter.track_issue_label_changed_action(
author: user, project: resource.project)
events
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
def create_timeline_events_from(added_labels: [], removed_labels: [])
return unless resource.incident_type_issue?
IncidentManagement::TimelineEvents::CreateService.change_labels(
resource,
user,
added_labels: added_labels,
removed_labels: removed_labels
)
end
end
end
ResourceEvents::ChangeLabelsService.prepend_mod_with('ResourceEvents::ChangeLabelsService')
|