Class: Projects::CreateService

Inherits:
BaseService show all
Defined in:
app/services/projects/create_service.rb

Instance Attribute Summary

Attributes inherited from BaseService

#current_user, #params, #project

Instance Method Summary collapse

Methods inherited from BaseService

#abilities, #can?, #deny_visibility_level, #event_service, #log_info, #notification_service, #repository, #system_hook_service, #todo_service

Methods included from Gitlab::CurrentSettings

#current_application_settings, #fake_application_settings

Constructor Details

#initialize(user, params) ⇒ CreateService

Returns a new instance of CreateService


3
4
5
# File 'app/services/projects/create_service.rb', line 3

def initialize(user, params)
  @current_user, @params = user, params.dup
end

Instance Method Details

#executeObject


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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/services/projects/create_service.rb', line 7

def execute
  forked_from_project_id = params.delete(:forked_from_project_id)

  @project = Project.new(params)

  # 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
  end

  # Set project name from path
  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
    @project.name = @project.path.dup
  elsif @project.name.present?
    # For compatibility - set path from name
    # TODO: remove this in 8.0
    @project.path = @project.name.dup.parameterize
  end

  # get namespace id
  namespace_id = params[:namespace_id]

  if namespace_id
    # Find matching namespace and check if it allowed
    # for current user if namespace_id passed.
    unless allowed_namespace?(current_user, namespace_id)
      @project.namespace_id = nil
      deny_namespace
      return @project
    end
  else
    # Set current user namespace if namespace_id is nil
    @project.namespace_id = current_user.namespace_id
  end

  @project.creator = current_user

  if forked_from_project_id
    @project.build_forked_project_link(forked_from_project_id: forked_from_project_id)
  end

  Project.transaction do
    @project.save

    if @project.persisted? && !@project.import?
      raise 'Failed to create repository' unless @project.create_repository
    end
  end

  after_create_actions if @project.persisted?

  @project
rescue => e
  message = "Unable to save project: #{e.message}"
  Rails.logger.error(message)
  @project.errors.add(:base, message) if @project
  @project
end