From 806139936898726b32c4fe216ac3a9f4419ce91e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D=C3=A1vila?= Date: Wed, 3 Feb 2016 18:28:40 -0500 Subject: Add RevertService class with basic logic to revert commit --- app/services/commits/revert_service.rb | 48 ++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 app/services/commits/revert_service.rb (limited to 'app/services/commits/revert_service.rb') diff --git a/app/services/commits/revert_service.rb b/app/services/commits/revert_service.rb new file mode 100644 index 00000000000..4a364f0d86a --- /dev/null +++ b/app/services/commits/revert_service.rb @@ -0,0 +1,48 @@ +module Commits + class RevertService < ::BaseService + class ValidationError < StandardError; end + + def execute + @source_project = params[:source_project] || @project + @target_branch = params[:target_branch] + @commit_to_revert = @source_project.commit(params[:revert_commit_id]) + + # Check push permissions to branch + validate + + if commit + success + else + error("Something went wrong. Your changes were not committed") + end + rescue Repository::CommitError, Gitlab::Git::Repository::InvalidBlobName, GitHooksService::PreReceiveError, ValidationError => ex + error(ex.message) + end + + def commit + raw_repo = repository.rugged + + # Create branch with revert commit + reverted = repository.revert(current_user, @commit_to_revert.id, + @commit_to_revert.revert_branch_name, @target_branch, + @commit_to_revert.revert_message) + repository.rm_branch(current_user, @commit_to_revert.revert_branch_name) + + reverted + end + + private + + def raise_error(message) + raise ValidationError.new(message) + end + + def validate + 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") + end + end + end +end -- cgit v1.2.3