diff --git a/doc/api/openapi/openapi_v2.yaml b/doc/api/openapi/openapi_v2.yaml index fe9c9c3fdbe8d75e6d72ed01f8a40e7287c2b835..39d8f827fc37b8ed2c325eff76870be06a6c8c7d 100644 --- a/doc/api/openapi/openapi_v2.yaml +++ b/doc/api/openapi/openapi_v2.yaml @@ -61723,6 +61723,9 @@ definitions: sha: type: string description: When present, must have the HEAD SHA of the source branch + publish_review: + type: boolean + description: When `true` submits pending review comments approval_password: type: string description: Current user's password if project is set to require explicit diff --git a/lib/api/merge_request_approvals.rb b/lib/api/merge_request_approvals.rb index 684e2a53b5276ffa01d6d61e6a5b0144fbd5f501..5c0ac409bda852a1e48baf1ad7cacc9f00268965 100644 --- a/lib/api/merge_request_approvals.rb +++ b/lib/api/merge_request_approvals.rb @@ -52,6 +52,7 @@ def present_approval(merge_request) end params do optional :sha, type: String, desc: 'When present, must have the HEAD SHA of the source branch' + optional :publish_review, type: Boolean, desc: 'When `true` submits pending review comments' use :ee_approval_params end @@ -60,6 +61,12 @@ def present_approval(merge_request) check_sha_param!(params, merge_request) + if params[:publish_review] + result = ::DraftNotes::PublishService.new(merge_request, current_user).execute + + render_api_error('Failed to publish review', 500) unless result[:status] == :success + end + success = ::MergeRequests::ApprovalService .new(project: user_project, current_user: current_user, params: params) diff --git a/spec/requests/api/merge_request_approvals_spec.rb b/spec/requests/api/merge_request_approvals_spec.rb index c003736b9366baa85a305b292b9406a7101bf61e..d1738780d9308b0a5d9de27fb7bc6147594fdc0b 100644 --- a/spec/requests/api/merge_request_approvals_spec.rb +++ b/spec/requests/api/merge_request_approvals_spec.rb @@ -42,6 +42,50 @@ def approve(extra_params = {}) post api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/approve", approver), params: extra_params end + context 'when the publish_review param is not set' do + it 'approves the merge request' do + expect(DraftNotes::PublishService).not_to receive(:new) + + approve + + expect(response).to have_gitlab_http_status(:created) + end + end + + context 'when the publish_review param is false' do + it 'approves the merge request' do + expect(DraftNotes::PublishService).not_to receive(:new) + + approve(publish_review: false) + + expect(response).to have_gitlab_http_status(:created) + end + end + + context 'when the publish_review param is true' do + it 'approves the merge request' do + expect_next_instance_of(::DraftNotes::PublishService) do |service| + expect(service).to receive(:execute).and_call_original + end + + approve(publish_review: true) + + expect(response).to have_gitlab_http_status(:created) + end + + context 'when publish service returns an error' do + it 'returns an error and does not approve the merge request' do + expect_next_instance_of(::DraftNotes::PublishService) do |service| + expect(service).to receive(:execute).and_return(ServiceResponse.error(message: 'Error')) + end + + approve(publish_review: true) + + expect(response).to have_gitlab_http_status(:internal_server_error) + end + end + end + context 'when the sha param is not set' do it 'approves the merge request' do approve