2018-09-11 09:05:23 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-06-07 07:35:17 +00:00
|
|
|
# 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')
|
2018-09-01 15:55:06 +00:00
|
|
|
|
2018-06-07 07:35:17 +00:00
|
|
|
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
|