diff options
Diffstat (limited to 'app/services/commits/revert_service.rb')
-rw-r--r-- | app/services/commits/revert_service.rb | 29 |
1 files changed, 15 insertions, 14 deletions
diff --git a/app/services/commits/revert_service.rb b/app/services/commits/revert_service.rb index 43d1c766e35..a3c950ede1f 100644 --- a/app/services/commits/revert_service.rb +++ b/app/services/commits/revert_service.rb @@ -9,7 +9,8 @@ module Commits @commit = params[:commit] @create_merge_request = params[:create_merge_request].present? - validate and commit + check_push_permissions unless @create_merge_request + commit rescue Repository::CommitError, Gitlab::Git::Repository::InvalidBlobName, GitHooksService::PreReceiveError, ValidationError, ReversionError => ex error(ex.message) @@ -17,39 +18,39 @@ module Commits def commit revert_into = @create_merge_request ? @commit.revert_branch_name : @target_branch + revert_tree_id = repository.check_revert_content(@commit, @target_branch) - if @create_merge_request - # Temporary branch exists and contains the revert commit - return success if repository.find_branch(revert_into) + if revert_tree_id + create_target_branch(revert_into) if @create_merge_request - create_target_branch - end - - unless repository.revert(current_user, @commit, revert_into) + repository.revert(current_user, @commit, revert_into, revert_tree_id) + success + else error_msg = "Sorry, we cannot revert this #{params[:revert_type_title]} automatically. It may have already been reverted, or a more recent commit may have updated some of its content." raise ReversionError, error_msg end - - success end private - def create_target_branch + def create_target_branch(new_branch) + # Temporary branch exists and contains the revert commit + return success if repository.find_branch(new_branch) + result = CreateBranchService.new(@project, current_user) - .execute(@commit.revert_branch_name, @target_branch, source_project: @source_project) + .execute(new_branch, @target_branch, source_project: @source_project) if result[:status] == :error raise ReversionError, "There was an error creating the source branch: #{result[:message]}" end end - def validate + def check_push_permissions allowed = ::Gitlab::GitAccess.new(current_user, project).can_push_to_branch?(@target_branch) unless allowed - raise_error('You are not allowed to push into this branch') + raise ValidationError.new('You are not allowed to push into this branch') end true |