summaryrefslogtreecommitdiff
path: root/app/validators
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2018-09-05 13:39:41 +0000
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2018-09-05 13:39:41 +0000
commit464b0de1acfc9383a551a05efa8c8a705c8bf70c (patch)
tree6766e8c53a968ed40cd71748c0e0b7d622f8c3df /app/validators
parent97ee68b1750bade46ef6c1d7c813bc917a13d89d (diff)
parent9d742e61a79dcc85589598259e2fdac030b7ac00 (diff)
downloadgitlab-ce-464b0de1acfc9383a551a05efa8c8a705c8bf70c.tar.gz
Merge branch 'filter-web-hooks-by-branch' into 'master'
Filter web hooks by branch See merge request gitlab-org/gitlab-ce!19513
Diffstat (limited to 'app/validators')
-rw-r--r--app/validators/branch_filter_validator.rb35
1 files changed, 35 insertions, 0 deletions
diff --git a/app/validators/branch_filter_validator.rb b/app/validators/branch_filter_validator.rb
new file mode 100644
index 00000000000..ef482aaaa63
--- /dev/null
+++ b/app/validators/branch_filter_validator.rb
@@ -0,0 +1,35 @@
+# BranchFilterValidator
+#
+# Custom validator for branch names. Squishes whitespace and ignores empty
+# string. This only checks that a string is a valid git branch name. It does
+# not check whether a branch already exists.
+#
+# Example:
+#
+# class Webhook < ActiveRecord::Base
+# validates :push_events_branch_filter, branch_name: true
+# end
+#
+class BranchFilterValidator < ActiveModel::EachValidator
+ def validate_each(record, attribute, value)
+ value.squish! unless value.nil?
+
+ if value.present?
+ value_without_wildcards = value.tr('*', 'x')
+
+ unless Gitlab::GitRefValidator.validate(value_without_wildcards)
+ record.errors[attribute] << "is not a valid branch name"
+ end
+
+ unless value.length <= 4000
+ record.errors[attribute] << "is longer than the allowed length of 4000 characters."
+ end
+ end
+ end
+
+ private
+
+ def contains_wildcard?(value)
+ value.include?('*')
+ end
+end