summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/pipeline/expression/lexeme/matches.rb
blob: ecfab62722640d1fee62d3b52084a6b3dc0a0dc5 (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# frozen_string_literal: true

module Gitlab
  module Ci
    module Pipeline
      module Expression
        module Lexeme
          class Matches < Lexeme::Operator
            PATTERN = /=~/.freeze

            def evaluate(variables = {})
              text = @left.evaluate(variables)
              regexp = @right.evaluate(variables)

              regexp.scan(text.to_s).any?

              if ci_variables_complex_expressions?
                # return offset of first match, or nil if no matches
                if match = regexp.scan(text.to_s).first
                  text.to_s.index(match)
                end
              else
                # return true or false
                regexp.scan(text.to_s).any?
              end
            end

            def self.build(_value, behind, ahead)
              new(behind, ahead)
            end

            def self.precedence
              10 # See: https://ruby-doc.org/core-2.5.0/doc/syntax/precedence_rdoc.html
            end

            private

            def ci_variables_complex_expressions?
              Feature.enabled?(:ci_variables_complex_expressions, default_enabled: true)
            end
          end
        end
      end
    end
  end
end