2018-07-05 10:18:17 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-21 08:29:52 +00:00
|
|
|
class DeleteBranchService < BaseService
|
|
|
|
def execute(branch_name)
|
2014-05-22 11:23:20 +00:00
|
|
|
repository = project.repository
|
|
|
|
branch = repository.find_branch(branch_name)
|
|
|
|
|
|
|
|
unless current_user.can?(:push_code, project)
|
2014-07-27 14:40:00 +00:00
|
|
|
return error('You dont have push access to repo', 405)
|
2014-05-22 11:23:20 +00:00
|
|
|
end
|
|
|
|
|
2017-05-08 07:41:58 +00:00
|
|
|
unless branch
|
|
|
|
return error('No such branch', 404)
|
|
|
|
end
|
|
|
|
|
2015-11-26 00:20:40 +00:00
|
|
|
if repository.rm_branch(current_user, branch_name)
|
2014-05-22 11:23:20 +00:00
|
|
|
success('Branch was removed')
|
|
|
|
else
|
2015-03-13 13:55:17 +00:00
|
|
|
error('Failed to remove branch')
|
2014-05-22 11:23:20 +00:00
|
|
|
end
|
2018-06-11 10:42:09 +00:00
|
|
|
rescue Gitlab::Git::PreReceiveError => ex
|
2016-07-04 12:30:22 +00:00
|
|
|
error(ex.message)
|
2014-05-22 11:23:20 +00:00
|
|
|
end
|
|
|
|
|
2014-07-27 14:40:00 +00:00
|
|
|
def error(message, return_code = 400)
|
2016-07-06 13:26:59 +00:00
|
|
|
super(message).merge(return_code: return_code)
|
2014-05-22 11:23:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def success(message)
|
2016-07-06 13:26:59 +00:00
|
|
|
super().merge(message: message)
|
2014-05-22 11:23:20 +00:00
|
|
|
end
|
2015-03-13 13:55:17 +00:00
|
|
|
|
|
|
|
def build_push_data(branch)
|
2016-08-12 08:09:29 +00:00
|
|
|
Gitlab::DataBuilder::Push.build(
|
|
|
|
project,
|
|
|
|
current_user,
|
2016-09-27 03:07:31 +00:00
|
|
|
branch.dereferenced_target.sha,
|
2016-08-12 08:09:29 +00:00
|
|
|
Gitlab::Git::BLANK_SHA,
|
|
|
|
"#{Gitlab::Git::BRANCH_REF_PREFIX}#{branch.name}",
|
|
|
|
[])
|
2015-03-13 13:55:17 +00:00
|
|
|
end
|
2014-05-22 11:23:20 +00:00
|
|
|
end
|