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

108 lines
3.4 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
# https://gitlab.com/gitlab-org/gitlab-ce/issues/39769
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 = {
visibility_level: allowed_visibility_level,
2015-09-11 14:41:01 +05:30
description: @project.description,
name: @project.name,
path: @project.path,
2015-12-23 02:04:40 +05:30
shared_runners_enabled: @project.shared_runners_enabled,
2018-12-13 13:39:08 +05:30
namespace_id: target_namespace.id,
fork_network: fork_network,
# 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.
relations_block: -> (project) { build_fork_network_member(project) }
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
2018-03-17 18:26:18 +05:30
def target_namespace
@target_namespace ||= @params[:namespace] || current_user.namespace
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