debian-mirror-gitlab/lib/gitlab/ci/pipeline/expression/lexeme/pattern.rb

44 lines
1 KiB
Ruby
Raw Normal View History

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)
2020-10-24 23:57:45 +05:30
super(regexp.gsub(/\\\//, '/'))
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
end
end
end
end
end
end