BigW Consortium Gitlab

protectable_dropdown.rb 913 Bytes
Newer Older
1
class ProtectableDropdown
2 3
  REF_TYPES = %i[branches tags].freeze

4
  def initialize(project, ref_type)
5 6
    raise ArgumentError, "invalid ref type `#{ref_type}`" unless ref_type.in?(REF_TYPES)

7 8 9 10 11 12
    @project = project
    @ref_type = ref_type
  end

  # Tags/branches which are yet to be individually protected
  def protectable_ref_names
13
    @protectable_ref_names ||= ref_names - non_wildcard_protected_ref_names
14 15 16 17 18 19 20 21 22
  end

  def hash
    protectable_ref_names.map { |ref_name| { text: ref_name, id: ref_name, title: ref_name } }
  end

  private

  def refs
23
    @project.repository.public_send(@ref_type) # rubocop:disable GitlabSecurity/PublicSend
24 25
  end

26 27 28 29
  def ref_names
    refs.map(&:name)
  end

30
  def protections
31
    @project.public_send("protected_#{@ref_type}") # rubocop:disable GitlabSecurity/PublicSend
32
  end
33 34 35 36

  def non_wildcard_protected_ref_names
    protections.reject(&:wildcard?).map(&:name)
  end
37
end