2019-03-28 21:07:03 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
class PushOptions
|
|
|
|
VALID_OPTIONS = HashWithIndifferentAccess.new({
|
|
|
|
merge_request: {
|
2019-04-05 09:22:58 -04:00
|
|
|
keys: [:create, :merge_when_pipeline_succeeds, :target]
|
2019-03-28 21:07:03 -04:00
|
|
|
},
|
|
|
|
ci: {
|
|
|
|
keys: [:skip]
|
|
|
|
}
|
|
|
|
}).freeze
|
|
|
|
|
|
|
|
NAMESPACE_ALIASES = HashWithIndifferentAccess.new({
|
|
|
|
mr: :merge_request
|
|
|
|
}).freeze
|
|
|
|
|
2019-05-05 06:19:14 -04:00
|
|
|
OPTION_MATCHER = /(?<namespace>[^\.]+)\.(?<key>[^=]+)=?(?<value>.*)/.freeze
|
2019-03-28 21:07:03 -04:00
|
|
|
|
|
|
|
attr_reader :options
|
|
|
|
|
|
|
|
def initialize(options = [])
|
|
|
|
@options = parse_options(options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get(*args)
|
|
|
|
options.dig(*args)
|
|
|
|
end
|
|
|
|
|
2019-04-05 00:19:30 -04:00
|
|
|
# Allow #to_json serialization
|
|
|
|
def as_json(*_args)
|
|
|
|
options
|
|
|
|
end
|
|
|
|
|
2019-03-28 21:07:03 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def parse_options(raw_options)
|
|
|
|
options = HashWithIndifferentAccess.new
|
|
|
|
|
|
|
|
Array.wrap(raw_options).each do |option|
|
|
|
|
namespace, key, value = parse_option(option)
|
|
|
|
|
|
|
|
next if [namespace, key].any?(&:nil?)
|
|
|
|
|
|
|
|
options[namespace] ||= HashWithIndifferentAccess.new
|
|
|
|
options[namespace][key] = value
|
|
|
|
end
|
|
|
|
|
|
|
|
options
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_option(option)
|
|
|
|
parts = OPTION_MATCHER.match(option)
|
|
|
|
return unless parts
|
|
|
|
|
|
|
|
namespace, key, value = parts.values_at(:namespace, :key, :value).map(&:strip)
|
|
|
|
namespace = NAMESPACE_ALIASES[namespace] if NAMESPACE_ALIASES[namespace]
|
|
|
|
value = value.presence || true
|
|
|
|
|
|
|
|
return unless valid_option?(namespace, key)
|
|
|
|
|
|
|
|
[namespace, key, value]
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_option?(namespace, key)
|
|
|
|
keys = VALID_OPTIONS.dig(namespace, :keys)
|
|
|
|
keys && keys.include?(key.to_sym)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|