diff options
author | Rubén Dávila <rdavila84@gmail.com> | 2016-02-03 18:28:40 -0500 |
---|---|---|
committer | Robert Speicher <rspeicher@gmail.com> | 2016-02-19 13:14:50 -0500 |
commit | 806139936898726b32c4fe216ac3a9f4419ce91e (patch) | |
tree | 7329f75325b00fe74999ed1d5d384c9ff01038d3 /app/services/commits | |
parent | 34e26b8212954dba32165c39b63858658b82c0f0 (diff) | |
download | gitlab-ce-806139936898726b32c4fe216ac3a9f4419ce91e.tar.gz |
Add RevertService class with basic logic to revert commit
Diffstat (limited to 'app/services/commits')
-rw-r--r-- | app/services/commits/revert_service.rb | 48 |
1 files changed, 48 insertions, 0 deletions
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 |