Read about our upcoming Code of Conduct on this issue

create_service.rb 2.52 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
    def execute
8
      @project = Project.new(params)
9

10
      # Reset visibility levet if is not allowed to set it
11
      unless Gitlab::VisibilityLevel.allowed_for?(current_user, params[:visibility_level])
12
        @project.visibility_level = default_features.visibility_level
13
14
      end

15
16
17
18
19
      # Parametrize path for project
      #
      # Ex.
      #  'GitLab HQ'.parameterize => "gitlab-hq"
      #
20
      @project.path = @project.name.dup.parameterize unless @project.path.present?
21

22
23
      # get namespace id
      namespace_id = params[:namespace_id]
24
25
26
27

      if namespace_id
        # Find matching namespace and check if it allowed
        # for current user if namespace_id passed.
28
29
        unless allowed_namespace?(current_user, namespace_id)
          @project.namespace_id = nil
30
31
32
33
34
          deny_namespace
          return @project
        end
      else
        # Set current user namespace if namespace_id is nil
35
        @project.namespace_id = current_user.namespace_id
36
37
      end

38
      @project.creator = current_user
39

40
      if @project.save
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
41
        log_info("#{@project.owner.name} created a new project \"#{@project.name_with_namespace}\"")
42
        system_hook_service.execute_hooks_for(@project, :create)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
43

44
        unless @project.group
45
46
47
48
          @project.users_projects.create(
            project_access: UsersProject::MASTER,
            user: current_user
          )
49
        end
50
51
52
53

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

        if @project.import?
54
          @project.import_start
55
56
57
58
59
60
61
62
63
64
65
        else
          GitlabShellWorker.perform_async(
            :add_repository,
            @project.path_with_namespace
          )

        end

        if @project.wiki_enabled?
          begin
            # force the creation of a wiki,
66
67
            ProjectWiki.new(@project, @project.owner).wiki
          rescue ProjectWiki::CouldNotCreateWikiError => ex
68
69
70
71
72
            # Prevent project observer crash
            # if failed to create wiki
            nil
          end
        end
73
74
75
      end

      @project
76
77
    rescue => ex
      @project.errors.add(:base, "Can't save project. Please try again later")
78
79
80
81
82
83
84
85
86
87
      @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
88
      namespace = Namespace.find_by(id: namespace_id)
89
      current_user.can?(:create_projects, namespace)
90
91
92
    end
  end
end