gitlab-org--gitlab-foss/app/services/delete_branch_service.rb
Alejandro Rodríguez fa3bbd449e Fix lightweight tags not processed correctly by GitTagPushService
When we updated gitlab_git to 10.4.1, `tag.target` changed from pointing
to the sha of the tag to the sha of the commit the tag points to. The
problem is that only annotated tags have `object_sha`s, lightweight tags
don't (it's nil), so (only) in their case we still need to use
`tag.target`.
2016-10-28 13:53:18 -03:00

50 lines
1.2 KiB
Ruby

require_relative 'base_service'
class DeleteBranchService < BaseService
def execute(branch_name)
repository = project.repository
branch = repository.find_branch(branch_name)
unless branch
return error('No such branch', 404)
end
if branch_name == repository.root_ref
return error('Cannot remove HEAD branch', 405)
end
if project.protected_branch?(branch_name)
return error('Protected branch cant be removed', 405)
end
unless current_user.can?(:push_code, project)
return error('You dont have push access to repo', 405)
end
if repository.rm_branch(current_user, branch_name)
success('Branch was removed')
else
error('Failed to remove branch')
end
rescue GitHooksService::PreReceiveError => ex
error(ex.message)
end
def error(message, return_code = 400)
super(message).merge(return_code: return_code)
end
def success(message)
super().merge(message: message)
end
def build_push_data(branch)
Gitlab::DataBuilder::Push.build(
project,
current_user,
branch.dereferenced_target.sha,
Gitlab::Git::BLANK_SHA,
"#{Gitlab::Git::BRANCH_REF_PREFIX}#{branch.name}",
[])
end
end