gitlab-org--gitlab-foss/app/models/protected_branch.rb
Yorick Peterse a0527ab806
Only load branch names for protected branch checks
When checking if a branch is protected we don't need all columns of
every protected branch row, instead we only care about the names. By
using "select" here we reduce the amount of data we need to send over
the wire and load into memory.
2017-11-29 16:54:54 +01:00

22 lines
717 B
Ruby

class ProtectedBranch < ActiveRecord::Base
include Gitlab::ShellAdapter
include ProtectedRef
extend Gitlab::CurrentSettings
protected_ref_access_levels :merge, :push
# Check if branch name is marked as protected in the system
def self.protected?(project, ref_name)
return true if project.empty_repo? && default_branch_protected?
refs = project.protected_branches.select(:name)
self.matching(ref_name, protected_refs: refs).present?
end
def self.default_branch_protected?
current_application_settings.default_branch_protection == Gitlab::Access::PROTECTION_FULL ||
current_application_settings.default_branch_protection == Gitlab::Access::PROTECTION_DEV_CAN_MERGE
end
end