Commit 65f4e7b2 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add support for pattern matching in variables expressions

parent 475d2edf
...@@ -15,7 +15,7 @@ module Gitlab ...@@ -15,7 +15,7 @@ module Gitlab
text = @left.evaluate(variables) text = @left.evaluate(variables)
regexp = @right.evaluate(variables) regexp = @right.evaluate(variables)
regexp.scan(text).any? regexp.scan(text.to_s).any?
end end
def self.build(_value, behind, ahead) def self.build(_value, behind, ahead)
......
...@@ -3,6 +3,8 @@ module Gitlab ...@@ -3,6 +3,8 @@ module Gitlab
module Pipeline module Pipeline
module Expression module Expression
module Lexeme module Lexeme
require_dependency 're2'
class Pattern < Lexeme::Value class Pattern < Lexeme::Value
PATTERN = %r{/(?<regexp>.+)/}.freeze PATTERN = %r{/(?<regexp>.+)/}.freeze
......
...@@ -10,8 +10,10 @@ module Gitlab ...@@ -10,8 +10,10 @@ module Gitlab
LEXEMES = [ LEXEMES = [
Expression::Lexeme::Variable, Expression::Lexeme::Variable,
Expression::Lexeme::String, Expression::Lexeme::String,
Expression::Lexeme::Pattern,
Expression::Lexeme::Null, Expression::Lexeme::Null,
Expression::Lexeme::Equals Expression::Lexeme::Equals,
Expression::Lexeme::Matches
].freeze ].freeze
MAX_TOKENS = 100 MAX_TOKENS = 100
......
...@@ -6,12 +6,14 @@ module Gitlab ...@@ -6,12 +6,14 @@ module Gitlab
StatementError = Class.new(Expression::ExpressionError) StatementError = Class.new(Expression::ExpressionError)
GRAMMAR = [ GRAMMAR = [
%w[variable],
%w[variable equals string], %w[variable equals string],
%w[variable equals variable], %w[variable equals variable],
%w[variable equals null], %w[variable equals null],
%w[string equals variable], %w[string equals variable],
%w[null equals variable], %w[null equals variable],
%w[variable] %w[variable matches pattern],
%w[pattern matches variable]
].freeze ].freeze
def initialize(statement, variables = {}) def initialize(statement, variables = {})
......
...@@ -39,6 +39,16 @@ describe Gitlab::Ci::Pipeline::Expression::Lexeme::Matches do ...@@ -39,6 +39,16 @@ describe Gitlab::Ci::Pipeline::Expression::Lexeme::Matches do
expect(operator.evaluate).to eq true expect(operator.evaluate).to eq true
end end
it 'supports matching against a nil value' do
allow(left).to receive(:evaluate).and_return(nil)
allow(right).to receive(:evaluate)
.and_return(Gitlab::UntrustedRegexp.new('pattern'))
operator = described_class.new(left, right)
expect(operator.evaluate).to eq false
end
it 'supports multiline strings' do it 'supports multiline strings' do
allow(left).to receive(:evaluate).and_return <<~TEXT allow(left).to receive(:evaluate).and_return <<~TEXT
My awesome contents My awesome contents
......
require 'fast_spec_helper' require 'fast_spec_helper'
require_dependency 're2'
describe Gitlab::Ci::Pipeline::Expression::Lexeme::Pattern do describe Gitlab::Ci::Pipeline::Expression::Lexeme::Pattern do
describe '.build' do describe '.build' do
......
...@@ -84,7 +84,6 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do ...@@ -84,7 +84,6 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do
describe '#evaluate' do describe '#evaluate' do
statements = [ statements = [
['$PRESENT_VARIABLE == "my variable"', true], ['$PRESENT_VARIABLE == "my variable"', true],
["$PRESENT_VARIABLE == 'my variable'", true],
['"my variable" == $PRESENT_VARIABLE', true], ['"my variable" == $PRESENT_VARIABLE', true],
['$PRESENT_VARIABLE == null', false], ['$PRESENT_VARIABLE == null', false],
['$EMPTY_VARIABLE == null', false], ['$EMPTY_VARIABLE == null', false],
...@@ -93,7 +92,11 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do ...@@ -93,7 +92,11 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do
['$UNDEFINED_VARIABLE == null', true], ['$UNDEFINED_VARIABLE == null', true],
['null == $UNDEFINED_VARIABLE', true], ['null == $UNDEFINED_VARIABLE', true],
['$PRESENT_VARIABLE', 'my variable'], ['$PRESENT_VARIABLE', 'my variable'],
['$UNDEFINED_VARIABLE', nil] ['$UNDEFINED_VARIABLE', nil],
["$PRESENT_VARIABLE =~ /var.*e$/", true],
["$PRESENT_VARIABLE =~ /^var.*/", false],
["$EMPTY_VARIABLE =~ /var.*/", false],
["$UNDEFINED_VARIABLE =~ /var.*/", false]
] ]
statements.each do |expression, value| statements.each do |expression, value|
...@@ -115,7 +118,9 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do ...@@ -115,7 +118,9 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do
['$PRESENT_VARIABLE', true], ['$PRESENT_VARIABLE', true],
['$UNDEFINED_VARIABLE', false], ['$UNDEFINED_VARIABLE', false],
['$EMPTY_VARIABLE', false], ['$EMPTY_VARIABLE', false],
['$INVALID = 1', false] ['$INVALID = 1', false],
["$PRESENT_VARIABLE =~ /var.*/", true],
["$UNDEFINED_VARIABLE =~ /var.*/", false]
] ]
statements.each do |expression, value| statements.each do |expression, value|
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment