2014-09-21 04:29:52 -04:00
|
|
|
require_relative 'base_service'
|
|
|
|
|
|
|
|
class DeleteBranchService < BaseService
|
|
|
|
def execute(branch_name)
|
2014-05-22 07:23:20 -04:00
|
|
|
repository = project.repository
|
|
|
|
branch = repository.find_branch(branch_name)
|
|
|
|
|
|
|
|
# No such branch
|
|
|
|
unless branch
|
2014-07-27 10:40:00 -04:00
|
|
|
return error('No such branch', 404)
|
2014-05-22 07:23:20 -04:00
|
|
|
end
|
|
|
|
|
2014-05-22 07:39:09 -04:00
|
|
|
if branch_name == repository.root_ref
|
2014-07-27 10:40:00 -04:00
|
|
|
return error('Cannot remove HEAD branch', 405)
|
2014-05-22 07:39:09 -04:00
|
|
|
end
|
|
|
|
|
2014-05-22 07:23:20 -04:00
|
|
|
# Dont allow remove of protected branch
|
|
|
|
if project.protected_branch?(branch_name)
|
2014-07-27 10:40:00 -04:00
|
|
|
return error('Protected branch cant be removed', 405)
|
2014-05-22 07:23:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Dont allow user to remove branch if he is not allowed to push
|
|
|
|
unless current_user.can?(:push_code, project)
|
2014-07-27 10:40:00 -04:00
|
|
|
return error('You dont have push access to repo', 405)
|
2014-05-22 07:23:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if repository.rm_branch(branch_name)
|
2015-03-13 09:55:17 -04:00
|
|
|
push_data = build_push_data(branch)
|
|
|
|
|
|
|
|
EventCreateService.new.push(project, current_user, push_data)
|
2015-03-13 09:56:08 -04:00
|
|
|
project.execute_hooks(push_data.dup, :push_hooks)
|
|
|
|
project.execute_services(push_data.dup, :push_hooks)
|
|
|
|
|
2014-05-22 07:23:20 -04:00
|
|
|
success('Branch was removed')
|
|
|
|
else
|
2015-03-13 09:55:17 -04:00
|
|
|
error('Failed to remove branch')
|
2014-05-22 07:23:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-27 10:40:00 -04:00
|
|
|
def error(message, return_code = 400)
|
2014-09-21 04:29:52 -04:00
|
|
|
out = super(message)
|
|
|
|
out[:return_code] = return_code
|
|
|
|
out
|
2014-05-22 07:23:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def success(message)
|
2014-09-21 04:29:52 -04:00
|
|
|
out = super()
|
|
|
|
out[:message] = message
|
|
|
|
out
|
2014-05-22 07:23:20 -04:00
|
|
|
end
|
2015-03-13 09:55:17 -04:00
|
|
|
|
|
|
|
def build_push_data(branch)
|
|
|
|
Gitlab::PushDataBuilder
|
|
|
|
.build(project, current_user, branch.target, Gitlab::Git::BLANK_SHA, "#{Gitlab::Git::BRANCH_REF_PREFIX}#{branch.name}", [])
|
|
|
|
end
|
2014-05-22 07:23:20 -04:00
|
|
|
end
|