Merge branch 'check-protected-branches' into 'master'

Cleaned up/tweaked Project#open_branches

See commit c825404572040a3a45cb9e2b3d3e7d64900f66c9 for the details of the changes and https://gitlab.com/gitlab-org/gitlab-ce/issues/14280#note_4973648 for more information.

See merge request !3985
This commit is contained in:
Robert Speicher 2016-04-29 21:45:15 +00:00
commit 11773f3fc9
3 changed files with 23 additions and 10 deletions

View file

@ -1,6 +1,7 @@
Please view this file on the master branch, on stable branches it's out of date. Please view this file on the master branch, on stable branches it's out of date.
v 8.8.0 (unreleased) v 8.8.0 (unreleased)
- Project#open_branches has been cleaned up and no longer loads entire records into memory.
- Make build status canceled if any of the jobs was canceled and none failed - Make build status canceled if any of the jobs was canceled and none failed
- Remove future dates from contribution calendar graph. - Remove future dates from contribution calendar graph.
- Use ActionDispatch Remote IP for Akismet checking - Use ActionDispatch Remote IP for Akismet checking

View file

@ -735,19 +735,17 @@ class Project < ActiveRecord::Base
end end
def open_branches def open_branches
all_branches = repository.branches # We're using a Set here as checking values in a large Set is faster than
# checking values in a large Array.
protected_set = Set.new(protected_branch_names)
if protected_branches.present? repository.branches.reject do |branch|
all_branches.reject! do |branch| protected_set.include?(branch.name)
protected_branches_names.include?(branch.name)
end end
end end
all_branches def protected_branch_names
end @protected_branch_names ||= protected_branches.pluck(:name)
def protected_branches_names
@protected_branches_names ||= protected_branches.map(&:name)
end end
def root_ref?(branch) def root_ref?(branch)
@ -764,7 +762,7 @@ class Project < ActiveRecord::Base
# Check if current branch name is marked as protected in the system # Check if current branch name is marked as protected in the system
def protected_branch?(branch_name) def protected_branch?(branch_name)
protected_branches_names.include?(branch_name) protected_branches.where(name: branch_name).any?
end end
def developers_can_push_to_protected_branch?(branch_name) def developers_can_push_to_protected_branch?(branch_name)

View file

@ -798,4 +798,18 @@ describe Project, models: true do
end end
end end
end end
describe '#protected_branch?' do
let(:project) { create(:empty_project) }
it 'returns true when a branch is a protected branch' do
project.protected_branches.create!(name: 'foo')
expect(project.protected_branch?('foo')).to eq(true)
end
it 'returns false when a branch is not a protected branch' do
expect(project.protected_branch?('foo')).to eq(false)
end
end
end end