DeleteMergedBranchesService should not delete protected branches

When deleting all the branches that are merged, the protected branches
should not be deleted.
This commit is contained in:
Toon Claes 2017-07-04 11:51:00 +02:00
parent f8973d76bd
commit e0f106ae97
3 changed files with 14 additions and 0 deletions

View file

@ -10,6 +10,8 @@ class DeleteMergedBranchesService < BaseService
branches = branches.select { |branch| project.repository.merged_to_root_ref?(branch) } branches = branches.select { |branch| project.repository.merged_to_root_ref?(branch) }
# Prevent deletion of branches relevant to open merge requests # Prevent deletion of branches relevant to open merge requests
branches -= merge_request_branch_names branches -= merge_request_branch_names
# Prevent deletion of protected branches
branches -= project.protected_branches.pluck(:name)
branches.each do |branch| branches.each do |branch|
DeleteBranchService.new(project, current_user).execute(branch) DeleteBranchService.new(project, current_user).execute(branch)

View file

@ -0,0 +1,4 @@
---
title: Do not delete protected branches when deleting all merged branches
merge_request: 12624
author:

View file

@ -24,6 +24,14 @@ describe DeleteMergedBranchesService, services: true do
expect(project.repository.branch_names).to include('master') expect(project.repository.branch_names).to include('master')
end end
it 'keeps protected branches' do
create(:protected_branch, project: project, name: 'improve/awesome')
service.execute
expect(project.repository.branch_names).to include('improve/awesome')
end
context 'user without rights' do context 'user without rights' do
let(:user) { create(:user) } let(:user) { create(:user) }