From 26932c4ef66184ccdd616572d5fcb57793d23e30 Mon Sep 17 00:00:00 2001 From: Phil Hughes Date: Fri, 10 Oct 2025 15:28:48 +0100 Subject: [PATCH] Add `publish_review` param to approve API endpoint Adds a `publish_review` boolean param to the approve API endpoint that will submit any pending review comments before approving. https://gitlab.com/gitlab-org/gitlab/-/issues/562579 --- doc/api/openapi/openapi_v2.yaml | 3 ++ lib/api/merge_request_approvals.rb | 7 +++ .../api/merge_request_approvals_spec.rb | 44 +++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/doc/api/openapi/openapi_v2.yaml b/doc/api/openapi/openapi_v2.yaml index fe9c9c3fdbe8d7..39d8f827fc37b8 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 684e2a53b5276f..5c0ac409bda852 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 c003736b9366ba..d1738780d9308b 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 -- GitLab