debian-mirror-gitlab/app/services/groups/nested_create_service.rb

58 lines
1.5 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
module Groups
class NestedCreateService < Groups::BaseService
2018-11-18 11:00:15 +05:30
attr_reader :group_path, :visibility_level
2018-03-17 18:26:18 +05:30
def initialize(user, params)
@current_user, @params = user, params.dup
@group_path = @params.delete(:group_path)
2018-11-18 11:00:15 +05:30
@visibility_level = @params.delete(:visibility_level) ||
Gitlab::CurrentSettings.current_application_settings.default_group_visibility
2018-03-17 18:26:18 +05:30
end
def execute
2019-05-18 00:54:41 +05:30
return unless group_path
2018-03-17 18:26:18 +05:30
if namespace = namespace_or_group(group_path)
return namespace
end
2019-02-15 15:39:39 +05:30
if group_path.include?('/') && !Group.supports_nested_objects?
2018-03-17 18:26:18 +05:30
raise 'Nested groups are not supported on MySQL'
end
create_group_path
end
private
def create_group_path
group_path_segments = group_path.split('/')
last_group = nil
partial_path_segments = []
while subgroup_name = group_path_segments.shift
partial_path_segments << subgroup_name
partial_path = partial_path_segments.join('/')
new_params = params.reverse_merge(
path: subgroup_name,
name: subgroup_name,
2018-11-18 11:00:15 +05:30
parent: last_group,
visibility_level: visibility_level
2018-03-17 18:26:18 +05:30
)
2018-11-18 11:00:15 +05:30
last_group = namespace_or_group(partial_path) ||
Groups::CreateService.new(current_user, new_params).execute
2018-03-17 18:26:18 +05:30
end
last_group
end
def namespace_or_group(group_path)
Namespace.find_by_full_path(group_path)
end
end
end