2018-07-17 16:50:37 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-03-19 15:37:50 +00:00
|
|
|
module Projects
|
2014-01-16 17:03:42 +00:00
|
|
|
class ForkService < BaseService
|
2017-12-07 08:44:55 +00: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)
|
|
|
|
|
|
|
|
fork_to_project
|
|
|
|
end
|
|
|
|
|
|
|
|
def fork_new_project
|
2015-04-24 19:37:12 +00:00
|
|
|
new_params = {
|
|
|
|
forked_from_project_id: @project.id,
|
2016-05-24 21:53:53 +00:00
|
|
|
visibility_level: allowed_visibility_level,
|
2015-04-24 19:37:12 +00:00
|
|
|
description: @project.description,
|
|
|
|
name: @project.name,
|
|
|
|
path: @project.path,
|
2015-12-04 11:55:23 +00:00
|
|
|
shared_runners_enabled: @project.shared_runners_enabled,
|
2017-12-20 15:19:54 +00:00
|
|
|
namespace_id: target_namespace.id
|
2014-08-25 09:19:52 +00:00
|
|
|
}
|
|
|
|
|
2015-04-24 19:37:12 +00:00
|
|
|
if @project.avatar.present? && @project.avatar.image?
|
|
|
|
new_params[:avatar] = @project.avatar
|
2014-10-03 08:12:44 +00:00
|
|
|
end
|
2014-11-13 17:40:47 +00:00
|
|
|
|
2015-04-24 19:37:12 +00:00
|
|
|
new_project = CreateService.new(current_user, new_params).execute
|
2016-09-19 18:28:41 +00:00
|
|
|
return new_project unless new_project.persisted?
|
|
|
|
|
2016-08-01 22:31:21 +00:00
|
|
|
builds_access_level = @project.project_feature.builds_access_level
|
2018-07-02 10:43:06 +00:00
|
|
|
new_project.project_feature.update(builds_access_level: builds_access_level)
|
2016-08-01 22:31:21 +00:00
|
|
|
|
2017-09-28 14:38:12 +00:00
|
|
|
link_fork_network(new_project)
|
|
|
|
|
2015-04-24 19:37:12 +00:00
|
|
|
new_project
|
2013-05-02 19:30:13 +00:00
|
|
|
end
|
2016-05-24 21:53:53 +00:00
|
|
|
|
2017-09-28 14:38:12 +00: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 08:44:55 +00:00
|
|
|
def link_fork_network(fork_to_project)
|
|
|
|
fork_network.fork_network_members.create(project: fork_to_project,
|
2017-09-28 14:38:12 +00:00
|
|
|
forked_from_project: @project)
|
2017-12-07 08:44:55 +00: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 14:38:12 +00:00
|
|
|
end
|
|
|
|
|
2017-08-14 13:22:09 +00:00
|
|
|
def refresh_forks_count
|
|
|
|
Projects::ForksCountService.new(@project).refresh_cache
|
|
|
|
end
|
|
|
|
|
2017-12-20 15:19:54 +00:00
|
|
|
def target_namespace
|
|
|
|
@target_namespace ||= @params[:namespace] || current_user.namespace
|
|
|
|
end
|
|
|
|
|
2016-05-24 21:53:53 +00:00
|
|
|
def allowed_visibility_level
|
2017-12-20 15:19:54 +00:00
|
|
|
target_level = [@project.visibility_level, target_namespace.visibility_level].min
|
2016-05-24 21:53:53 +00:00
|
|
|
|
2017-12-20 15:19:54 +00:00
|
|
|
Gitlab::VisibilityLevel.closest_allowed_level(target_level)
|
2016-05-24 21:53:53 +00:00
|
|
|
end
|
2013-03-19 15:37:50 +00:00
|
|
|
end
|
|
|
|
end
|