gitlab-org--gitlab-foss/spec/services/delete_merged_branches_service_spec.rb
Toon Claes ca3c868567 When deleting merged branches, ignore protected tags
In gitlab-org/gitlab-ce!13251 wildcard Protected Branches were handled
properly when deleting all merged branches. But this fix wasn't that
good. It also checked branch names against Protected Tags. That's not
correct.

This change will **only** check if there is a Protected Branch
matching the merged branch, and ignores Protected Tags.

Closes gitlab-org/gitlab-ce#39732.
2017-11-07 16:58:36 +01:00

82 lines
2.3 KiB
Ruby

require 'spec_helper'
describe DeleteMergedBranchesService do
include ProjectForksHelper
subject(:service) { described_class.new(project, project.owner) }
let(:project) { create(:project, :repository) }
context '#execute' do
it 'deletes a branch that was merged' do
service.execute
expect(project.repository.branch_names).not_to include('improve/awesome')
end
it 'keeps branch that is unmerged' do
service.execute
expect(project.repository.branch_names).to include('feature')
end
it 'keeps "master"' do
service.execute
expect(project.repository.branch_names).to include('master')
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
it 'keeps wildcard protected branches' do
create(:protected_branch, project: project, name: 'improve/*')
service.execute
expect(project.repository.branch_names).to include('improve/awesome')
end
it 'ignores protected tags' do
create(:protected_tag, project: project, name: 'improve/*')
service.execute
expect(project.repository.branch_names).not_to include('improve/awesome')
end
context 'user without rights' do
let(:user) { create(:user) }
it 'cannot execute' do
expect { described_class.new(project, user).execute }.to raise_error(Gitlab::Access::AccessDeniedError)
end
end
context 'open merge requests' do
it 'does not delete branches from open merge requests' do
forked_project = fork_project(project)
create(:merge_request, :opened, source_project: project, target_project: project, source_branch: 'branch-merged', target_branch: 'master')
create(:merge_request, :opened, source_project: forked_project, target_project: project, target_branch: 'improve/awesome', source_branch: 'master')
service.execute
expect(project.repository.branch_names).to include('branch-merged')
expect(project.repository.branch_names).to include('improve/awesome')
end
end
end
context '#async_execute' do
it 'calls DeleteMergedBranchesWorker async' do
expect(DeleteMergedBranchesWorker).to receive(:perform_async)
service.async_execute
end
end
end