Module: Gitlab::Regex::Packages

Included in:
Gitlab::Regex
Defined in:
lib/gitlab/regex.rb

Constant Summary collapse

CONAN_RECIPE_FILES =
%w[conanfile.py conanmanifest.txt conan_sources.tgz conan_export.tgz].freeze
CONAN_PACKAGE_FILES =
%w[conaninfo.txt conanmanifest.txt conan_package.tgz].freeze
API_PATH_REGEX =
%r{^/api/v\d+/(projects/[^/]+/|groups?/[^/]+/-/)?packages/[A-Za-z]+}.freeze

Instance Method Summary collapse

Instance Method Details

#_semver_major_minor_patch_regexObject

These partial semver regexes are intended for use in composing other regexes rather than being used alone.


172
173
174
175
176
# File 'lib/gitlab/regex.rb', line 172

def _semver_major_minor_patch_regex
  @_semver_major_minor_patch_regex ||= /
    #{_semver_major_regex}\.#{_semver_minor_regex}\.#{_semver_patch_regex}
  /x.freeze
end

#_semver_major_regexObject


178
179
180
181
182
# File 'lib/gitlab/regex.rb', line 178

def _semver_major_regex
  @_semver_major_regex ||= /
    (?<major>0|[1-9]\d*)
  /x.freeze
end

#_semver_minor_regexObject


184
185
186
187
188
# File 'lib/gitlab/regex.rb', line 184

def _semver_minor_regex
  @_semver_minor_regex ||= /
    (?<minor>0|[1-9]\d*)
  /x.freeze
end

#_semver_patch_regexObject


190
191
192
193
194
# File 'lib/gitlab/regex.rb', line 190

def _semver_patch_regex
  @_semver_patch_regex ||= /
    (?<patch>0|[1-9]\d*)
  /x.freeze
end

#_semver_prerelease_build_regexObject


196
197
198
199
200
201
# File 'lib/gitlab/regex.rb', line 196

