diff options
Diffstat (limited to 'app/services/projects/create_service.rb')
-rw-r--r-- | app/services/projects/create_service.rb | 39 |
1 files changed, 31 insertions, 8 deletions
diff --git a/app/services/projects/create_service.rb b/app/services/projects/create_service.rb index 94cc4700a49..8ad2b0ac761 100644 --- a/app/services/projects/create_service.rb +++ b/app/services/projects/create_service.rb @@ -5,7 +5,7 @@ module Projects include ValidatesClassificationLabel ImportSourceDisabledError = Class.new(StandardError) - INTERNAL_IMPORT_SOURCES = %w[bare_repository gitlab_custom_project_template gitlab_project_migration].freeze + INTERNAL_IMPORT_SOURCES = %w[gitlab_custom_project_template gitlab_project_migration].freeze def initialize(user, params) @current_user = user @@ -58,6 +58,7 @@ module Projects return @project if @project.errors.any? validate_create_permissions + validate_import_permissions return @project if @project.errors.any? @relations_block&.call(@project) @@ -98,6 +99,13 @@ module Projects @project.errors.add(:namespace, "is not valid") end + def validate_import_permissions + return unless @project.import? + return if current_user.can?(:import_projects, parent_namespace) + + @project.errors.add(:user, 'is not allowed to import projects') + end + def after_create_actions log_info("#{current_user.name} created a new project \"#{@project.full_name}\"") @@ -144,8 +152,10 @@ module Projects # completes), and any other affected users in the background def setup_authorizations if @project.group - group_access_level = @project.group.max_member_access_for_user(current_user, - only_concrete_membership: true) + group_access_level = @project.group.max_member_access_for_user( + current_user, + only_concrete_membership: true + ) if group_access_level > GroupMember::NO_ACCESS current_user.project_authorizations.safe_find_or_create_by!( @@ -187,7 +197,7 @@ module Projects def create_readme commit_attrs = { - branch_name: @default_branch.presence || @project.default_branch_or_main, + branch_name: default_branch, commit_message: 'Initial commit', file_path: 'README.md', file_content: readme_content @@ -201,7 +211,11 @@ module Projects end def readme_content - @readme_template.presence || ReadmeRendererService.new(@project, current_user).execute + readme_attrs = { + default_branch: default_branch + } + + @readme_template.presence || ReadmeRendererService.new(@project, current_user, readme_attrs).execute end def skip_wiki? @@ -217,8 +231,10 @@ module Projects @project.create_labels unless @project.gitlab_project_import? - unless @project.import? - raise 'Failed to create repository' unless @project.create_repository + break if @project.import? + + unless @project.create_repository(default_branch: default_branch) + raise 'Failed to create repository' end end end @@ -267,6 +283,10 @@ module Projects private + def default_branch + @default_branch.presence || @project.default_branch_or_main + end + def validate_import_source_enabled! return unless @params[:import_type] @@ -274,6 +294,9 @@ module Projects return if INTERNAL_IMPORT_SOURCES.include?(import_type) + # Skip validation when creating project from a built in template + return if @params[:import_export_upload].present? && import_type == 'gitlab_project' + unless ::Gitlab::CurrentSettings.import_sources&.include?(import_type) raise ImportSourceDisabledError, "#{import_type} import source is disabled" end @@ -289,7 +312,7 @@ module Projects def import_schedule if @project.errors.empty? - @project.import_state.schedule if @project.import? && !@project.bare_repository_import? && !@project.gitlab_project_migration? + @project.import_state.schedule if @project.import? && !@project.gitlab_project_migration? else fail(error: @project.errors.full_messages.join(', ')) end |