Class: Projects::CreateService

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

Instance Attribute Summary

Attributes inherited from BaseService

#current_user, #params, #project

Instance Method Summary collapse

Methods included from ValidatesClassificationLabel

#classification_label_change?, #rejection_reason_for_label, #validate_classification_label

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?

Constructor Details

#initialize(user, params) ⇒ CreateService

Returns a new instance of CreateService.


7
8
9
10
11
12
13
# File 'app/services/projects/create_service.rb', line 7

def initialize(user, params)
  @current_user, @params  = user, params.dup
  @skip_wiki              = @params.delete(:skip_wiki)
  @initialize_with_readme = Gitlab::Utils.to_boolean(@params.delete(:initialize_with_readme))
  @import_data            = @params.delete(:import_data)
  @relations_block        = @params.delete(:relations_block)
end

Instance Method Details

#executeObject


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
69
70
71
# File 'app/services/projects/create_service.rb', line 15

def execute
  if create_from_template?
    return ::Projects::CreateFromTemplateService.new(current_user, params).execute
  end

  @project = Project.new(params)

  # Make sure that the user is allowed to use the specified visibility level
  if project_visibility.restricted?
    deny_visibility_level(@project, project_visibility.visibility_level)
    return @project
  end

  set_project_name_from_path

  # 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

  @relations_block&.call(@project)
  yield(@project) if block_given?

  validate_classification_label(@project, :external_authorization_classification_label)

  # If the block added errors, don't try to save the project
  return @project if @project.errors.any?

  @project.creator = current_user

  save_project_and_import_data

  Gitlab::ApplicationContext.with_context(related_class: "Projects::CreateService", project: @project) do
    after_create_actions if @project.persisted?

    import_schedule
  end

  @project
rescue ActiveRecord::RecordInvalid => e
  message = "Unable to save #{e.record.type}: #{e.record.errors.full_messages.join(", ")} "
  fail(error: message)
rescue => e
  @project.errors.add(:base, e.message) if @project
  fail(error: e.message)
end