gitlab-org--gitlab-foss/app/services/merge_requests/squash_service.rb

46 lines
1.2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2018-05-29 05:51:43 -04:00
module MergeRequests
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
if merge_request.commits_count == 1 && message == merge_request.first_commit.safe_message
return success(squash_sha: merge_request.diff_head_sha)
end
2018-05-29 05:51:43 -04:00
return error(s_('MergeRequests|This project does not allow squashing commits when merge requests are accepted.')) if squash_forbidden?
squash! || error(s_('MergeRequests|Failed to squash. Should be done manually.'))
2019-02-06 07:33:11 -05:00
end
private
def squash!
squash_sha = repository.squash(current_user, merge_request, message)
2018-05-29 05:51:43 -04:00
success(squash_sha: squash_sha)
rescue StandardError => e
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
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
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