Commit 855b1ef0 authored by Alex Kalderimis's avatar Alex Kalderimis

Optimize for setting MR assignees

This uses the new specific service from
https://gitlab.com/gitlab-org/gitlab/-/merge_requests/57523 to handle
cases where we are setting assignees.
parent 397fb73b
...@@ -7,15 +7,17 @@ module MergeRequests ...@@ -7,15 +7,17 @@ module MergeRequests
# This saves a lot of queries for irrelevant things that cannot possibly # This saves a lot of queries for irrelevant things that cannot possibly
# change in the execution of this service. # change in the execution of this service.
def execute(merge_request) def execute(merge_request)
return unless current_user&.can?(:update_merge_request, merge_request) return merge_request unless current_user&.can?(:update_merge_request, merge_request)
old_ids = merge_request.assignees.map(&:id) old_ids = merge_request.assignees.map(&:id)
return if old_ids.to_set == update_attrs[:assignee_ids].to_set # no-change return merge_request if old_ids.to_set == update_attrs[:assignee_ids].to_set # no-change
merge_request.update!(**update_attrs) merge_request.update!(**update_attrs)
# Defer the more expensive operations (handle_assignee_changes) to the background # Defer the more expensive operations (handle_assignee_changes) to the background
MergeRequests::AssigneesChangeWorker.perform_async(merge_request.id, current_user.id, old_ids) MergeRequests::AssigneesChangeWorker.perform_async(merge_request.id, current_user.id, old_ids)
merge_request
end end
def handle_assignee_changes(merge_request, old_assignees) def handle_assignee_changes(merge_request, old_assignees)
......
...@@ -424,7 +424,14 @@ module API ...@@ -424,7 +424,14 @@ module API
mr_params[:force_remove_source_branch] = mr_params.delete(:remove_source_branch) if mr_params.has_key?(:remove_source_branch) mr_params[:force_remove_source_branch] = mr_params.delete(:remove_source_branch) if mr_params.has_key?(:remove_source_branch)
mr_params = convert_parameters_from_legacy_format(mr_params) mr_params = convert_parameters_from_legacy_format(mr_params)
merge_request = ::MergeRequests::UpdateService.new(user_project, current_user, mr_params).execute(merge_request) service = if mr_params.keys == [:assignee_ids] || mr_params.keys == [:assignee_id]
mr_params[:assignee_ids] = Array.wrap(mr_params[:assignee_ids]) if mr_params.key?(:assignee_id)
::MergeRequests::UpdateAssigneesService
else
::MergeRequests::UpdateService
end
merge_request = service.new(user_project, current_user, mr_params).execute(merge_request)
handle_merge_request_errors!(merge_request) handle_merge_request_errors!(merge_request)
......
...@@ -2151,6 +2151,23 @@ RSpec.describe API::MergeRequests do ...@@ -2151,6 +2151,23 @@ RSpec.describe API::MergeRequests do
let(:entity) { merge_request } let(:entity) { merge_request }
end end
context 'when only assignee_ids are provided' do
let(:params) do
{
assignee_ids: [user2.id]
}
end
it 'sets the assignees' do
put api("/projects/#{project.id}/merge_requests/#{merge_request.iid}", user), params: params
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['assignees']).to contain_exactly(
a_hash_including('name' => user2.name)
)
end
end
context 'accepts reviewer_ids' do context 'accepts reviewer_ids' do
let(:params) do let(:params) do
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment