Commit 66a0ec77 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Extract EE specific code to ee/lib/ee/api/merge_requests.rb

So we keep minimum difference between CE and EE for
lib/api/merge_requests.rb
parent fa2c63ea
module EE
module API
module MergeRequests
module ClassMethods
def update_params_at_least_one_of
super.push(*%i[
squash
])
end
end
# rubocop:disable Metrics/AbcSize
def self.prepended(api)
api.singleton_class.prepend(ClassMethods)
api.module_eval do
helpers do
params :merge_params_ee do
optional :squash, type: Grape::API::Boolean, desc: 'When true, the commits will be squashed into a single commit on merge'
end
params :optional_params_ee do
optional :approvals_before_merge, type: Integer, desc: 'Number of approvals required before this can be merged'
use :merge_params_ee
end
def update_merge_request_ee(merge_request)
if params[:squash] && merge_request.project.feature_available?(:merge_request_squash)
merge_request.update(squash: params[:squash])
end
end
end
params do
requires :id, type: String, desc: 'The ID of a project'
end
resource :projects, requirements: ::API::API::PROJECT_ENDPOINT_REQUIREMENTS do
# Get the status of the merge request's approvals
#
# Parameters:
# id (required) - The ID of a project
# merge_request_idd (required) - IID of MR
# Examples:
# GET /projects/:id/merge_requests/:merge_request_iid/approvals
#
desc "List a merge request's approvals" do
success EE::API::Entities::MergeRequestApprovals
end
get ':id/merge_requests/:merge_request_iid/approvals' do
merge_request = find_merge_request_with_access(params[:merge_request_iid])
present merge_request, with: EE::API::Entities::MergeRequestApprovals, current_user: current_user
end
# Approve a merge request
#
# Parameters:
# id (required) - The ID of a project
# merge_request_iid (required) - IID of MR
# Examples:
# POST /projects/:id/merge_requests/:merge_request_iid/approve
#
desc 'Approve a merge request' do
success EE::API::Entities::MergeRequestApprovals
end
params do
optional :sha, type: String, desc: 'When present, must have the HEAD SHA of the source branch'
end
post ':id/merge_requests/:merge_request_iid/approve' do
merge_request = find_project_merge_request(params[:merge_request_iid])
unauthorized! unless merge_request.can_approve?(current_user)
check_sha_param!(params, merge_request)
::MergeRequests::ApprovalService
.new(user_project, current_user)
.execute(merge_request)
present merge_request, with: EE::API::Entities::MergeRequestApprovals, current_user: current_user
end
desc 'Remove an approval from a merge request' do
success EE::API::Entities::MergeRequestApprovals
end
post ':id/merge_requests/:merge_request_iid/unapprove' do
merge_request = find_project_merge_request(params[:merge_request_iid])
not_found! unless merge_request.has_approved?(current_user)
::MergeRequests::RemoveApprovalService
.new(user_project, current_user)
.execute(merge_request)
present merge_request, with: EE::API::Entities::MergeRequestApprovals, current_user: current_user
end
end
end
end
# rubocop:enable Metrics/AbcSize
end
end
end
......@@ -6,6 +6,34 @@ module API
helpers ::Gitlab::IssuableMetadata
# EE::API::MergeRequests would override the following helpers
helpers do
params :optional_params_ee do
end
params :merge_params_ee do
end
def update_merge_request_ee(merge_request)
end
end
def self.update_params_at_least_one_of
%i[
assignee_id
description
labels
milestone_id
remove_source_branch
state_event
target_branch
title
discussion_locked
]
end
prepend EE::API::MergeRequests
helpers do
def find_merge_requests(args = {})
args = declared_params.merge(args)
......@@ -31,6 +59,12 @@ module API
mr.all_pipelines
end
def check_sha_param!(params, merge_request)
if params[:sha] && merge_request.diff_head_sha != params[:sha]
render_api_error!("SHA does not match HEAD of source branch: #{merge_request.diff_head_sha}", 409)
end
end
params :merge_requests_params do
optional :state, type: String, values: %w[opened closed merged all], default: 'all',
desc: 'Return opened, closed, merged, or all merge requests'
......@@ -104,27 +138,13 @@ module API
render_api_error!(errors, 400)
end
def check_sha_param!(params, merge_request)
if params[:sha] && merge_request.diff_head_sha != params[:sha]
render_api_error!("SHA does not match HEAD of source branch: #{merge_request.diff_head_sha}", 409)
end
end
params :optional_params_ce do
params :optional_params do
optional :description, type: String, desc: 'The description of the merge request'
optional :assignee_id, type: Integer, desc: 'The ID of a user to assign the merge request'
optional :milestone_id, type: Integer, desc: 'The ID of a milestone to assign the merge request'
optional :labels, type: String, desc: 'Comma-separated list of label names'
optional :remove_source_branch, type: Boolean, desc: 'Remove source branch when merging'
end
params :optional_params_ee do
optional :approvals_before_merge, type: Integer, desc: 'Number of approvals required before this can be merged'
optional :squash, type: Boolean, desc: 'Squash commits when merging'
end
params :optional_params do
use :optional_params_ce
use :optional_params_ee
end
end
......@@ -250,31 +270,14 @@ module API
success Entities::MergeRequest
end
params do
# CE
at_least_one_of_ce = [
:assignee_id,
:description,
:labels,
:milestone_id,
:remove_source_branch,
:state_event,
:target_branch,
:title,
:discussion_locked
]
optional :title, type: String, allow_blank: false, desc: 'The title of the merge request'
optional :target_branch, type: String, allow_blank: false, desc: 'The target branch'
optional :state_event, type: String, values: %w[close reopen],
desc: 'Status of the merge request'
optional :discussion_locked, type: Boolean, desc: 'Whether the MR discussion is locked'
# EE
at_least_one_of_ee = [
:squash
]
use :optional_params
at_least_one_of(*(at_least_one_of_ce + at_least_one_of_ee))
at_least_one_of(*::API::MergeRequests.update_params_at_least_one_of)
end
put ':id/merge_requests/:merge_request_iid' do
Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab-ce/issues/42318')
......@@ -297,7 +300,6 @@ module API
success Entities::MergeRequest
end
params do
# CE
optional :merge_commit_message, type: String, desc: 'Custom merge commit message'
optional :should_remove_source_branch, type: Boolean,
desc: 'When true, the source branch will be deleted if possible'
......@@ -305,8 +307,7 @@ module API
desc: 'When true, this merge request will be merged when the pipeline succeeds'
optional :sha, type: String, desc: 'When present, must have the HEAD SHA of the source branch'
# EE
optional :squash, type: Boolean, desc: 'When true, the commits will be squashed into a single commit on merge'
use :merge_params_ee
end
put ':id/merge_requests/:merge_request_iid/merge' do
Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab-ce/issues/42317')
......@@ -324,9 +325,7 @@ module API
check_sha_param!(params, merge_request)
if params[:squash] && merge_request.project.feature_available?(:merge_request_squash)
merge_request.update(squash: params[:squash])
end
update_merge_request_ee(merge_request)
merge_params = {
commit_message: params[:merge_commit_message],
......@@ -359,66 +358,6 @@ module API
.cancel(merge_request)
end
# Get the status of the merge request's approvals
#
# Parameters:
# id (required) - The ID of a project
# merge_request_idd (required) - IID of MR
# Examples:
# GET /projects/:id/merge_requests/:merge_request_iid/approvals
#
desc "List a merge request's approvals" do
success EE::API::Entities::MergeRequestApprovals
end
get ':id/merge_requests/:merge_request_iid/approvals' do
merge_request = find_merge_request_with_access(params[:merge_request_iid])
present merge_request, with: EE::API::Entities::MergeRequestApprovals, current_user: current_user
end
# Approve a merge request
#
# Parameters:
# id (required) - The ID of a project
# merge_request_iid (required) - IID of MR
# Examples:
# POST /projects/:id/merge_requests/:merge_request_iid/approve
#
desc 'Approve a merge request' do
success EE::API::Entities::MergeRequestApprovals
end
params do
optional :sha, type: String, desc: 'When present, must have the HEAD SHA of the source branch'
end
post ':id/merge_requests/:merge_request_iid/approve' do
merge_request = find_project_merge_request(params[:merge_request_iid])
unauthorized! unless merge_request.can_approve?(current_user)
check_sha_param!(params, merge_request)
::MergeRequests::ApprovalService
.new(user_project, current_user)
.execute(merge_request)
present merge_request, with: EE::API::Entities::MergeRequestApprovals, current_user: current_user
end
desc 'Remove an approval from a merge request' do
success EE::API::Entities::MergeRequestApprovals
end
post ':id/merge_requests/:merge_request_iid/unapprove' do
merge_request = find_project_merge_request(params[:merge_request_iid])
not_found! unless merge_request.has_approved?(current_user)
::MergeRequests::RemoveApprovalService
.new(user_project, current_user)
.execute(merge_request)
present merge_request, with: EE::API::Entities::MergeRequestApprovals, current_user: current_user
end
desc 'List issues that will be closed on merge' do
success Entities::MRNote
end
......
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