2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-03 12:10:58 -04:00
|
|
|
class ProtectableDropdown
|
2017-08-10 12:39:26 -04:00
|
|
|
REF_TYPES = %i[branches tags].freeze
|
2022-01-13 13:16:51 -05:00
|
|
|
REF_NAME_METHODS = {
|
|
|
|
branches: :branch_names,
|
|
|
|
tags: :tag_names
|
|
|
|
}.freeze
|
2017-08-10 12:39:26 -04:00
|
|
|
|
2017-04-03 12:10:58 -04:00
|
|
|
def initialize(project, ref_type)
|
2017-08-10 12:39:26 -04:00
|
|
|
raise ArgumentError, "invalid ref type `#{ref_type}`" unless ref_type.in?(REF_TYPES)
|
|
|
|
|
2017-04-03 12:10:58 -04:00
|
|
|
@project = project
|
|
|
|
@ref_type = ref_type
|
|
|
|
end
|
|
|
|
|
|
|
|
# Tags/branches which are yet to be individually protected
|
|
|
|
def protectable_ref_names
|
2021-01-05 22:10:22 -05:00
|
|
|
return [] if @project.empty_repo?
|
|
|
|
|
2017-04-05 13:59:46 -04:00
|
|
|
@protectable_ref_names ||= ref_names - non_wildcard_protected_ref_names
|
2017-04-03 12:10:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def hash
|
|
|
|
protectable_ref_names.map { |ref_name| { text: ref_name, id: ref_name, title: ref_name } }
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-01-13 13:16:51 -05:00
|
|
|
def ref_names
|
|
|
|
@project.repository.public_send(ref_name_method) # rubocop:disable GitlabSecurity/PublicSend
|
2017-04-03 12:10:58 -04:00
|
|
|
end
|
|
|
|
|
2022-01-13 13:16:51 -05:00
|
|
|
def ref_name_method
|
|
|
|
REF_NAME_METHODS[@ref_type]
|
2017-04-05 13:59:46 -04:00
|
|
|
end
|
|
|
|
|
2017-04-03 12:10:58 -04:00
|
|
|
def protections
|
2017-08-10 12:39:26 -04:00
|
|
|
@project.public_send("protected_#{@ref_type}") # rubocop:disable GitlabSecurity/PublicSend
|
2017-04-03 12:10:58 -04:00
|
|
|
end
|
2017-04-05 13:59:46 -04:00
|
|
|
|
|
|
|
def non_wildcard_protected_ref_names
|
|
|
|
protections.reject(&:wildcard?).map(&:name)
|
|
|
|
end
|
2017-04-03 12:10:58 -04:00
|
|
|
end
|