protected_branch.rb 1 KB
Newer Older
1 2
# frozen_string_literal: true

3
class ProtectedBranch < ActiveRecord::Base
4
  include ProtectedRef
5

6
  protected_ref_access_levels :merge, :push
7

8
  def self.protected_ref_accessible_to?(ref, user, project:, action:, protected_refs: nil)
Mark Chao's avatar
Mark Chao committed
9
    # Maintainers, owners and admins are allowed to create the default branch
10 11 12 13 14 15 16
    if default_branch_protected? && project.empty_repo?
      return true if user.admin? || project.team.max_member_access(user.id) > Gitlab::Access::DEVELOPER
    end

    super
  end

17 18
  # Check if branch name is marked as protected in the system
  def self.protected?(project, ref_name)
19
    return true if project.empty_repo? && default_branch_protected?
20

21 22 23
    refs = project.protected_branches.select(:name)

    self.matching(ref_name, protected_refs: refs).present?
24
  end
25 26

  def self.default_branch_protected?
27 28
    Gitlab::CurrentSettings.default_branch_protection == Gitlab::Access::PROTECTION_FULL ||
      Gitlab::CurrentSettings.default_branch_protection == Gitlab::Access::PROTECTION_DEV_CAN_MERGE
29
  end
30
end