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

45 lines
1.2 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-04 21:01:54 +05:30
PATTERN = %r{^/.+/[ismU]*$}.freeze
NEW_PATTERN = %r{^\/([^\/]|\\/)+[^\\]\/[ismU]*}.freeze
2018-11-08 19:23:39 +05:30
def initialize(regexp)
2019-09-04 21:01:54 +05:30
@value = self.class.eager_matching_with_escape_characters? ? regexp.gsub(/\\\//, '/') : regexp
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
2019-09-04 21:01:54 +05:30
def self.pattern
eager_matching_with_escape_characters? ? NEW_PATTERN : PATTERN
end
2018-11-08 19:23:39 +05:30
def self.build(string)
new(string)
end
2019-09-04 21:01:54 +05:30
def self.eager_matching_with_escape_characters?
Feature.enabled?(:ci_variables_complex_expressions)
end
2018-11-08 19:23:39 +05:30
end
end
end
end
end
end