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[gitlab_custom_project_template gitlab_project_migration].freeze
README_FILE =
'README.md'

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?, #can_all?, #can_any?

Constructor Details

#initialize(user, params) ⇒ CreateService

Returns a new instance of CreateService.



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

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)
  @repository_object_format = @params.delete(:repository_object_format)
  @import_export_upload = @params.delete(:import_export_upload)

  build_topics
end

Instance Method Details

#executeObject



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
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/services/projects/create_service.rb', line 27

def execute
  params[:wiki_enabled] = params[:wiki_access_level] if params[:wiki_access_level]
  params[:builds_enabled] = params[:builds_access_level] if params[:builds_access_level]
  params[:snippets_enabled] = params[:snippets_access_level] if params[:snippets_access_level]
  params[:merge_requests_enabled] = params[:merge_requests_access_level] if params[:merge_requests_access_level]
  params[:issues_enabled] = params[:issues_access_level] if params[:issues_access_level]

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

  @project = Project.new.tap do |p|
    # Explicitly build an association for ci_cd_settings
    # See: https://gitlab.com/gitlab-org/gitlab/-/issues/421050
    p.build_ci_cd_settings
    p.assign_attributes(params.merge(creator: current_user))
  end

  if @import_export_upload
    @import_export_upload.project = project
  end

  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

  @project.namespace_id = (params[:namespace_id] || current_user.namespace_id).to_i
  @project.organization_id = (params[:organization_id] || @project.namespace.organization_id).to_i

  @project.check_personal_projects_limit
  return @project if @project.errors.any?

  validate_create_permissions
  validate_import_permissions
  return @project if @project.errors.any?

  @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