2018-07-17 12:50:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-29 05:51:43 -04:00
|
|
|
module MergeRequests
|
2019-11-15 13:06:24 -05:00
|
|
|
class SquashService < MergeRequests::BaseService
|
2019-02-06 07:33:11 -05:00
|
|
|
def execute
|
|
|
|
# If performing a squash would result in no change, then
|
|
|
|
# immediately return a success message without performing a squash
|
2021-11-19 13:12:50 -05:00
|
|
|
if merge_request.commits_count == 1 && message == merge_request.first_commit.safe_message
|
2020-08-06 17:10:15 -04:00
|
|
|
return success(squash_sha: merge_request.diff_head_sha)
|
|
|
|
end
|
2018-05-29 05:51:43 -04:00
|
|
|
|
2020-06-30 14:09:13 -04:00
|
|
|
return error(s_('MergeRequests|This project does not allow squashing commits when merge requests are accepted.')) if squash_forbidden?
|
|
|
|
|
2019-04-15 08:25:48 -04:00
|
|
|
squash! || error(s_('MergeRequests|Failed to squash. Should be done manually.'))
|
2019-02-06 07:33:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def squash!
|
2021-11-19 13:12:50 -05:00
|
|
|
squash_sha = repository.squash(current_user, merge_request, message)
|
2018-05-29 05:51:43 -04:00
|
|
|
|
|
|
|
success(squash_sha: squash_sha)
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError => e
|
2020-05-07 11:09:29 -04:00
|
|
|
log_error(exception: e, message: 'Failed to squash merge request')
|
|
|
|
|
2018-05-29 05:51:43 -04:00
|
|
|
false
|
|
|
|
end
|
2019-02-06 07:33:11 -05:00
|
|
|
|
2020-06-30 14:09:13 -04:00
|
|
|
def squash_forbidden?
|
|
|
|
target_project.squash_never?
|
|
|
|
end
|
|
|
|
|
2019-02-06 07:33:11 -05:00
|
|
|
def repository
|
|
|
|
target_project.repository
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge_request
|
|
|
|
params[:merge_request]
|
|
|
|
end
|
|
|
|
|
|
|
|
def message
|
2021-11-19 13:12:50 -05:00
|
|
|
params[:squash_commit_message].presence || merge_request.default_squash_commit_message
|
2019-02-06 07:33:11 -05:00
|
|
|
end
|
2018-05-29 05:51:43 -04:00
|
|
|
end
|
|
|
|
end
|