def _semver_prerelease_build_regex
  @_semver_prerelease_build_regex ||= /
    (?:-(?<prerelease>(?:\d*[a-zA-Z-][0-9a-zA-Z-]*|[1-9]\d*|0)(?:\.(?:\d*[a-zA-Z-][0-9a-zA-Z-]*|[1-9]\d*|0))*))?
    (?:\+(?<build>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?
  /x.freeze
end

#composer_dev_version_regexObject


33
34
35
# File 'lib/gitlab/regex.rb', line 33

def composer_dev_version_regex
  @composer_dev_version_regex ||= %r{(^dev-)|(-dev$)}.freeze
end

#composer_package_version_regexObject


28
29
30
31
# File 'lib/gitlab/regex.rb', line 28

def composer_package_version_regex
  # see https://github.com/composer/semver/blob/31f3ea725711245195f62e54ffa402d8ef2fdba9/src/VersionParser.php#L215
  @composer_package_version_regex ||= %r{\Av?((\d++)(\.(?:\d++|[xX*]))?(\.(?:\d++|[xX*]))?(\.(?:\d++|[xX*]))?)?\z}.freeze
end

#conan_package_reference_regexObject


11
12
13
# File 'lib/gitlab/regex.rb', line 11

def conan_package_reference_regex
  @conan_package_reference_regex ||= %r{\A[A-Za-z0-9]+\z}.freeze
end

#conan_recipe_component_regexObject


23
24
25
26
# File 'lib/gitlab/regex.rb', line 23

def conan_recipe_component_regex
  # https://docs.conan.io/en/latest/reference/conanfile/attributes.html#name
  @conan_recipe_component_regex ||= %r{\A#{conan_name_regex}\z}.freeze
end

#conan_recipe_user_channel_regexObject


19
20
21
# File 'lib/gitlab/regex.rb', line 19

def conan_recipe_user_channel_regex
  %r{\A(_|#{conan_name_regex})\z}.freeze
end

#conan_revision_regexObject


15
16
17
# File 'lib/gitlab/regex.rb', line 15

def conan_revision_regex
  @conan_revision_regex ||= %r{\A0\z}.freeze
end

#debian_architecture_regexObject


128
129
130
131
132
# File 'lib/gitlab/regex.rb', line 128

def debian_architecture_regex
  # See official parser: https://git.dpkg.org/cgit/dpkg/dpkg.git/tree/lib/dpkg/arch.c?id=9e0c88ec09475f4d1addde9cdba1ad7849720356#n43
  # But we limit to lower case
  @debian_architecture_regex ||= %r{\A#{::Packages::Debian::ARCHITECTURE_REGEX}\z}.freeze
end

#debian_component_regexObject


138
139
140
# File 'lib/gitlab/regex.rb', line 138

def debian_component_regex
  @debian_component_regex ||= %r{\A#{::Packages::Debian::COMPONENT_REGEX}\z}.freeze
end

#debian_distribution_regexObject


134
135
136
# File 'lib/gitlab/regex.rb', line 134

def debian_distribution_regex
  @debian_distribution_regex ||= %r{\A#{::Packages::Debian::DISTRIBUTION_REGEX}\z}i.freeze
end

#debian_package_name_regexObject


109
110
111
112
113
114
115
116
# File 'lib/gitlab/regex.rb', line 109

def debian_package_name_regex
  # See official parser
  # https://git.dpkg.org/cgit/dpkg/dpkg.git/tree/lib/dpkg/parsehelp.c?id=9e0c88ec09475f4d1addde9cdba1ad7849720356#n122
  # @debian_package_name_regex ||= %r{\A[a-z0-9][-+\._a-z0-9]*\z}i.freeze
  # But we prefer a more strict version from Lintian
  # https://salsa.debian.org/lintian/lintian/-/blob/5080c0068ffc4a9ddee92022a91d0c2ff53e56d1/lib/Lintian/Util.pm#L116
  @debian_package_name_regex ||= %r{\A[a-z0-9][-+\.a-z0-9]+\z}.freeze
end

#debian_version_regexObject


118
119
120
121
122
123
124
125
126
# File 'lib/gitlab/regex.rb', line 118

def debian_version_regex
  # See official parser: https://git.dpkg.org/cgit/dpkg/dpkg.git/tree/lib/dpkg/parsehelp.c?id=9e0c88ec09475f4d1addde9cdba1ad7849720356#n205
  @debian_version_regex ||= %r{
    \A(?:
      (?:([0-9]{1,9}):)?    (?# epoch)
      ([0-9][0-9a-z\.+~-]*)  (?# version)
      (?:(-[0-0a-z\.+~]+))?  (?# revision)
      )\z}xi.freeze
end

#generic_package_file_name_regexObject


236
237
238
# File 'lib/gitlab/regex.rb', line 236

def generic_package_file_name_regex
  generic_package_name_regex
end

#generic_package_name_regexObject


232
233
234
# File 'lib/gitlab/regex.rb', line 232

def generic_package_name_regex
  maven_file_name_regex
end

#generic_package_version_regexObject


228
229
230
# File 'lib/gitlab/regex.rb', line 228

def generic_package_version_regex
  maven_version_regex
end

#go_package_regexObject


208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/gitlab/regex.rb', line 208

def go_package_regex
  # A Go package name looks like a URL but is not; it:
  #   - Must not have a scheme, such as http:// or https://
  #   - Must not have a port number, such as :8080 or :8443

  @go_package_regex ||= %r{
    \b (?# word boundary)
    (?<domain>
      [0-9a-z](?:(?:-|[0-9a-z]){0,61}[0-9a-z])? (?# first domain)
      (?:\.[0-9a-z](?:(?:-|[0-9a-z]){0,61}[0-9a-z])?)* (?# inner domains)
      \.[a-z]{2,} (?# top-level domain)
    )
    (?<path>/(?:
      [-/$_.+!*'(),0-9a-z] (?# plain URL character)
      | %[0-9a-f]{2})* (?# URL encoded character)
    )? (?# path)
    \b (?# word boundary)
  }ix.freeze
end

#helm_channel_regexObject


142
143
144
# File 'lib/gitlab/regex.rb', line 142

def helm_channel_regex
  @helm_channel_regex ||= %r{\A([a-zA-Z0-9](\.|-|_)?){1,255}(?<!\.|-|_)\z}.freeze
end

#helm_package_regexObject


146
147
148
# File 'lib/gitlab/regex.rb', line 146

def helm_package_regex
  @helm_package_regex ||= %r{#{helm_channel_regex}}.freeze
end

#helm_version_regexObject


150
151
152
153
# File 'lib/gitlab/regex.rb', line 150

def helm_version_regex
  # identical to semver_regex, with optional preceding 'v'
  @helm_version_regex ||= Regexp.new("\\Av?#{::Gitlab::Regex.unbounded_semver_regex.source}\\z", ::Gitlab::Regex.unbounded_semver_regex.options)
end

#maven_app_group_regexObject


68
69
70
# File 'lib/gitlab/regex.rb', line 68

def maven_app_group_regex
  maven_app_name_regex
end

#maven_app_name_regexObject


60
61
62
# File 'lib/gitlab/regex.rb', line 60

def maven_app_name_regex
  @maven_app_name_regex ||= /\A[\w\-\.]+\z/.freeze
end

#maven_file_name_regexObject


52
53
54
# File 'lib/gitlab/regex.rb', line 52

def maven_file_name_regex
  @maven_file_name_regex ||= %r{\A[A-Za-z0-9\.\_\-\+]+\z}.freeze
end

#maven_path_regexObject


56
57
58
# File 'lib/gitlab/regex.rb', line 56

def maven_path_regex
  @maven_path_regex ||= %r{\A\@?(([\w\-\.]*)/)*([\w\-\.\+]*)\z}.freeze
end

#maven_version_regexObject


64
65
66
# File 'lib/gitlab/regex.rb', line 64

def maven_version_regex
  @maven_version_regex ||= /\A(?!.*\.\.)[\w+.-]+\z/.freeze
end

#npm_package_name_regexObject


72
73
74
# File 'lib/gitlab/regex.rb', line 72

def npm_package_name_regex
  @npm_package_name_regex ||= %r{\A(?:@(#{Gitlab::PathRegex::NAMESPACE_FORMAT_REGEX})/)?[-+\.\_a-zA-Z0-9]+\z}
end

#nuget_package_name_regexObject


76
77
78
# File 'lib/gitlab/regex.rb', line 76

def nuget_package_name_regex
  @nuget_package_name_regex ||= %r{\A[-+\.\_a-zA-Z0-9]+\z}.freeze
end

#nuget_version_regexObject


80
81
82
83
84
85
86
87
88
# File 'lib/gitlab/regex.rb', line 80

def nuget_version_regex
  @nuget_version_regex ||= /
    \A#{_semver_major_regex}
    \.#{_semver_minor_regex}
    (\.#{_semver_patch_regex})?
    (\.\d*)?
    #{_semver_prerelease_build_regex}\z
  /x.freeze
end

#package_name_regexObject


37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gitlab/regex.rb', line 37

def package_name_regex
  @package_name_regex ||=
    %r{
        \A\@?
        (?> # atomic group to prevent backtracking
          (([\w\-\.\+]*)\/)*([\w\-\.]+)
        )
        @?
        (?> # atomic group to prevent backtracking
          (([\w\-\.\+]*)\/)*([\w\-\.]*)
        )
        \z
      }x.freeze
end

#prefixed_semver_regexObject


203
204
205
206
# File 'lib/gitlab/regex.rb', line 203

def prefixed_semver_regex
  # identical to semver_regex, except starting with 'v'
  @prefixed_semver_regex ||= Regexp.new("\\Av#{::Gitlab::Regex.unbounded_semver_regex.source}\\z", ::Gitlab::Regex.unbounded_semver_regex.options)
end

#pypi_version_regexObject


94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/gitlab/regex.rb', line 94

def pypi_version_regex
  # See the official regex: https://github.com/pypa/packaging/blob/16.7/packaging/version.py#L159

  @pypi_version_regex ||= %r{
    \A(?:
      v?
      (?:([0-9]+)!)?                                                 (?# epoch)
      ([0-9]+(?:\.[0-9]+)*)                                          (?# release segment)
      ([-_\.]?((a|b|c|rc|alpha|beta|pre|preview))[-_\.]?([0-9]+)?)?  (?# pre-release)
      ((?:-([0-9]+))|(?:[-_\.]?(post|rev|r)[-_\.]?([0-9]+)?))?       (?# post release)
      ([-_\.]?(dev)[-_\.]?([0-9]+)?)?                                (?# dev release)
      (?:\+([a-z0-9]+(?:[-_\.][a-z0-9]+)*))?                         (?# local version)
      )\z}xi.freeze
end

#semver_regexObject


166
167
168
# File 'lib/gitlab/regex.rb', line 166

def semver_regex
  @semver_regex ||= Regexp.new("\\A#{::Gitlab::Regex.unbounded_semver_regex.source}\\z", ::Gitlab::Regex.unbounded_semver_regex.options).freeze
end

#sha256_regexObject


240
241
242
# File 'lib/gitlab/regex.rb', line 240

def sha256_regex
  @sha256_regex ||= /\A[0-9a-f]{64}\z/i.freeze
end

#terraform_module_package_name_regexObject


90
91
92
# File 'lib/gitlab/regex.rb', line 90

def terraform_module_package_name_regex
  @terraform_module_package_name_regex ||= %r{\A[-a-z0-9]+\/[-a-z0-9]+\z}.freeze
end

#unbounded_semver_regexObject


155
156
157
158
159
160
161
162
163
164
# File 'lib/gitlab/regex.rb', line 155

def unbounded_semver_regex
  # See the official regex: https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string

  # The order of the alternatives in <prerelease> are intentionally
  # reordered to be greedy. Without this change, the unbounded regex would
  # only partially match "v0.0.0-20201230123456-abcdefabcdef".
  @unbounded_semver_regex ||= /
    #{_semver_major_minor_patch_regex}#{_semver_prerelease_build_regex}
  /x.freeze
end