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

wildcard_branch_filter_validator.rb « web_hooks « validators « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 12ec78f05dee474f1c5cffddd638145ccff4ff3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# frozen_string_literal: true

# WildcardBranchFilterValidator
#
# Custom validator for wildcard branch filter. Squishes whitespace and ignores
# empty string. This only checks that a string is a valid wildcard git branch
# like "feature/login" and "feature/*". It doesn't check whether a branch already
# exists.
#
# Example:
#
#   class Webhook < ActiveRecord::Base
#     validates :push_events_branch_filter, "web_hooks/wildcard_branch_filter": true
#   end
#
module WebHooks
  class WildcardBranchFilterValidator < ActiveModel::EachValidator
    def validate_each(record, attribute, value)
      value.squish! unless value.nil?

      return unless value.present?

      value_without_wildcards = value.tr('*', 'x')

      unless Gitlab::GitRefValidator.validate(value_without_wildcards)
        record.errors.add(attribute, "is not a valid branch name")
      end

      return if value.length <= 4000

      record.errors.add(attribute, "is longer than the allowed length of 4000 characters.")
    end
  end
end