diff options
author | Luke Duncalfe <lduncalfe@eml.cc> | 2019-03-29 04:07:03 +0300 |
---|---|---|
committer | Luke Duncalfe <lduncalfe@eml.cc> | 2019-04-09 00:36:42 +0300 |
commit | aa352a95df665ded5178c1b26d4492433e47714e (patch) | |
tree | 47e60787a36f6ab36d4649cef763f16b636583d8 /lib/gitlab/push_options.rb | |
parent | 225edb0d2d7737cf52ef5cd358082d08e20feaa4 (diff) |
Support merge request create with push options
To create a new merge request:
git push -u origin -o merge_request.create
To create a new merge request setting target branch:
git push -u origin -o merge_request.create \
-o merge_request.target=123
To update an existing merge request with a new target branch:
git push -u origin -o merge_request.target=123
A new Gitlab::PushOptions class handles parsing and validating the push
options array. This can be the start of the standard of GitLab accepting
push options that follow namespacing rules. Rules are discussed in issue
https://gitlab.com/gitlab-org/gitlab-ce/issues/43263.
E.g. these push options:
-o merge_request.create -o merge_request.target=123
Become parsed as:
{
merge_request: {
create: true,
target: '123',
}
}
And are fetched with the class via:
push_options.get(:merge_request)
push_options.get(:merge_request, :create)
push_options.get(:merge_request, :target)
A new MergeRequests::PushOptionsHandlerService takes the `merge_request`
namespaced push options and handles creating and updating
merge requests.
Any errors encountered are passed to the existing `output` Hash in
Api::Internal's `post_receive` endpoint, and passed to gitlab-shell
where they're output to the user.
Issue https://gitlab.com/gitlab-org/gitlab-ce/issues/43263
Diffstat (limited to 'lib/gitlab/push_options.rb')
-rw-r--r-- | lib/gitlab/push_options.rb | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/lib/gitlab/push_options.rb b/lib/gitlab/push_options.rb new file mode 100644 index 00000000000..923aa09527d --- /dev/null +++ b/lib/gitlab/push_options.rb @@ -0,0 +1,65 @@ +# frozen_string_literal: true + +module Gitlab + class PushOptions + VALID_OPTIONS = HashWithIndifferentAccess.new({ + merge_request: { + keys: [:create, :target] + }, + ci: { + keys: [:skip] + } + }).freeze + + NAMESPACE_ALIASES = HashWithIndifferentAccess.new({ + mr: :merge_request + }).freeze + + OPTION_MATCHER = /(?<namespace>[^\.]+)\.(?<key>[^=]+)=?(?<value>.*)/ + + attr_reader :options + + def initialize(options = []) + @options = parse_options(options) + end + + def get(*args) + options.dig(*args) + 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?) + + 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 |