2018-07-17 12:50:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-03-19 11:37:50 -04:00
|
|
|
module Projects
|
2014-01-16 12:03:42 -05:00
|
|
|
class ForkService < BaseService
|
2017-12-07 03:44:55 -05:00
|
|
|
def execute(fork_to_project = nil)
|
|
|
|
if fork_to_project
|
|
|
|
link_existing_project(fork_to_project)
|
|
|
|
else
|
|
|
|
fork_new_project
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def link_existing_project(fork_to_project)
|
|
|
|
return if fork_to_project.forked?
|
|
|
|
|
|
|
|
link_fork_network(fork_to_project)
|
|
|
|
|
2018-08-02 07:30:24 -04:00
|
|
|
# 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
|
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
fork_to_project
|
|
|
|
end
|
|
|
|
|
|
|
|
def fork_new_project
|
2015-04-24 15:37:12 -04:00
|
|
|
new_params = {
|
|
|
|
forked_from_project_id: @project.id,
|
2016-05-24 17:53:53 -04:00
|
|
|
visibility_level: allowed_visibility_level,
|
2015-04-24 15:37:12 -04:00
|
|
|
description: @project.description,
|
|
|
|
name: @project.name,
|
|
|
|
path: @project.path,
|
2015-12-04 06:55:23 -05:00
|
|
|
shared_runners_enabled: @project.shared_runners_enabled,
|
2017-12-20 10:19:54 -05:00
|
|
|
namespace_id: target_namespace.id
|
2014-08-25 05:19:52 -04:00
|
|
|
}
|
|
|
|
|
2015-04-24 15:37:12 -04:00
|
|
|
if @project.avatar.present? && @project.avatar.image?
|
|
|
|
new_params[:avatar] = @project.avatar
|
2014-10-03 04:12:44 -04:00
|
|
|
end
|
2014-11-13 12:40:47 -05:00
|
|
|
|
2015-04-24 15:37:12 -04:00
|
|
|
new_project = CreateService.new(current_user, new_params).execute
|
2016-09-19 14:28:41 -04:00
|
|
|
return new_project unless new_project.persisted?
|
|
|
|
|
2016-08-01 18:31:21 -04:00
|
|
|
builds_access_level = @project.project_feature.builds_access_level
|
2018-07-02 06:43:06 -04:00
|
|
|
new_project.project_feature.update(builds_access_level: builds_access_level)
|
2016-08-01 18:31:21 -04:00
|
|
|
|
2017-09-28 10:38:12 -04:00
|
|
|
link_fork_network(new_project)
|
|
|
|
|
2015-04-24 15:37:12 -04:00
|
|
|
new_project
|
2013-05-02 15:30:13 -04:00
|
|
|
end
|
2016-05-24 17:53:53 -04:00
|
|
|
|
2017-09-28 10:38:12 -04:00
|
|
|
def fork_network
|
|
|
|
if @project.fork_network
|
|
|
|
@project.fork_network
|
|
|
|
elsif forked_from_project = @project.forked_from_project
|
|
|
|
# TODO: remove this case when all background migrations have completed
|
|
|
|
# this only happens when a project had a `forked_project_link` that was
|
|
|
|
# not migrated to the `fork_network` relation
|
|
|
|
forked_from_project.fork_network || forked_from_project.create_root_of_fork_network
|
|
|
|
else
|
|
|
|
@project.create_root_of_fork_network
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
def link_fork_network(fork_to_project)
|
|
|
|
fork_network.fork_network_members.create(project: fork_to_project,
|
2017-09-28 10:38:12 -04:00
|
|
|
forked_from_project: @project)
|
2017-12-07 03:44:55 -05:00
|
|
|
|
|
|
|
# TODO: remove this when ForkedProjectLink model is removed
|
|
|
|
unless fork_to_project.forked_project_link
|
|
|
|
fork_to_project.create_forked_project_link(forked_to_project: fork_to_project,
|
|
|
|
forked_from_project: @project)
|
|
|
|
end
|
|
|
|
|
|
|
|
refresh_forks_count
|
2017-09-28 10:38:12 -04:00
|
|
|
end
|
|
|
|
|
2017-08-14 09:22:09 -04:00
|
|
|
def refresh_forks_count
|
|
|
|
Projects::ForksCountService.new(@project).refresh_cache
|
|
|
|
end
|
|
|
|
|
2017-12-20 10:19:54 -05:00
|
|
|
def target_namespace
|
|
|
|
@target_namespace ||= @params[:namespace] || current_user.namespace
|
|
|
|
end
|
|
|
|
|
2016-05-24 17:53:53 -04:00
|
|
|
def allowed_visibility_level
|
2017-12-20 10:19:54 -05:00
|
|
|
target_level = [@project.visibility_level, target_namespace.visibility_level].min
|
2016-05-24 17:53:53 -04:00
|
|
|
|
2017-12-20 10:19:54 -05:00
|
|
|
Gitlab::VisibilityLevel.closest_allowed_level(target_level)
|
2016-05-24 17:53:53 -04:00
|
|
|
end
|
2013-03-19 11:37:50 -04:00
|
|
|
end
|
|
|
|
end
|