Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRubén Dávila <rdavila84@gmail.com>2016-02-04 02:28:40 +0300
committerRobert Speicher <rspeicher@gmail.com>2016-02-19 21:14:50 +0300
commit806139936898726b32c4fe216ac3a9f4419ce91e (patch)
tree7329f75325b00fe74999ed1d5d384c9ff01038d3 /app/services/commits/revert_service.rb
parent34e26b8212954dba32165c39b63858658b82c0f0 (diff)
Add RevertService class with basic logic to revert commit
Diffstat (limited to 'app/services/commits/revert_service.rb')
-rw-r--r--app/services/commits/revert_service.rb48
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