Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBob Van Landuyt <bob@vanlanduyt.co>2017-08-22 13:13:25 +0300
committerBob Van Landuyt <bob@vanlanduyt.co>2017-08-23 14:36:38 +0300
commit22ef4ba3a4be66296e5ee9231b4eb39e172c0f1f (patch)
treed59cb5328fffd5f0161444893b3429ae94e543cd /app/services/groups
parentd8d2b73b9f17e5af9aeccb1e9ba40045486651b5 (diff)
Migrate creation of nested groups into a service
Diffstat (limited to 'app/services/groups')
-rw-r--r--app/services/groups/nested_create_service.rb45
1 files changed, 45 insertions, 0 deletions
diff --git a/app/services/groups/nested_create_service.rb b/app/services/groups/nested_create_service.rb
new file mode 100644
index 00000000000..8d793f5c02e
--- /dev/null
+++ b/app/services/groups/nested_create_service.rb
@@ -0,0 +1,45 @@
+module Groups
+ class NestedCreateService < Groups::BaseService
+ attr_reader :group_path
+
+ def initialize(user, params)
+ @current_user, @params = user, params.dup
+
+ @group_path = @params.delete(:group_path)
+ end
+
+ def execute
+ return nil unless group_path
+
+ if group = Group.find_by_full_path(group_path)
+ return group
+ 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,
+ parent: last_group
+ )
+ new_params[:visibility_level] ||= Gitlab::CurrentSettings.current_application_settings.default_group_visibility
+
+ last_group = Group.find_by_full_path(partial_path) || Groups::CreateService.new(current_user, new_params).execute
+ end
+
+ last_group
+ end
+ end
+end