diff options
Diffstat (limited to 'lib/gitlab/ci/environment_matcher.rb')
-rw-r--r-- | lib/gitlab/ci/environment_matcher.rb | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/lib/gitlab/ci/environment_matcher.rb b/lib/gitlab/ci/environment_matcher.rb new file mode 100644 index 00000000000..7d7a7742f68 --- /dev/null +++ b/lib/gitlab/ci/environment_matcher.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +module Gitlab + module Ci + class EnvironmentMatcher + def initialize(pattern) + @pattern = pattern + end + + def match?(environment) + return false if pattern.blank? + + exact_match?(environment) || wildcard_match?(environment) + end + + private + + attr_reader :pattern, :match_type + + def exact_match?(environment) + pattern == environment + end + + def wildcard_match?(environment) + return false unless wildcard? + + wildcard_regex.match?(environment) + end + + def wildcard? + pattern.include?('*') + end + + def wildcard_regex + @wildcard_regex ||= Regexp.new(pattern.gsub('*', '.*')) + end + end + end +end |