debian-mirror-gitlab/app/services/projects/fork_service.rb

138 lines
4.5 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
module Projects
class ForkService < BaseService
2018-03-17 18:26:18 +05:30
def execute(fork_to_project = nil)
2020-04-08 14:13:33 +05:30
forked_project = fork_to_project ? link_existing_project(fork_to_project) : fork_new_project
2020-01-01 13:55:28 +05:30
refresh_forks_count if forked_project&.saved?
forked_project
2018-03-17 18:26:18 +05:30
end
2020-11-24 15:15:51 +05:30
def valid_fork_targets(options = {})
@valid_fork_targets ||= ForkTargetsFinder.new(@project, current_user).execute(options)
2020-04-08 14:13:33 +05:30
end
2018-03-17 18:26:18 +05:30
2020-10-24 23:57:45 +05:30
def valid_fork_target?(namespace = target_namespace)
2020-04-08 14:13:33 +05:30
return true if current_user.admin?
2020-10-24 23:57:45 +05:30
valid_fork_targets.include?(namespace)
2018-12-13 13:39:08 +05:30
end
2020-04-08 14:13:33 +05:30
private
2018-03-17 18:26:18 +05:30
def link_existing_project(fork_to_project)
return if fork_to_project.forked?
2018-12-13 13:39:08 +05:30
build_fork_network_member(fork_to_project)
2018-03-17 18:26:18 +05:30
2020-03-13 15:44:24 +05:30
fork_to_project if link_fork_network(fork_to_project)
2018-03-17 18:26:18 +05:30
end
def fork_new_project
2020-04-08 14:13:33 +05:30
new_project = CreateService.new(current_user, new_fork_params).execute
return new_project unless new_project.persisted?
2021-07-02 01:05:55 +05:30
new_project.project_feature.update!(
@project.project_feature.slice(ProjectFeature::FEATURES.map { |f| "#{f}_access_level" })
)
2020-04-08 14:13:33 +05:30
new_project
end
def new_fork_params
2015-09-11 14:41:01 +05:30
new_params = {
2021-03-08 18:12:59 +05:30
forked_from_project: @project,
2021-03-11 19:13:27 +05:30
visibility_level: target_visibility_level,
description: target_description,
2019-09-04 21:01:54 +05:30
name: target_name,
path: target_path,
shared_runners_enabled: @project.shared_runners_enabled,
namespace_id: target_namespace.id,
fork_network: fork_network,
2019-12-21 20:55:43 +05:30
ci_config_path: @project.ci_config_path,
2019-09-04 21:01:54 +05:30
# We need to set ci_default_git_depth to 0 for the forked project when
# @project.ci_default_git_depth is nil in order to keep the same behaviour
# and not get ProjectCiCdSetting::DEFAULT_GIT_DEPTH set on create
ci_cd_settings_attributes: { default_git_depth: @project.ci_default_git_depth || 0 },
2018-12-13 13:39:08 +05:30
# We need to assign the fork network membership after the project has
# been instantiated to avoid ActiveRecord trying to create it when
# initializing the project, as that would cause a foreign key constraint
# exception.
2019-12-04 20:38:33 +05:30
relations_block: -> (project) { build_fork_network_member(project) },
2021-11-11 11:23:49 +05:30
skip_disk_validation: skip_disk_validation,
external_authorization_classification_label: @project.external_authorization_classification_label
2015-04-26 12:48:37 +05:30
}
2015-09-11 14:41:01 +05:30
if @project.avatar.present? && @project.avatar.image?
new_params[:avatar] = @project.avatar
2015-04-26 12:48:37 +05:30
end
2022-03-02 08:16:31 +05:30
new_params[:mr_default_target_self] = target_mr_default_target_self unless target_mr_default_target_self.nil?
2019-02-15 15:39:39 +05:30
new_params.merge!(@project.object_pool_params)
2020-04-08 14:13:33 +05:30
new_params
end
2016-09-29 09:46:39 +05:30
2020-04-08 14:13:33 +05:30
def allowed_fork?
current_user.can?(:fork_project, @project)
2014-09-02 18:07:02 +05:30
end
2018-03-17 18:26:18 +05:30
def fork_network
2018-12-13 13:39:08 +05:30
@fork_network ||= @project.fork_network || @project.build_root_of_fork_network
end
def build_fork_network_member(fork_to_project)
if allowed_fork?
fork_to_project.build_fork_network_member(forked_from_project: @project,
fork_network: fork_network)
2018-03-17 18:26:18 +05:30
else
2018-12-13 13:39:08 +05:30
fork_to_project.errors.add(:forked_from_project_id, 'is forbidden')
2018-03-17 18:26:18 +05:30
end
end
def link_fork_network(fork_to_project)
2018-12-13 13:39:08 +05:30
return if fork_to_project.errors.any?
2018-03-17 18:26:18 +05:30
2020-01-01 13:55:28 +05:30
fork_to_project.fork_network_member.save
2018-03-17 18:26:18 +05:30
end
2017-09-10 17:25:29 +05:30
def refresh_forks_count
Projects::ForksCountService.new(@project).refresh_cache
end
2019-07-07 11:18:12 +05:30
def target_path
@target_path ||= @params[:path] || @project.path
end
def target_name
@target_name ||= @params[:name] || @project.name
end
2021-03-11 19:13:27 +05:30
def target_description
@target_description ||= @params[:description] || @project.description
end
2018-03-17 18:26:18 +05:30
def target_namespace
@target_namespace ||= @params[:namespace] || current_user.namespace
end
2019-12-04 20:38:33 +05:30
def skip_disk_validation
@skip_disk_validation ||= @params[:skip_disk_validation] || false
end
2021-03-11 19:13:27 +05:30
def target_visibility_level
2018-03-17 18:26:18 +05:30
target_level = [@project.visibility_level, target_namespace.visibility_level].min
2021-03-11 19:13:27 +05:30
target_level = [target_level, Gitlab::VisibilityLevel.level_value(params[:visibility])].min if params.key?(:visibility)
2018-03-17 18:26:18 +05:30
Gitlab::VisibilityLevel.closest_allowed_level(target_level)
end
2022-03-02 08:16:31 +05:30
def target_mr_default_target_self
@target_mr_default_target_self ||= params[:mr_default_target_self]
end
2014-09-02 18:07:02 +05:30
end
end