create_service.rb 2.96 KB
Newer Older
1
module Projects
2
  class CreateService < BaseService
3
4
5
6
    def initialize(user, params)
      @current_user, @params = user, params.dup
    end

7
8
    def execute
      # get namespace id
9
      namespace_id = params.delete(:namespace_id)
10

11
12
13
14
15
      # check that user is allowed to set specified visibility_level
      unless Gitlab::VisibilityLevel.allowed_for?(current_user, params[:visibility_level])
        params.delete(:visibility_level)
      end

16
17
18
19
20
21
22
      # Load default feature settings
      default_features = Gitlab.config.gitlab.default_projects_features

      default_opts = {
        issues_enabled: default_features.issues,
        wiki_enabled: default_features.wiki,
        snippets_enabled: default_features.snippets,
23
        merge_requests_enabled: default_features.merge_requests,
24
        visibility_level: default_features.visibility_level
25
      }.stringify_keys
26
27

      @project = Project.new(default_opts.merge(params))
28
29
30
31
32
33

      # Parametrize path for project
      #
      # Ex.
      #  'GitLab HQ'.parameterize => "gitlab-hq"
      #
34
      @project.path = @project.name.dup.parameterize unless @project.path.present?
35
36
37
38
39
40


      if namespace_id
        # Find matching namespace and check if it allowed
        # for current user if namespace_id passed.
        if allowed_namespace?(current_user, namespace_id)
41
          @project.namespace_id = namespace_id
42
43
44
45
46
47
        else
          deny_namespace
          return @project
        end
      else
        # Set current user namespace if namespace_id is nil
48
        @project.namespace_id = current_user.namespace_id
49
50
      end

51
      @project.creator = current_user
52

53
      if @project.save
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
54
        log_info("#{@project.owner.name} created a new project \"#{@project.name_with_namespace}\"")
55
        system_hook_service.execute_hooks_for(@project, :create)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
56

57
        unless @project.group
58
59
60
61
          @project.users_projects.create(
            project_access: UsersProject::MASTER,
            user: current_user
          )
62
        end
63
64
65
66

        @project.update_column(:last_activity_at, @project.created_at)

        if @project.import?
67
          @project.import_start
68
69
70
71
72
73
74
75
76
77
78
        else
          GitlabShellWorker.perform_async(
            :add_repository,
            @project.path_with_namespace
          )

        end

        if @project.wiki_enabled?
          begin
            # force the creation of a wiki,
79
80
            ProjectWiki.new(@project, @project.owner).wiki
          rescue ProjectWiki::CouldNotCreateWikiError => ex
81
82
83
84
85
            # Prevent project observer crash
            # if failed to create wiki
            nil
          end
        end
86
87
88
      end

      @project
89
90
    rescue => ex
      @project.errors.add(:base, "Can't save project. Please try again later")
91
92
93
94
95
96
97
98
99
100
      @project
    end

    protected

    def deny_namespace
      @project.errors.add(:namespace, "is not valid")
    end

    def allowed_namespace?(user, namespace_id)
skv's avatar
skv committed
101
      namespace = Namespace.find_by(id: namespace_id)
102
      current_user.can?(:create_projects, namespace)
103
104
105
    end
  end
end