summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/pipeline/expression/lexeme/pattern.rb
blob: 53fb5f769d82a12f4f711d9ddc8de3f93ade2dfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
module Gitlab
  module Ci
    module Pipeline
      module Expression
        module Lexeme
          require_dependency 're2'

          class Pattern < Lexeme::Value
            PATTERN = %r{^(?<regexp>/.+/[ismU]*)$}.freeze

            def initialize(regexp)
              @value = regexp
            end

            def evaluate(variables = {})
              Gitlab::UntrustedRegexp.fabricate(@value)
            rescue RegexpError
              raise Expression::RuntimeError, 'Invalid regular expression!'
            end

            def self.build(string)
              new(string.match(PATTERN)[:regexp])
            end
          end
        end
      end
    end
  end
end