Class: Projects::CreateService

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

Constant Summary collapse

ImportSourceDisabledError =
Class.new(StandardError)
INTERNAL_IMPORT_SOURCES =
%w[bare_repository gitlab_custom_project_template gitlab_project_migration].freeze

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.


10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/services/projects/create_service.rb', line 10

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

  build_topics
end

Instance Method Details

#executeObject


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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/services/projects/create_service.rb', line 24

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

  @project = Project.new(params)

  validate_import_source_enabled!

  @project.visibility_level = @project.group.visibility_level unless @project.visibility_level_allowed_by_group?

  # If a project is newly created it should have shared runners settings
  # based on its group having it enabled. This is like the "default value"
  @project.shared_runners_enabled = false if !params.key?(:shared_runners_enabled) && @project.group && @project.group.shared_runners_setting != 'enabled'

  # 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 current_user.can?(:create_projects, parent_namespace)
      @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(project: @project) do
    after_create_actions if @project.persisted?

    import_schedule
  end

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