2021-04-17 20:07:23 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Mutations
|
|
|
|
module MergeRequests
|
|
|
|
class Accept < Base
|
|
|
|
graphql_name 'MergeRequestAccept'
|
|
|
|
authorize :accept_merge_request
|
|
|
|
description <<~DESC
|
|
|
|
Accepts a merge request.
|
|
|
|
When accepted, the source branch will be merged into the target branch, either
|
|
|
|
immediately if possible, or using one of the automatic merge strategies.
|
|
|
|
DESC
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
NOT_MERGEABLE = 'This branch cannot be merged'
|
|
|
|
HOOKS_VALIDATION_ERROR = 'Pre-merge hooks failed'
|
|
|
|
SHA_MISMATCH = 'The merge-head is not at the anticipated SHA'
|
|
|
|
MERGE_FAILED = 'The merge failed'
|
|
|
|
ALREADY_SCHEDULED = 'The merge request is already scheduled to be merged'
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
argument :strategy,
|
|
|
|
::Types::MergeStrategyEnum,
|
|
|
|
required: false,
|
|
|
|
as: :auto_merge_strategy,
|
2023-03-17 16:20:25 +05:30
|
|
|
description: 'How to merge the merge request.'
|
2021-04-17 20:07:23 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
argument :commit_message, ::GraphQL::Types::String,
|
2021-04-17 20:07:23 +05:30
|
|
|
required: false,
|
|
|
|
description: 'Custom merge commit message.'
|
2021-10-27 15:23:28 +05:30
|
|
|
argument :sha, ::GraphQL::Types::String,
|
2021-04-17 20:07:23 +05:30
|
|
|
required: true,
|
2023-03-17 16:20:25 +05:30
|
|
|
description: 'HEAD SHA at the time when the merge was requested.'
|
2022-01-26 12:08:38 +05:30
|
|
|
argument :squash_commit_message, ::GraphQL::Types::String,
|
|
|
|
required: false,
|
|
|
|
description: 'Custom squash commit message (if squash is true).'
|
2021-04-17 20:07:23 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
argument :should_remove_source_branch, ::GraphQL::Types::Boolean,
|
2021-04-17 20:07:23 +05:30
|
|
|
required: false,
|
|
|
|
description: 'Should the source branch be removed.'
|
2021-10-27 15:23:28 +05:30
|
|
|
argument :squash, ::GraphQL::Types::Boolean,
|
2021-04-17 20:07:23 +05:30
|
|
|
required: false,
|
|
|
|
default_value: false,
|
|
|
|
description: 'Squash commits on the source branch before merge.'
|
|
|
|
|
|
|
|
def resolve(project_path:, iid:, **args)
|
2021-04-29 21:17:54 +05:30
|
|
|
Gitlab::QueryLimiting.disable!('https://gitlab.com/gitlab-org/gitlab/-/issues/4796')
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
merge_request = authorized_find!(project_path: project_path, iid: iid)
|
|
|
|
project = merge_request.target_project
|
|
|
|
merge_params = args.compact.with_indifferent_access
|
2021-06-08 01:23:25 +05:30
|
|
|
merge_service = ::MergeRequests::MergeService.new(project: project, current_user: current_user, params: merge_params)
|
2021-04-17 20:07:23 +05:30
|
|
|
|
|
|
|
if error = validate(merge_request, merge_service, merge_params)
|
|
|
|
return { merge_request: merge_request, errors: [error] }
|
|
|
|
end
|
|
|
|
|
|
|
|
merge_request.update(merge_error: nil, squash: merge_params[:squash])
|
|
|
|
|
|
|
|
result = if merge_params.key?(:auto_merge_strategy)
|
|
|
|
service = AutoMergeService.new(project, current_user, merge_params)
|
|
|
|
service.execute(merge_request, merge_params[:auto_merge_strategy])
|
|
|
|
else
|
|
|
|
merge_service.execute(merge_request)
|
|
|
|
end
|
|
|
|
|
|
|
|
{
|
|
|
|
merge_request: merge_request,
|
|
|
|
errors: result == :failed ? [MERGE_FAILED] : []
|
|
|
|
}
|
|
|
|
rescue ::MergeRequests::MergeBaseService::MergeError => e
|
|
|
|
{
|
|
|
|
merge_request: merge_request,
|
|
|
|
errors: [e.message]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate(merge_request, merge_service, merge_params)
|
|
|
|
if merge_request.auto_merge_enabled?
|
|
|
|
ALREADY_SCHEDULED
|
|
|
|
elsif !merge_request.mergeable?(skip_ci_check: merge_params.key?(:auto_merge_strategy))
|
|
|
|
NOT_MERGEABLE
|
|
|
|
elsif !merge_service.hooks_validation_pass?(merge_request)
|
|
|
|
HOOKS_VALIDATION_ERROR
|
|
|
|
elsif merge_params[:sha] != merge_request.diff_head_sha
|
|
|
|
SHA_MISMATCH
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|