2018-07-17 16:50:37 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-29 09:51:43 +00:00
|
|
|
module MergeRequests
|
2019-11-15 18:06:24 +00:00
|
|
|
class SquashService < MergeRequests::BaseService
|
2020-05-07 15:09:29 +00:00
|
|
|
SquashInProgressError = Class.new(RuntimeError)
|
2019-11-15 18:06:24 +00:00
|
|
|
|
2019-02-06 12:33:11 +00:00
|
|
|
def execute
|
|
|
|
# If performing a squash would result in no change, then
|
|
|
|
# immediately return a success message without performing a squash
|
2020-07-17 21:09:23 +00:00
|
|
|
return success(squash_sha: merge_request.diff_head_sha) if squash_redundant?
|
2018-05-29 09:51:43 +00:00
|
|
|
|
2020-06-30 18:09:13 +00:00
|
|
|
return error(s_('MergeRequests|This project does not allow squashing commits when merge requests are accepted.')) if squash_forbidden?
|
|
|
|
|
2020-05-07 15:09:29 +00:00
|
|
|
if squash_in_progress?
|
2019-04-15 12:25:48 +00:00
|
|
|
return error(s_('MergeRequests|Squash task canceled: another squash is already in progress.'))
|
2018-05-29 09:51:43 +00:00
|
|
|
end
|
|
|
|
|
2019-04-15 12:25:48 +00:00
|
|
|
squash! || error(s_('MergeRequests|Failed to squash. Should be done manually.'))
|
2020-06-30 18:09:13 +00:00
|
|
|
|
2020-05-07 15:09:29 +00:00
|
|
|
rescue SquashInProgressError
|
|
|
|
error(s_('MergeRequests|An error occurred while checking whether another squash is in progress.'))
|
2019-02-06 12:33:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-07-17 21:09:23 +00:00
|
|
|
def squash_redundant?
|
|
|
|
return true if merge_request.merged?
|
|
|
|
|
|
|
|
merge_request.commits_count < 2 && message.nil?
|
|
|
|
end
|
|
|
|
|
2019-02-06 12:33:11 +00:00
|
|
|
def squash!
|
|
|
|
squash_sha = repository.squash(current_user, merge_request, message || merge_request.default_squash_commit_message)
|
2018-05-29 09:51:43 +00:00
|
|
|
|
|
|
|
success(squash_sha: squash_sha)
|
|
|
|
rescue => e
|
2020-05-07 15:09:29 +00:00
|
|
|
log_error(exception: e, message: 'Failed to squash merge request')
|
|
|
|
|
2018-05-29 09:51:43 +00:00
|
|
|
false
|
|
|
|
end
|
2019-02-06 12:33:11 +00:00
|
|
|
|
2020-05-07 15:09:29 +00:00
|
|
|
def squash_in_progress?
|
|
|
|
merge_request.squash_in_progress?
|
|
|
|
rescue => e
|
|
|
|
log_error(exception: e, message: 'Failed to check squash in progress')
|
|
|
|
|
|
|
|
raise SquashInProgressError, e.message
|
|
|
|
end
|
|
|
|
|
2020-06-30 18:09:13 +00:00
|
|
|
def squash_forbidden?
|
|
|
|
target_project.squash_never?
|
|
|
|
end
|
|
|
|
|
2019-02-06 12:33:11 +00:00
|
|
|
def repository
|
|
|
|
target_project.repository
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge_request
|
|
|
|
params[:merge_request]
|
|
|
|
end
|
|
|
|
|
|
|
|
def message
|
|
|
|
params[:squash_commit_message].presence
|
|
|
|
end
|
2018-05-29 09:51:43 +00:00
|
|
|
end
|
|
|
|
end
|