2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
module MergeRequests
|
2019-12-26 22:10:19 +05:30
|
|
|
class RebaseService < MergeRequests::BaseService
|
2022-04-04 11:22:00 +05:30
|
|
|
REBASE_ERROR = 'Rebase failed: Rebase locally, resolve all conflicts, then push the branch.'
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
attr_reader :merge_request, :rebase_error
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
def validate(merge_request)
|
|
|
|
return error_response(_('Source branch does not exist')) unless
|
|
|
|
merge_request.source_branch_exists?
|
|
|
|
|
|
|
|
return error_response(_('Cannot push to source branch')) unless
|
|
|
|
user_access.can_push_to_branch?(merge_request.source_branch)
|
|
|
|
|
|
|
|
return error_response(_('Source branch is protected from force push')) unless
|
|
|
|
merge_request.permits_force_push?
|
|
|
|
|
|
|
|
ServiceResponse.success
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def execute(merge_request, skip_ci: false)
|
2018-03-17 18:26:18 +05:30
|
|
|
@merge_request = merge_request
|
2020-03-13 15:44:24 +05:30
|
|
|
@skip_ci = skip_ci
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
if rebase
|
|
|
|
success
|
|
|
|
else
|
2022-01-26 12:08:38 +05:30
|
|
|
error(rebase_error)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def rebase
|
2020-03-13 15:44:24 +05:30
|
|
|
repository.rebase(current_user, merge_request, skip_ci: @skip_ci)
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
true
|
2021-06-08 01:23:25 +05:30
|
|
|
rescue StandardError => e
|
2022-01-26 12:08:38 +05:30
|
|
|
set_rebase_error(e)
|
|
|
|
log_error(exception: e, message: rebase_error, save_message_on_model: true)
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
false
|
2019-09-30 21:07:59 +05:30
|
|
|
ensure
|
|
|
|
merge_request.update_column(:rebase_jid, nil)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_rebase_error(exception)
|
|
|
|
@rebase_error =
|
|
|
|
if exception.is_a?(Gitlab::Git::PreReceiveError)
|
2022-04-04 11:22:00 +05:30
|
|
|
"The rebase pre-receive hook failed: #{exception.message}."
|
2022-01-26 12:08:38 +05:30
|
|
|
else
|
|
|
|
REBASE_ERROR
|
|
|
|
end
|
|
|
|
end
|
2023-03-17 16:20:25 +05:30
|
|
|
|
|
|
|
def user_access
|
|
|
|
Gitlab::UserAccess.new(current_user, container: project)
|
|
|
|
end
|
|
|
|
|
|
|
|
def error_response(message)
|
|
|
|
ServiceResponse.error(message: message)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|