2018-07-05 06:18:17 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-21 04:29:52 -04:00
|
|
|
class DeleteBranchService < BaseService
|
|
|
|
def execute(branch_name)
|
2014-05-22 07:23:20 -04:00
|
|
|
repository = project.repository
|
|
|
|
branch = repository.find_branch(branch_name)
|
|
|
|
|
|
|
|
unless current_user.can?(:push_code, project)
|
2019-04-19 11:39:12 -04:00
|
|
|
return ServiceResponse.error(
|
|
|
|
message: 'You dont have push access to repo',
|
|
|
|
http_status: 405)
|
2014-05-22 07:23:20 -04:00
|
|
|
end
|
|
|
|
|
2017-05-08 03:41:58 -04:00
|
|
|
unless branch
|
2019-04-19 11:39:12 -04:00
|
|
|
return ServiceResponse.error(
|
|
|
|
message: 'No such branch',
|
|
|
|
http_status: 404)
|
2017-05-08 03:41:58 -04:00
|
|
|
end
|
|
|
|
|
2015-11-25 19:20:40 -05:00
|
|
|
if repository.rm_branch(current_user, branch_name)
|
2019-04-19 11:39:12 -04:00
|
|
|
ServiceResponse.success(message: 'Branch was deleted')
|
2014-05-22 07:23:20 -04:00
|
|
|
else
|
2019-04-19 11:39:12 -04:00
|
|
|
ServiceResponse.error(
|
|
|
|
message: 'Failed to remove branch',
|
|
|
|
http_status: 400)
|
2014-05-22 07:23:20 -04:00
|
|
|
end
|
2018-06-11 06:42:09 -04:00
|
|
|
rescue Gitlab::Git::PreReceiveError => ex
|
2019-04-19 11:39:12 -04:00
|
|
|
ServiceResponse.error(message: ex.message, http_status: 400)
|
2014-05-22 07:23:20 -04:00
|
|
|
end
|
|
|
|
end
|