gitlab-org--gitlab-foss/lib/gitlab/regex.rb

113 lines
3 KiB
Ruby
Raw Normal View History

2012-11-27 22:14:05 -05:00
module Gitlab
module Regex
extend self
NAMESPACE_REGEX_STR = '(?:[a-zA-Z0-9_\.][a-zA-Z0-9_\-\.]*[a-zA-Z0-9_\-]|[a-zA-Z0-9_])'.freeze
2015-03-24 10:21:37 -04:00
def namespace_regex
@namespace_regex ||= /\A#{NAMESPACE_REGEX_STR}\z/.freeze
2012-11-27 22:14:05 -05:00
end
def namespace_regex_message
2015-03-24 09:55:14 -04:00
"can contain only letters, digits, '_', '-' and '.'. " \
"Cannot start with '-' or end in '.'." \
2015-03-24 09:55:14 -04:00
end
def namespace_name_regex
@namespace_name_regex ||= /\A[\p{Alnum}\p{Pd}_\. ]*\z/.freeze
end
2015-03-24 09:55:14 -04:00
def namespace_name_regex_message
"can contain only letters, digits, '_', '.', dash and space."
2015-03-24 09:55:14 -04:00
end
2012-12-25 17:50:41 -05:00
def project_name_regex
@project_name_regex ||= /\A[\p{Alnum}_][\p{Alnum}\p{Pd}_\. ]*\z/.freeze
2012-12-25 17:50:41 -05:00
end
2015-03-24 09:55:14 -04:00
def project_name_regex_message
"can contain only letters, digits, '_', '.', dash and space. " \
"It must start with letter, digit or '_'."
end
2015-03-24 09:55:14 -04:00
def project_path_regex
@project_path_regex ||= /\A[a-zA-Z0-9_.][a-zA-Z0-9_\-\.]*(?<!\.git|\.atom)\z/.freeze
end
2015-03-24 09:55:14 -04:00
def project_path_regex_message
"can contain only letters, digits, '_', '-' and '.'. " \
"Cannot start with '-', end in '.git' or end in '.atom'" \
end
2015-03-24 09:55:14 -04:00
def file_name_regex
2016-02-01 10:20:49 -05:00
@file_name_regex ||= /\A[a-zA-Z0-9_\-\.\@]*\z/.freeze
2012-11-27 22:14:05 -05:00
end
2015-03-24 09:55:14 -04:00
def file_name_regex_message
2016-02-01 10:20:49 -05:00
"can contain only letters, digits, '_', '-', '@' and '.'. "
end
def file_path_regex
2016-02-01 10:20:49 -05:00
@file_path_regex ||= /\A[a-zA-Z0-9_\-\.\/\@]*\z/.freeze
end
def file_path_regex_message
2016-02-01 10:20:49 -05:00
"can contain only letters, digits, '_', '-', '@' and '.'. Separate directories with a '/'. "
end
def directory_traversal_regex
@directory_traversal_regex ||= /\.{2}/.freeze
end
def directory_traversal_regex_message
"cannot include directory traversal. "
end
2015-03-24 09:55:14 -04:00
def archive_formats_regex
2015-03-24 09:55:14 -04:00
# |zip|tar| tar.gz | tar.bz2 |
@archive_formats_regex ||= /(zip|tar|tar\.gz|tgz|gz|tar\.bz2|tbz|tbz2|tb2|bz2)/.freeze
end
2012-11-27 22:14:05 -05:00
def git_reference_regex
# Valid git ref regex, see:
# https://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html
2015-03-24 09:55:14 -04:00
@git_reference_regex ||= %r{
(?!
(?# doesn't begins with)
\/| (?# rule #6)
(?# doesn't contain)
.*(?:
[\/.]\.| (?# rule #1,3)
\/\/| (?# rule #6)
@\{| (?# rule #8)
\\ (?# rule #9)
)
)
[^\000-\040\177~^:?*\[]+ (?# rule #4-5)
(?# doesn't end with)
(?<!\.lock) (?# rule #1)
(?<![\/.]) (?# rule #6-7)
2015-03-24 09:55:14 -04:00
}x.freeze
2012-11-27 22:14:05 -05:00
end
2016-04-18 08:14:40 -04:00
2016-05-08 16:50:30 -04:00
def container_registry_reference_regex
2016-04-18 08:14:40 -04:00
git_reference_regex
end
2016-06-14 07:04:21 -04:00
def environment_name_regex
@environment_name_regex ||= /\A[a-zA-Z0-9_-]+\z/.freeze
end
def environment_name_regex_message
"can contain only letters, digits, '-' and '_'."
end
2012-11-27 22:14:05 -05:00
end
end