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

125 lines
4.1 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)
if fork_to_project
link_existing_project(fork_to_project)
else
fork_new_project
end
end
private
2018-12-13 13:39:08 +05:30
def allowed_fork?
current_user.can?(:fork_project, @project)
end
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
2018-12-13 13:39:08 +05:30
if link_fork_network(fork_to_project)
# A forked project stores its LFS objects in the `forked_from_project`.
# So the LFS objects become inaccessible, and therefore delete them from
# the database so they'll get cleaned up.
#
# TODO: refactor this to get the correct lfs objects when implementing
2019-12-04 20:38:33 +05:30
# https://gitlab.com/gitlab-org/gitlab-foss/issues/39769
2018-12-13 13:39:08 +05:30
fork_to_project.lfs_objects_projects.delete_all
2018-11-20 20:47:30 +05:30
2018-12-13 13:39:08 +05:30
fork_to_project
end
2018-03-17 18:26:18 +05:30
end
def fork_new_project
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,
# 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)
2015-09-11 14:41:01 +05:30
new_project = CreateService.new(current_user, new_params).execute
2016-10-01 15:18:49 +05:30
return new_project unless new_project.persisted?
2018-12-13 13:39:08 +05:30
# 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
2016-09-29 09:46:39 +05:30
builds_access_level = @project.project_feature.builds_access_level
2018-11-18 11:00:15 +05:30
new_project.project_feature.update(builds_access_level: builds_access_level)
2016-09-29 09:46:39 +05:30
2015-09-11 14:41:01 +05:30
new_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
2018-12-13 13:39:08 +05:30
fork_to_project.fork_network_member.save &&
refresh_forks_count
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
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
def allowed_visibility_level
2018-03-17 18:26:18 +05:30
target_level = [@project.visibility_level, target_namespace.visibility_level].min
2018-03-17 18:26:18 +05:30
Gitlab::VisibilityLevel.closest_allowed_level(target_level)
end
2014-09-02 18:07:02 +05:30
end
end