debian-mirror-gitlab/app/models/protectable_dropdown.rb

40 lines
944 B
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
class ProtectableDropdown
2018-03-17 18:26:18 +05:30
REF_TYPES = %i[branches tags].freeze
2017-08-17 22:00:37 +05:30
def initialize(project, ref_type)
2018-03-17 18:26:18 +05:30
raise ArgumentError, "invalid ref type `#{ref_type}`" unless ref_type.in?(REF_TYPES)
2017-08-17 22:00:37 +05:30
@project = project
@ref_type = ref_type
end
# Tags/branches which are yet to be individually protected
def protectable_ref_names
@protectable_ref_names ||= ref_names - non_wildcard_protected_ref_names
end
def hash
protectable_ref_names.map { |ref_name| { text: ref_name, id: ref_name, title: ref_name } }
end
private
def refs
2018-03-17 18:26:18 +05:30
@project.repository.public_send(@ref_type) # rubocop:disable GitlabSecurity/PublicSend
2017-08-17 22:00:37 +05:30
end
def ref_names
refs.map(&:name)
end
def protections
2018-03-17 18:26:18 +05:30
@project.public_send("protected_#{@ref_type}") # rubocop:disable GitlabSecurity/PublicSend
2017-08-17 22:00:37 +05:30
end
def non_wildcard_protected_ref_names
protections.reject(&:wildcard?).map(&:name)
end
end