2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
module MergeRequests
|
2019-12-26 22:10:19 +05:30
|
|
|
class SquashService < MergeRequests::BaseService
|
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
|
2022-01-26 12:08:38 +05:30
|
|
|
if merge_request.commits_count == 1 && message&.strip == merge_request.first_commit.safe_message&.strip
|
2018-11-08 19:23:39 +05:30
|
|
|
return success(squash_sha: merge_request.diff_head_sha)
|
|
|
|
end
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
return error(s_("MergeRequests|Squashing not allowed: This project doesn't allow you to squash commits when merging.")) if squash_forbidden?
|
2020-07-28 23:09:34 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
squash! || error(s_('MergeRequests|Squashing failed: Squash the commits locally, resolve any conflicts, then push the branch.'))
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def squash!
|
2022-01-26 12:08:38 +05:30
|
|
|
squash_sha = repository.squash(current_user, merge_request, message)
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
success(squash_sha: squash_sha)
|
2021-06-08 01:23:25 +05:30
|
|
|
rescue StandardError => e
|
2020-05-24 23:13:21 +05:30
|
|
|
log_error(exception: e, message: 'Failed to squash merge request')
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
false
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
def squash_forbidden?
|
|
|
|
target_project.squash_never?
|
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
def repository
|
|
|
|
target_project.repository
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge_request
|
|
|
|
params[:merge_request]
|
|
|
|
end
|
|
|
|
|
|
|
|
def message
|
2022-03-02 08:16:31 +05:30
|
|
|
params[:squash_commit_message].presence || merge_request.default_squash_commit_message(user: current_user)
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
end
|