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
      # Make sure that the user is allowed to use the specified visibility level
      return @project unless visibility_level_allowed?
14

15
      # Set project name from path
16
17
18
19
      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
20
        @project.name = @project.path.dup
21
22
23
24
      elsif @project.name.present?
        # For compatibility - set path from name
        # TODO: remove this in 8.0
        @project.path = @project.name.dup.parameterize
25
      end
26

27
28
      # get namespace id
      namespace_id = params[:namespace_id]
29
30
31
32

      if namespace_id
        # Find matching namespace and check if it allowed
        # for current user if namespace_id passed.
33
34
        unless allowed_namespace?(current_user, namespace_id)
          @project.namespace_id = nil
35
36
37
38
39
          deny_namespace
          return @project
        end
      else
        # Set current user namespace if namespace_id is nil
40
        @project.namespace_id = current_user.namespace_id
41
42
      end

43
      @project.creator = current_user
44

45
46
47
48
      if forked_from_project_id
        @project.build_forked_project_link(forked_from_project_id: forked_from_project_id)
      end

49
50
      Project.transaction do
        @project.save
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
51

52
        if @project.persisted? && !@project.import?
53
          raise 'Failed to create repository' unless @project.create_repository
54
        end
55
      end
56

57
      after_create_actions if @project.persisted?
58
59

      @project
60
61
62
63
    rescue => e
      message = "Unable to save project: #{e.message}"
      Rails.logger.error(message)
      @project.errors.add(:base, message) if @project
64
65
66
67
68
69
70
71
72
73
      @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
74
      namespace = Namespace.find_by(id: namespace_id)
75
      current_user.can?(:create_projects, namespace)
76
    end
77
78
79

    def after_create_actions
      log_info("#{@project.owner.name} created a new project \"#{@project.name_with_namespace}\"")
80
81
82

      @project.create_wiki if @project.wiki_enabled?

83
84
      @project.build_missing_services

Valery Sizov's avatar
Valery Sizov committed
85
86
      @project.create_labels

87
      event_service.create_project(@project, current_user)
88
89
90
      system_hook_service.execute_hooks_for(@project, :create)

      unless @project.group
91
        @project.team << [current_user, :master, current_user]
92
93
      end

Douwe Maan's avatar
Douwe Maan committed
94
      @project.import_start if @project.import?
95
    end
96
97
98
99

    def visibility_level_allowed?
      Gitlab::VisibilityLevel.allowed_for?(current_user, params[:visibility_level]) && @project.visibility_level_allowed?(@project.visibility_level)
    end
100
101
  end
end