2021-04-29 21:17:54 +05:30
|
|
|
# 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)
|
|
|
|
return merge_request unless current_user&.can?(:update_merge_request, merge_request)
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
old_assignees = merge_request.assignees.to_a
|
2021-04-29 21:17:54 +05:30
|
|
|
old_ids = old_assignees.map(&:id)
|
2022-08-27 11:52:29 +05:30
|
|
|
new_ids = new_user_ids(merge_request, update_attrs[:assignee_ids], :assignees)
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
return merge_request if merge_request.errors.any?
|
2021-04-29 21:17:54 +05:30
|
|
|
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
|
|
|
|
|
|
|
|
attrs = update_attrs.merge(assignee_ids: new_ids)
|
2022-11-25 23:54:43 +05:30
|
|
|
|
|
|
|
# We now have assignees validation on merge request
|
|
|
|
# If we use an update with bang, it will explode,
|
|
|
|
# instead we need to check if its valid then return if its not valid.
|
|
|
|
if Feature.enabled?(:limit_assignees_per_issuable)
|
|
|
|
merge_request.update(**attrs)
|
|
|
|
|
|
|
|
return merge_request unless merge_request.valid?
|
|
|
|
else
|
|
|
|
merge_request.update!(**attrs)
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
|
|
|
|
# Defer the more expensive operations (handle_assignee_changes) to the background
|
|
|
|
MergeRequests::HandleAssigneesChangeService
|
2021-06-08 01:23:25 +05:30
|
|
|
.new(project: project, current_user: current_user)
|
2021-04-29 21:17:54 +05:30
|
|
|
.async_execute(merge_request, old_assignees, execute_hooks: true)
|
|
|
|
|
|
|
|
merge_request
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def assignee_ids
|
2022-08-27 11:52:29 +05:30
|
|
|
filter_sentinel_values(params.fetch(:assignee_ids)).first(1)
|
2021-04-29 21:17:54 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def update_attrs
|
|
|
|
@attrs ||= { updated_at: Time.current, updated_by: current_user, assignee_ids: assignee_ids }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
MergeRequests::UpdateAssigneesService.prepend_mod_with('MergeRequests::UpdateAssigneesService')
|