regex.rb 4.02 KB
Newer Older
1 2
# frozen_string_literal: true

3 4 5 6
module Gitlab
  module Regex
    extend self

7
    def project_name_regex
8
      @project_name_regex ||= /\A[\p{Alnum}\u{00A9}-\u{1f9ff}_][\p{Alnum}\p{Pd}\u{00A9}-\u{1f9ff}_\. ]*\z/.freeze
9 10
    end

Douwe Maan's avatar
Douwe Maan committed
11
    def project_name_regex_message
12 13
      "can contain only letters, digits, emojis, '_', '.', dash, space. " \
      "It must start with letter, digit, emoji or '_'."
14 15
    end

16
    ##
17 18 19
    # Docker Distribution Registry repository / tag name rules
    #
    # See https://github.com/docker/distribution/blob/master/reference/regexp.go.
20 21
    #
    def container_repository_name_regex
22
      @container_repository_regex ||= %r{\A[a-z0-9]+((?:[._/]|__|[-]{0,10})[a-z0-9]+)*\Z}
23 24
    end

25 26 27 28
    ##
    # We do not use regexp anchors here because these are not allowed when
    # used as a routing constraint.
    #
29 30 31 32
    def container_registry_tag_regex
      @container_registry_tag_regex ||= /[\w][\w.-]{0,127}/
    end

Lin Jen-Shin's avatar
Lin Jen-Shin committed
33
    def environment_name_regex_chars
34
      'a-zA-Z0-9_/\\$\\{\\}\\. \\-'
Lin Jen-Shin's avatar
Lin Jen-Shin committed
35 36
    end

37 38 39 40
    def environment_name_regex_chars_without_slash
      'a-zA-Z0-9_\\$\\{\\}\\. -'
    end

41
    def environment_name_regex
42
      @environment_name_regex ||= /\A[#{environment_name_regex_chars_without_slash}]([#{environment_name_regex_chars}]*[#{environment_name_regex_chars_without_slash}])?\z/.freeze
43 44 45
    end

    def environment_name_regex_message
46
      "can contain only letters, digits, '-', '_', '/', '$', '{', '}', '.', and spaces, but it cannot start or end with '/'"
47
    end
48

49 50 51 52 53 54 55 56 57 58 59 60
    def environment_scope_regex_chars
      "#{environment_name_regex_chars}\\*"
    end

    def environment_scope_regex
      @environment_scope_regex ||= /\A[#{environment_scope_regex_chars}]+\z/.freeze
    end

    def environment_scope_regex_message
      "can contain only letters, digits, '-', '_', '/', '$', '{', '}', '.', '*' and spaces"
    end

61 62 63 64 65
    def kubernetes_namespace_regex
      /\A[a-z0-9]([-a-z0-9]*[a-z0-9])?\z/
    end

    def kubernetes_namespace_regex_message
66 67
      "can contain only lowercase letters, digits, and '-'. " \
      "Must start with a letter, and cannot end with '-'"
68
    end
Nick Thomas's avatar
Nick Thomas committed
69 70 71 72 73 74 75 76 77

    def environment_slug_regex
      @environment_slug_regex ||= /\A[a-z]([a-z0-9-]*[a-z0-9])?\z/.freeze
    end

    def environment_slug_regex_message
      "can contain only lowercase letters, digits, and '-'. " \
      "Must start with a letter, and cannot end with '-'"
    end
78 79

    def build_trace_section_regex
80
      @build_trace_section_regexp ||= /section_((?:start)|(?:end)):(\d+):([a-zA-Z0-9_.-]+)\r\033\[0K/.freeze
81
    end
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

    def markdown_code_or_html_blocks
      @markdown_code_or_html_blocks ||= %r{
          (?<code>
            # Code blocks:
            # ```
            # Anything, including `>>>` blocks which are ignored by this filter
            # ```

            ^```
            .+?
            \n```\ *$
          )
        |
          (?<html>
            # HTML block:
            # <tag>
            # Anything, including `>>>` blocks which are ignored by this filter
            # </tag>

            ^<[^>]+?>\ *\n
            .+?
            \n<\/[^>]+?>\ *$
          )
      }mx
    end
Felipe's avatar
Felipe committed
108

109 110 111 112 113 114
    # Based on Jira's project key format
    # https://confluence.atlassian.com/adminjiraserver073/changing-the-project-key-format-861253229.html
    def jira_issue_key_regex
      @jira_issue_key_regex ||= /[A-Z][A-Z_0-9]+-\d+/
    end

Felipe's avatar
Felipe committed
115 116 117
    def jira_transition_id_regex
      @jira_transition_id_regex ||= /\d+/
    end
Massimeddu Cireddu's avatar
Massimeddu Cireddu committed
118 119 120 121

    def breakline_regex
      @breakline_regex ||= /\r\n|\r|\n/
    end
122

123 124 125 126 127 128 129 130 131
    # https://docs.aws.amazon.com/general/latest/gr/acct-identifiers.html
    def aws_account_id_regex
      /\A\d{12}\z/
    end

    def aws_account_id_message
      'must be a 12-digit number'
    end

132 133 134 135 136 137
    # https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html
    def aws_arn_regex
      /\Aarn:\S+\z/
    end

    def aws_arn_regex_message
138
      'must be a valid Amazon Resource Name'
139
    end
140 141 142 143

    def utc_date_regex
      @utc_date_regex ||= /\A[0-9]{4}-[0-9]{2}-[0-9]{2}\z/.freeze
    end
144 145
  end
end
146 147

Gitlab::Regex.prepend_if_ee('EE::Gitlab::Regex')