summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorDuana Saskia <starkcoffee@users.noreply.github.com>2018-07-25 13:01:10 +0200
committerDuana Saskia <starkcoffee@users.noreply.github.com>2018-08-13 13:36:22 +0200
commitc322976032e45f02b60701ebf244a8a876063078 (patch)
treeb5e959931ee397e368b679fb5c0f16976141a619 /app
parentece6a1ea6ecffdbde5ff7d663f1ad1eb74f78aa6 (diff)
downloadgitlab-ce-c322976032e45f02b60701ebf244a8a876063078.tar.gz
Refactor ProtectedRefMatcher to be more generic
Diffstat (limited to 'app')
-rw-r--r--app/models/concerns/protected_ref.rb10
-rw-r--r--app/models/hooks/active_hook_filter.rb28
-rw-r--r--app/models/protected_ref_matcher.rb56
-rw-r--r--app/models/ref_matcher.rb46
4 files changed, 56 insertions, 84 deletions
diff --git a/app/models/concerns/protected_ref.rb b/app/models/concerns/protected_ref.rb
index e62e680af6e..af387c99f3d 100644
--- a/app/models/concerns/protected_ref.rb
+++ b/app/models/concerns/protected_ref.rb
@@ -50,14 +50,20 @@ module ProtectedRef
.map(&:"#{action}_access_levels").flatten
end
+ # Returns all protected refs that match the given ref name.
+ # This checks all records from the scope built up so far, and does
+ # _not_ return a relation.
+ #
+ # This method optionally takes in a list of `protected_refs` to search
+ # through, to avoid calling out to the database.
def matching(ref_name, protected_refs: nil)
- ProtectedRefMatcher.matching(self, ref_name, protected_refs: protected_refs)
+ (protected_refs || self.all).select { |protected_ref| protected_ref.matches?(ref_name) }
end
end
private
def ref_matcher
- @ref_matcher ||= ProtectedRefMatcher.new(self)
+ @ref_matcher ||= RefMatcher.new(self.name)
end
end
diff --git a/app/models/hooks/active_hook_filter.rb b/app/models/hooks/active_hook_filter.rb
index 611af971163..ea046bea368 100644
--- a/app/models/hooks/active_hook_filter.rb
+++ b/app/models/hooks/active_hook_filter.rb
@@ -1,6 +1,7 @@
class ActiveHookFilter
def initialize(hook)
@hook = hook
+ @push_events_filter_matcher = RefMatcher.new(@hook.push_events_branch_filter)
end
def matches?(hooks_scope, data)
@@ -8,31 +9,6 @@ class ActiveHookFilter
return true if @hook.push_events_branch_filter.blank?
branch_name = Gitlab::Git.branch_name(data[:ref])
- exact_match?(branch_name) || wildcard_match?(branch_name)
- end
-
- private
-
- def exact_match?(branch_name)
- @hook.push_events_branch_filter == branch_name
- end
-
- def wildcard_match?(branch_name)
- return false unless wildcard?
-
- wildcard_regex === branch_name
- end
-
- def wildcard_regex
- @wildcard_regex ||= begin
- name = @hook.push_events_branch_filter.gsub('*', 'STAR_DONT_ESCAPE')
- quoted_name = Regexp.quote(name)
- regex_string = quoted_name.gsub('STAR_DONT_ESCAPE', '.*?')
- /\A#{regex_string}\z/
- end
- end
-
- def wildcard?
- @hook.push_events_branch_filter && @hook.push_events_branch_filter.include?('*')
+ @push_events_filter_matcher.matches?(branch_name)
end
end
diff --git a/app/models/protected_ref_matcher.rb b/app/models/protected_ref_matcher.rb
deleted file mode 100644
index bfa9180ac93..00000000000
--- a/app/models/protected_ref_matcher.rb
+++ /dev/null
@@ -1,56 +0,0 @@
-# frozen_string_literal: true
-
-class ProtectedRefMatcher
- def initialize(protected_ref)
- @protected_ref = protected_ref
- end
-
- # Returns all protected refs that match the given ref name.
- # This checks all records from the scope built up so far, and does
- # _not_ return a relation.
- #
- # This method optionally takes in a list of `protected_refs` to search
- # through, to avoid calling out to the database.
- def self.matching(type, ref_name, protected_refs: nil)
- (protected_refs || type.all).select { |protected_ref| protected_ref.matches?(ref_name) }
- end
-
- # Returns all branches/tags (among the given list of refs [`Gitlab::Git::Branch`])
- # that match the current protected ref.
- def matching(refs)
- refs.select { |ref| @protected_ref.matches?(ref.name) }
- end
-
- # Checks if the protected ref matches the given ref name.
- def matches?(ref_name)
- return false if @protected_ref.name.blank?
-
- exact_match?(ref_name) || wildcard_match?(ref_name)
- end
-
- # Checks if this protected ref contains a wildcard
- def wildcard?
- @protected_ref.name && @protected_ref.name.include?('*')
- end
-
- protected
-
- def exact_match?(ref_name)
- @protected_ref.name == ref_name
- end
-
- def wildcard_match?(ref_name)
- return false unless wildcard?
-
- wildcard_regex === ref_name
- end
-
- def wildcard_regex
- @wildcard_regex ||= begin
- name = @protected_ref.name.gsub('*', 'STAR_DONT_ESCAPE')
- quoted_name = Regexp.quote(name)
- regex_string = quoted_name.gsub('STAR_DONT_ESCAPE', '.*?')
- /\A#{regex_string}\z/
- end
- end
-end
diff --git a/app/models/ref_matcher.rb b/app/models/ref_matcher.rb
new file mode 100644
index 00000000000..fa7d2c0f06c
--- /dev/null
+++ b/app/models/ref_matcher.rb
@@ -0,0 +1,46 @@
+# frozen_string_literal: true
+
+class RefMatcher
+ def initialize(ref_name_or_pattern)
+ @ref_name_or_pattern = ref_name_or_pattern
+ end
+
+ # Returns all branches/tags (among the given list of refs [`Gitlab::Git::Branch`])
+ # that match the current protected ref.
+ def matching(refs)
+ refs.select { |ref| matches?(ref.name) }
+ 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