debian-mirror-gitlab/lib/gitlab/push_options.rb

108 lines
2.4 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
module Gitlab
class PushOptions
VALID_OPTIONS = HashWithIndifferentAccess.new({
merge_request: {
2019-10-12 21:52:04 +05:30
keys: [
2021-04-29 21:17:54 +05:30
:assign,
2019-10-12 21:52:04 +05:30
:create,
:description,
2022-07-16 23:28:13 +05:30
:draft,
2019-12-04 20:38:33 +05:30
:label,
2019-10-12 21:52:04 +05:30
:merge_when_pipeline_succeeds,
2021-09-30 23:02:18 +05:30
:milestone,
2019-10-12 21:52:04 +05:30
:remove_source_branch,
:target,
2019-12-04 20:38:33 +05:30
:title,
2021-04-29 21:17:54 +05:30
:unassign,
2019-12-04 20:38:33 +05:30
:unlabel
2019-10-12 21:52:04 +05:30
]
2019-07-07 11:18:12 +05:30
},
ci: {
2020-01-01 13:55:28 +05:30
keys: [:skip, :variable]
2019-07-07 11:18:12 +05:30
}
}).freeze
2019-12-04 20:38:33 +05:30
MULTI_VALUE_OPTIONS = [
2020-01-01 13:55:28 +05:30
%w[ci variable],
2019-12-04 20:38:33 +05:30
%w[merge_request label],
2021-04-29 21:17:54 +05:30
%w[merge_request unlabel],
%w[merge_request assign],
%w[merge_request unassign]
2019-12-04 20:38:33 +05:30
].freeze
2019-07-07 11:18:12 +05:30
NAMESPACE_ALIASES = HashWithIndifferentAccess.new({
mr: :merge_request
}).freeze
2019-07-31 22:56:46 +05:30
OPTION_MATCHER = /(?<namespace>[^\.]+)\.(?<key>[^=]+)=?(?<value>.*)/.freeze
2019-07-07 11:18:12 +05:30
2020-03-13 15:44:24 +05:30
CI_SKIP = 'ci.skip'
2019-07-07 11:18:12 +05:30
attr_reader :options
def initialize(options = [])
@options = parse_options(options)
end
def get(*args)
options.dig(*args)
end
# Allow #to_json serialization
def as_json(*_args)
options
end
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?)
2019-12-04 20:38:33 +05:30
store_option_info(options, namespace, key, value)
2019-07-07 11:18:12 +05:30
end
options
end
2019-12-04 20:38:33 +05:30
def store_option_info(options, namespace, key, value)
options[namespace] ||= HashWithIndifferentAccess.new
if option_multi_value?(namespace, key)
options[namespace][key] ||= HashWithIndifferentAccess.new(0)
options[namespace][key][value] += 1
else
options[namespace][key] = value
end
end
def option_multi_value?(namespace, key)
MULTI_VALUE_OPTIONS.any? { |arr| arr == [namespace, key] }
end
2019-07-07 11:18:12 +05:30
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