2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
module Pipeline
|
|
|
|
module Expression
|
|
|
|
module Lexeme
|
|
|
|
require_dependency 're2'
|
|
|
|
|
|
|
|
class Pattern < Lexeme::Value
|
2019-09-30 21:07:59 +05:30
|
|
|
PATTERN = %r{^\/([^\/]|\\/)+[^\\]\/[ismU]*}.freeze
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
def initialize(regexp)
|
2021-10-27 15:23:28 +05:30
|
|
|
super(regexp.gsub(%r{\\/}, '/'))
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2019-04-03 18:18:56 +05:30
|
|
|
unless Gitlab::UntrustedRegexp::RubySyntax.valid?(@value)
|
2018-11-08 19:23:39 +05:30
|
|
|
raise Lexer::SyntaxError, 'Invalid regular expression!'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def evaluate(variables = {})
|
2019-04-03 18:18:56 +05:30
|
|
|
Gitlab::UntrustedRegexp::RubySyntax.fabricate!(@value)
|
2018-11-08 19:23:39 +05:30
|
|
|
rescue RegexpError
|
|
|
|
raise Expression::RuntimeError, 'Invalid regular expression!'
|
|
|
|
end
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
def inspect
|
|
|
|
"/#{value}/"
|
|
|
|
end
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
def self.pattern
|
2019-09-30 21:07:59 +05:30
|
|
|
PATTERN
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def self.build(string)
|
|
|
|
new(string)
|
|
|
|
end
|
2022-07-16 23:28:13 +05:30
|
|
|
|
|
|
|
def self.build_and_evaluate(data, variables = {})
|
|
|
|
return data if data.is_a?(Gitlab::UntrustedRegexp)
|
|
|
|
|
|
|
|
begin
|
|
|
|
new_pattern = build(data)
|
|
|
|
rescue Lexer::SyntaxError
|
|
|
|
return data
|
|
|
|
end
|
|
|
|
|
|
|
|
new_pattern.evaluate(variables)
|
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|