2018-07-16 12:31:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-22 06:13:25 -04:00
|
|
|
module Groups
|
|
|
|
class NestedCreateService < Groups::BaseService
|
2018-07-02 05:16:24 -04:00
|
|
|
attr_reader :group_path, :visibility_level
|
2017-08-22 06:13:25 -04:00
|
|
|
|
|
|
|
def initialize(user, params)
|
2021-04-19 17:09:27 -04:00
|
|
|
@current_user = user
|
|
|
|
@params = params.dup
|
2017-08-22 06:13:25 -04:00
|
|
|
@group_path = @params.delete(:group_path)
|
2018-07-02 05:16:24 -04:00
|
|
|
@visibility_level = @params.delete(:visibility_level) ||
|
|
|
|
Gitlab::CurrentSettings.current_application_settings.default_group_visibility
|
2017-08-22 06:13:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2019-02-08 07:19:53 -05:00
|
|
|
return unless group_path
|
2017-08-22 06:13:25 -04:00
|
|
|
|
2018-01-26 10:39:10 -05:00
|
|
|
if namespace = namespace_or_group(group_path)
|
|
|
|
return namespace
|
2017-08-22 06:13:25 -04:00
|
|
|
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-07-02 05:16:24 -04:00
|
|
|
parent: last_group,
|
|
|
|
visibility_level: visibility_level
|
2017-08-22 06:13:25 -04:00
|
|
|
)
|
|
|
|
|
2018-07-02 05:16:24 -04:00
|
|
|
last_group = namespace_or_group(partial_path) ||
|
|
|
|
Groups::CreateService.new(current_user, new_params).execute
|
2017-08-22 06:13:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
last_group
|
|
|
|
end
|
2018-01-26 10:39:10 -05:00
|
|
|
|
|
|
|
def namespace_or_group(group_path)
|
|
|
|
Namespace.find_by_full_path(group_path)
|
|
|
|
end
|
2017-08-22 06:13:25 -04:00
|
|
|
end
|
|
|
|
end
|