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-04-08 14:13:33 +05:30
|
|
|
def valid_fork_targets
|
|
|
|
@valid_fork_targets ||= ForkTargetsFinder.new(@project, current_user).execute
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
def valid_fork_target?
|
|
|
|
return true if current_user.admin?
|
|
|
|
|
|
|
|
valid_fork_targets.include?(target_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-09 13:42:32 +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?
|
|
|
|
|
|
|
|
# Set the forked_from_project relation after saving to avoid having to
|
|
|
|
# reload the project to reset the association information and cause an
|
|
|
|
# extra query.
|
|
|
|
new_project.forked_from_project = @project
|
|
|
|
|
|
|
|
builds_access_level = @project.project_feature.builds_access_level
|
|
|
|
new_project.project_feature.update(builds_access_level: builds_access_level)
|
|
|
|
|
|
|
|
new_project
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_fork_params
|
2015-09-11 14:41:01 +05:30
|
|
|
new_params = {
|
2019-09-04 21:01:54 +05:30
|
|
|
visibility_level: allowed_visibility_level,
|
|
|
|
description: @project.description,
|
|
|
|
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) },
|
|
|
|
skip_disk_validation: skip_disk_validation
|
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
|
|
|
|
|
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
|
2016-06-16 23:09:34 +05:30
|
|
|
|
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
|
2016-06-16 23:09:34 +05:30
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
def allowed_visibility_level
|
2018-03-17 18:26:18 +05:30
|
|
|
target_level = [@project.visibility_level, target_namespace.visibility_level].min
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
Gitlab::VisibilityLevel.closest_allowed_level(target_level)
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|