Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gitlab/ci/environment_matcher.rb')
-rw-r--r--lib/gitlab/ci/environment_matcher.rb39
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