2021-04-06 11:09:23 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module MergeRequests
|
|
|
|
class UpdateAssigneesService < UpdateService
|
|
|
|
# a stripped down service that only does what it must to update the
|
|
|
|
# assignees, and knows that it does not have to check for other updates.
|
|
|
|
# This saves a lot of queries for irrelevant things that cannot possibly
|
|
|
|
# change in the execution of this service.
|
|
|
|
def execute(merge_request)
|
2021-04-09 05:09:10 -04:00
|
|
|
return merge_request unless current_user&.can?(:update_merge_request, merge_request)
|
2021-04-06 11:09:23 -04:00
|
|
|
|
2021-05-20 11:10:13 -04:00
|
|
|
old_assignees = merge_request.assignees.to_a
|
2021-04-12 14:12:15 -04:00
|
|
|
old_ids = old_assignees.map(&:id)
|
2021-04-09 05:09:10 -04:00
|
|
|
new_ids = new_assignee_ids(merge_request)
|
2021-05-20 11:10:13 -04:00
|
|
|
|
|
|
|
return merge_request if merge_request.errors.any?
|
2021-04-09 05:09:10 -04:00
|
|
|
return merge_request if new_ids.size != update_attrs[:assignee_ids].size
|
|
|
|
return merge_request if old_ids.to_set == new_ids.to_set # no-change
|
2021-04-06 11:09:23 -04:00
|
|
|
|
2021-04-09 05:09:10 -04:00
|
|
|
attrs = update_attrs.merge(assignee_ids: new_ids)
|
|
|
|
merge_request.update!(**attrs)
|
2021-04-06 11:09:23 -04:00
|
|
|
|
2022-05-05 17:09:10 -04:00
|
|
|
bulk_update_assignees_state(merge_request, merge_request.assignees - old_assignees)
|
|
|
|
|
2021-04-06 11:09:23 -04:00
|
|
|
# Defer the more expensive operations (handle_assignee_changes) to the background
|
2021-04-12 14:12:15 -04:00
|
|
|
MergeRequests::HandleAssigneesChangeService
|
2021-05-11 23:10:21 -04:00
|
|
|
.new(project: project, current_user: current_user)
|
2021-04-12 14:12:15 -04:00
|
|
|
.async_execute(merge_request, old_assignees, execute_hooks: true)
|
2021-04-09 05:09:10 -04:00
|
|
|
|
|
|
|
merge_request
|
2021-04-06 11:09:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-04-09 05:09:10 -04:00
|
|
|
def new_assignee_ids(merge_request)
|
|
|
|
# prime the cache - prevent N+1 lookup during authorization loop.
|
2021-05-20 11:10:13 -04:00
|
|
|
user_ids = update_attrs[:assignee_ids]
|
|
|
|
return [] if user_ids.empty?
|
|
|
|
|
|
|
|
merge_request.project.team.max_member_access_for_user_ids(user_ids)
|
|
|
|
User.id_in(user_ids).map do |user|
|
2021-04-09 05:09:10 -04:00
|
|
|
if user.can?(:read_merge_request, merge_request)
|
|
|
|
user.id
|
|
|
|
else
|
|
|
|
merge_request.errors.add(
|
|
|
|
:assignees,
|
|
|
|
"Cannot assign #{user.to_reference} to #{merge_request.to_reference}"
|
|
|
|
)
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
|
2021-04-06 11:09:23 -04:00
|
|
|
def assignee_ids
|
2021-05-14 08:10:58 -04:00
|
|
|
params.fetch(:assignee_ids).reject { _1 == 0 }.first(1)
|
2021-04-06 11:09:23 -04:00
|
|
|
end
|
|
|
|
|
2021-04-09 05:09:10 -04:00
|
|
|
def params
|
|
|
|
ps = super
|
|
|
|
|
|
|
|
# allow either assignee_id or assignee_ids, preferring assignee_id if passed.
|
|
|
|
{ assignee_ids: ps.key?(:assignee_id) ? Array.wrap(ps[:assignee_id]) : ps[:assignee_ids] }
|
|
|
|
end
|
|
|
|
|
2021-04-06 11:09:23 -04:00
|
|
|
def update_attrs
|
|
|
|
@attrs ||= { updated_at: Time.current, updated_by: current_user, assignee_ids: assignee_ids }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
MergeRequests::UpdateAssigneesService.prepend_mod_with('MergeRequests::UpdateAssigneesService')
|