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
|
# frozen_string_literal: true
module Gitlab
module ResourceEvents
class AssignmentEventRecorder
BATCH_SIZE = 100
def initialize(parent:, old_assignees:)
@parent = parent
@old_assignees = old_assignees
end
def record
case parent
when Issue
record_for_parent(
::ResourceEvents::IssueAssignmentEvent,
:issue_id,
parent,
old_assignees
)
when MergeRequest
record_for_parent(
::ResourceEvents::MergeRequestAssignmentEvent,
:merge_request_id,
parent,
old_assignees
)
end
end
private
attr_reader :parent, :old_assignees
def record_for_parent(resource_klass, foreign_key, parent, old_assignees)
removed_events = (old_assignees - parent.assignees).map do |unassigned_user|
{
foreign_key => parent.id,
user_id: unassigned_user.id,
action: :remove
}
end.to_set
added_events = (parent.assignees.to_a - old_assignees).map do |added_user|
{
foreign_key => parent.id,
user_id: added_user.id,
action: :add
}
end.to_set
(removed_events + added_events).each_slice(BATCH_SIZE) do |events|
resource_klass.insert_all(events)
end
end
end
end
end
|