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

88 lines
2.7 KiB
Ruby
Raw Normal View History

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
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-09-11 14:41:01 +05:30
new_params = {
forked_from_project_id: @project.id,
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-03-17 18:26:18 +05:30
namespace_id: target_namespace.id
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
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?
2016-09-29 09:46:39 +05:30
builds_access_level = @project.project_feature.builds_access_level
new_project.project_feature.update_attributes(builds_access_level: builds_access_level)
2018-03-17 18:26:18 +05:30
link_fork_network(new_project)
2017-09-10 17:25:29 +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
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
def link_fork_network(fork_to_project)
fork_network.fork_network_members.create(project: fork_to_project,
forked_from_project: @project)
# 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
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