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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

46 lines
1.1 KiB
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
2022-03-02 08:16:31 +05:30
REF_NAME_METHODS = {
branches: :branch_names,
tags: :tag_names
}.freeze
2018-03-17 18:26:18 +05:30
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
2021-03-08 18:12:59 +05:30
return [] if @project.empty_repo?
2017-08-17 22:00:37 +05:30
@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
2022-03-02 08:16:31 +05:30
def ref_names
@project.repository.public_send(ref_name_method) # rubocop:disable GitlabSecurity/PublicSend
2017-08-17 22:00:37 +05:30
end
2022-03-02 08:16:31 +05:30
def ref_name_method
REF_NAME_METHODS[@ref_type]
2017-08-17 22:00:37 +05:30
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