create_service.rb 2.91 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
9
      forked_from_project_id = params.delete(:forked_from_project_id)

10
      @project = Project.new(params)
11

12
13
14
15
16
17
      # Make sure that the user is allowed to use the specified visibility
      # level
      unless Gitlab::VisibilityLevel.allowed_for?(current_user,
                                                  params[:visibility_level])
        deny_visibility_level(@project)
        return @project
18
19
      end

20
      # Set project name from path
21
22
23
24
      if @project.name.present? && @project.path.present?
        # if both name and path set - everything is ok
      elsif @project.path.present?
        # Set project name from path
25
        @project.name = @project.path.dup
26
27
28
29
      elsif @project.name.present?
        # For compatibility - set path from name
        # TODO: remove this in 8.0
        @project.path = @project.name.dup.parameterize
30
      end
31

32
33
      # get namespace id
      namespace_id = params[:namespace_id]
34
35
36
37

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

48
      @project.creator = current_user
49

50
51
52
53
      if forked_from_project_id
        @project.build_forked_project_link(forked_from_project_id: forked_from_project_id)
      end

54
55
      Project.transaction do
        @project.save
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
56

57
        if @project.persisted? && !@project.import?
58
          raise 'Failed to create repository' unless @project.create_repository
59
        end
60
      end
61

62
      after_create_actions if @project.persisted?
63
64

      @project
65
66
    rescue => ex
      @project.errors.add(:base, "Can't save project. Please try again later")
67
68
69
70
71
72
73
74
75
76
      @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
77
      namespace = Namespace.find_by(id: namespace_id)
78
      current_user.can?(:create_projects, namespace)
79
    end
80
81
82

    def after_create_actions
      log_info("#{@project.owner.name} created a new project \"#{@project.name_with_namespace}\"")
83
84
85

      @project.create_wiki if @project.wiki_enabled?

86
87
      @project.build_missing_services

Valery Sizov's avatar
Valery Sizov committed
88
89
      @project.create_labels

90
      event_service.create_project(@project, current_user)
91
92
93
      system_hook_service.execute_hooks_for(@project, :create)

      unless @project.group
94
        @project.team << [current_user, :master, current_user]
95
96
97
98
99
100
101
102
      end

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

      if @project.import?
        @project.import_start
      end
    end
103
104
  end
end