aef9f1eb94
The number of forks of a project doesn't change very frequently and running a COUNT(*) every time this information is requested can be quite expensive. We also end up running such a COUNT(*) query at least twice on the homepage of a project. By caching this data and refreshing it when necessary we can reduce project homepage loading times by around 60 milliseconds (based on the timings of https://gitlab.com/gitlab-org/gitlab-ce).
45 lines
1.3 KiB
Ruby
45 lines
1.3 KiB
Ruby
module Projects
|
|
class ForkService < BaseService
|
|
def execute
|
|
new_params = {
|
|
forked_from_project_id: @project.id,
|
|
visibility_level: allowed_visibility_level,
|
|
description: @project.description,
|
|
name: @project.name,
|
|
path: @project.path,
|
|
shared_runners_enabled: @project.shared_runners_enabled,
|
|
namespace_id: @params[:namespace].try(:id) || current_user.namespace.id
|
|
}
|
|
|
|
if @project.avatar.present? && @project.avatar.image?
|
|
new_params[:avatar] = @project.avatar
|
|
end
|
|
|
|
new_project = CreateService.new(current_user, new_params).execute
|
|
return new_project unless new_project.persisted?
|
|
|
|
builds_access_level = @project.project_feature.builds_access_level
|
|
new_project.project_feature.update_attributes(builds_access_level: builds_access_level)
|
|
|
|
refresh_forks_count
|
|
|
|
new_project
|
|
end
|
|
|
|
private
|
|
|
|
def refresh_forks_count
|
|
Projects::ForksCountService.new(@project).refresh_cache
|
|
end
|
|
|
|
def allowed_visibility_level
|
|
project_level = @project.visibility_level
|
|
|
|
if Gitlab::VisibilityLevel.non_restricted_level?(project_level)
|
|
project_level
|
|
else
|
|
Gitlab::VisibilityLevel.highest_allowed_level
|
|
end
|
|
end
|
|
end
|
|
end
|