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

nested_create_service.rb « groups « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 35d45aaf0cca8f4132409aaee621a69c6f5701b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# frozen_string_literal: true

module Groups
  class NestedCreateService < Groups::BaseService
    attr_reader :group_path, :visibility_level

    def initialize(user, params)
      @current_user = user
      @params = params.dup
      @group_path = @params.delete(:group_path)
      @visibility_level = @params.delete(:visibility_level) ||
        Gitlab::CurrentSettings.current_application_settings.default_group_visibility
    end

    def execute
      return unless group_path

      if namespace = namespace_or_group(group_path)
        return namespace
      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,
          visibility_level: visibility_level
        )

        last_group = namespace_or_group(partial_path) ||
          Groups::CreateService.new(current_user, new_params).execute
      end

      last_group
    end

    def namespace_or_group(group_path)
      Namespace.find_by_full_path(group_path)
    end
  end
end