2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
module MergeRequests
|
|
|
|
class SquashService < MergeRequests::WorkingCopyBaseService
|
2019-03-02 22:35:43 +05:30
|
|
|
def execute
|
|
|
|
# If performing a squash would result in no change, then
|
|
|
|
# immediately return a success message without performing a squash
|
|
|
|
if merge_request.commits_count < 2 && message.nil?
|
2018-11-08 19:23:39 +05:30
|
|
|
return success(squash_sha: merge_request.diff_head_sha)
|
|
|
|
end
|
|
|
|
|
|
|
|
if merge_request.squash_in_progress?
|
|
|
|
return error('Squash task canceled: another squash is already in progress.')
|
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
squash! || error('Failed to squash. Should be done manually.')
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def squash!
|
|
|
|
squash_sha = repository.squash(current_user, merge_request, message || merge_request.default_squash_commit_message)
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
success(squash_sha: squash_sha)
|
|
|
|
rescue => e
|
|
|
|
log_error("Failed to squash merge request #{merge_request.to_reference(full: true)}:")
|
|
|
|
log_error(e.message)
|
|
|
|
false
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
def repository
|
|
|
|
target_project.repository
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge_request
|
|
|
|
params[:merge_request]
|
|
|
|
end
|
|
|
|
|
|
|
|
def message
|
|
|
|
params[:squash_commit_message].presence
|
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
end
|