2018-07-25 07:01:10 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class RefMatcher
|
|
|
|
def initialize(ref_name_or_pattern)
|
|
|
|
@ref_name_or_pattern = ref_name_or_pattern
|
|
|
|
end
|
|
|
|
|
2022-01-13 13:16:51 -05:00
|
|
|
# Returns all branches/tags (among the given list of refs [`Gitlab::Git::Branch`] or their names [`String`])
|
2018-07-25 07:01:10 -04:00
|
|
|
# that match the current protected ref.
|
|
|
|
def matching(refs)
|
2022-01-13 13:16:51 -05:00
|
|
|
refs.select { |ref| ref.is_a?(String) ? matches?(ref) : matches?(ref.name) }
|
2018-07-25 07:01:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Checks if the protected ref matches the given ref name.
|
|
|
|
def matches?(ref_name)
|
|
|
|
return false if @ref_name_or_pattern.blank?
|
|
|
|
|
|
|
|
exact_match?(ref_name) || wildcard_match?(ref_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Checks if this protected ref contains a wildcard
|
|
|
|
def wildcard?
|
|
|
|
@ref_name_or_pattern && @ref_name_or_pattern.include?('*')
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def exact_match?(ref_name)
|
|
|
|
@ref_name_or_pattern == ref_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def wildcard_match?(ref_name)
|
|
|
|
return false unless wildcard?
|
|
|
|
|
|
|
|
wildcard_regex === ref_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def wildcard_regex
|
|
|
|
@wildcard_regex ||= begin
|
|
|
|
name = @ref_name_or_pattern.gsub('*', 'STAR_DONT_ESCAPE')
|
|
|
|
quoted_name = Regexp.quote(name)
|
|
|
|
regex_string = quoted_name.gsub('STAR_DONT_ESCAPE', '.*?')
|
|
|
|
/\A#{regex_string}\z/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|