2016-02-03 23:28:40 +00:00
|
|
|
module Commits
|
|
|
|
class RevertService < ::BaseService
|
|
|
|
class ValidationError < StandardError; end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
@source_project = params[:source_project] || @project
|
2016-02-05 23:12:41 +00:00
|
|
|
@target_branch = params[:target_branch]
|
|
|
|
@commit = params[:commit]
|
2016-02-09 22:16:15 +00:00
|
|
|
@create_merge_request = params[:create_merge_request].present?
|
2016-02-03 23:28:40 +00:00
|
|
|
|
|
|
|
# Check push permissions to branch
|
|
|
|
validate
|
|
|
|
|
|
|
|
if commit
|
|
|
|
success
|
|
|
|
else
|
2016-02-08 20:37:27 +00:00
|
|
|
error("Sorry, we cannot revert this #{params[:revert_type_title]} automatically.
|
2016-02-07 17:56:51 +00:00
|
|
|
It may have already been reverted, or a more recent commit may
|
|
|
|
have updated some of its content.")
|
2016-02-03 23:28:40 +00:00
|
|
|
end
|
|
|
|
rescue Repository::CommitError, Gitlab::Git::Repository::InvalidBlobName, GitHooksService::PreReceiveError, ValidationError => ex
|
|
|
|
error(ex.message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit
|
2016-02-09 22:16:15 +00:00
|
|
|
if @create_merge_request
|
|
|
|
repository.revert(current_user, @commit, @target_branch, @commit.revert_branch_name)
|
|
|
|
else
|
|
|
|
repository.revert(current_user, @commit, @target_branch)
|
|
|
|
end
|
2016-02-03 23:28:40 +00:00
|
|
|
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
|