BigW Consortium Gitlab

protected_ref.rb 1.01 KB
Newer Older
1 2 3 4 5
module ProtectedRef
  extend ActiveSupport::Concern

  included do
    belongs_to :project
6

7 8 9
    validates :name, presence: true
    validates :project, presence: true

10 11
    delegate :matching, :matches?, :wildcard?, to: :ref_matcher

12
    def self.protected_ref_accessible_to?(ref, user, action:)
13
      access_levels_for_ref(ref, action: action).any? do |access_level|
14 15 16 17
        access_level.check_access(user)
      end
    end

18 19 20 21 22 23
    def self.developers_can?(action, ref)
      access_levels_for_ref(ref, action: action).any? do |access_level|
        access_level.access_level == Gitlab::Access::DEVELOPER
      end
    end

24
    def self.access_levels_for_ref(ref, action:)
25
      self.matching(ref).map(&:"#{action}_access_levels").flatten
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    end

    def self.matching(ref_name, protected_refs: nil)
      ProtectedRefMatcher.matching(self, ref_name, protected_refs: protected_refs)
    end
  end

  def commit
    project.commit(self.name)
  end

  private

  def ref_matcher
    @ref_matcher ||= ProtectedRefMatcher.new(self)
  end
end