2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
module Gitlab
|
|
|
|
module Search
|
|
|
|
class Query < SimpleDelegator
|
2019-02-15 15:39:39 +05:30
|
|
|
include EncodingHelper
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def initialize(query, filter_opts = {}, &block)
|
|
|
|
@raw_query = query.dup
|
|
|
|
@filters = []
|
|
|
|
@filter_options = { default_parser: :downcase.to_proc }.merge(filter_opts)
|
|
|
|
|
|
|
|
self.instance_eval(&block) if block_given?
|
|
|
|
|
|
|
|
@query = Gitlab::Search::ParsedQuery.new(*extract_filters)
|
|
|
|
# set the ParsedQuery as our default delegator thanks to SimpleDelegator
|
|
|
|
super(@query)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def filter(name, **attributes)
|
2020-10-24 23:57:45 +05:30
|
|
|
filter = {
|
|
|
|
parser: @filter_options[:default_parser],
|
|
|
|
name: name
|
|
|
|
}.merge(attributes)
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
@filters << filter
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_options(**options)
|
|
|
|
@filter_options.merge!(options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_filters
|
|
|
|
fragments = []
|
|
|
|
|
|
|
|
filters = @filters.each_with_object([]) do |filter, parsed_filters|
|
2020-10-24 23:57:45 +05:30
|
|
|
match = @raw_query.split.find { |part| part =~ /\A-?#{filter[:name]}:/ }
|
2018-11-08 19:23:39 +05:30
|
|
|
next unless match
|
|
|
|
|
|
|
|
input = match.split(':')[1..-1].join
|
|
|
|
next if input.empty?
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
filter[:negated] = match.start_with?("-")
|
2018-11-08 19:23:39 +05:30
|
|
|
filter[:value] = parse_filter(filter, input)
|
|
|
|
filter[:regex_value] = Regexp.escape(filter[:value]).gsub('\*', '.*?')
|
|
|
|
fragments << match
|
|
|
|
|
|
|
|
parsed_filters << filter
|
|
|
|
end
|
|
|
|
|
|
|
|
query = (@raw_query.split - fragments).join(' ')
|
|
|
|
|
|
|
|
[query, filters]
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_filter(filter, input)
|
2019-02-15 15:39:39 +05:30
|
|
|
result = filter[:parser].call(input)
|
|
|
|
|
|
|
|
@filter_options[:encode_binary] ? encode_binary(result) : result
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|