debian-mirror-gitlab/app/controllers/projects/forks_controller.rb

115 lines
3.5 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2015-04-26 12:48:37 +05:30
class Projects::ForksController < Projects::ApplicationController
2016-06-02 11:05:42 +05:30
include ContinueParams
2019-12-04 20:38:33 +05:30
include RendersMemberAccess
2020-07-28 23:09:34 +05:30
include RendersProjectsList
2020-04-08 14:13:33 +05:30
include Gitlab::Utils::StrongMemoize
2016-06-02 11:05:42 +05:30
2015-04-26 12:48:37 +05:30
# Authorize
2018-03-17 18:26:18 +05:30
before_action :whitelist_query_limiting, only: [:create]
2015-09-11 14:41:01 +05:30
before_action :require_non_empty_project
before_action :authorize_download_code!
2017-08-17 22:00:37 +05:30
before_action :authenticate_user!, only: [:new, :create]
2020-03-13 15:44:24 +05:30
before_action :authorize_fork_project!, only: [:new, :create]
2020-04-08 14:13:33 +05:30
before_action :authorize_fork_namespace!, only: [:create]
2015-04-26 12:48:37 +05:30
2016-04-02 18:10:28 +05:30
def index
2019-12-04 20:38:33 +05:30
@total_forks_count = project.forks.size
@public_forks_count = project.forks.public_only.size
@private_forks_count = @total_forks_count - project.forks.public_and_internal_only.size
@internal_forks_count = @total_forks_count - @public_forks_count - @private_forks_count
2016-04-02 18:10:28 +05:30
2020-04-22 19:07:51 +05:30
@forks = load_forks.page(params[:page])
2016-04-02 18:10:28 +05:30
2019-12-04 20:38:33 +05:30
prepare_projects_for_rendering(@forks)
2016-06-02 11:05:42 +05:30
respond_to do |format|
format.html
format.json do
render json: {
html: view_to_html_string("projects/forks/_projects", projects: @forks)
}
end
end
2016-04-02 18:10:28 +05:30
end
2015-04-26 12:48:37 +05:30
def new
2020-10-24 23:57:45 +05:30
respond_to do |format|
format.html do
@own_namespace = current_user.namespace if fork_service.valid_fork_targets.include?(current_user.namespace)
@project = project
end
format.json do
2020-11-24 15:15:51 +05:30
namespaces = load_namespaces_with_associations - [project.namespace]
2020-10-24 23:57:45 +05:30
render json: {
2020-11-24 15:15:51 +05:30
namespaces: ForkNamespaceSerializer.new.represent(namespaces, project: project, current_user: current_user, memberships: memberships_hash)
2020-10-24 23:57:45 +05:30
}
end
end
2015-04-26 12:48:37 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2015-04-26 12:48:37 +05:30
def create
2020-04-08 14:13:33 +05:30
@forked_project = fork_namespace.projects.find_by(path: project.path)
@forked_project = nil unless @forked_project && @forked_project.forked_from_project == project
2020-04-08 14:13:33 +05:30
@forked_project ||= fork_service.execute
2015-04-26 12:48:37 +05:30
2019-09-30 21:07:59 +05:30
if !@forked_project.saved? || !@forked_project.forked?
2015-04-26 12:48:37 +05:30
render :error
2019-09-30 21:07:59 +05:30
elsif @forked_project.import_in_progress?
redirect_to project_import_path(@forked_project, continue: continue_params)
elsif continue_params[:to]
redirect_to continue_params[:to], notice: continue_params[:notice]
else
redirect_to project_path(@forked_project), notice: "The project '#{@forked_project.name}' was successfully forked."
2015-04-26 12:48:37 +05:30
end
end
2018-03-17 18:26:18 +05:30
2020-03-13 15:44:24 +05:30
private
2020-04-22 19:07:51 +05:30
def load_forks
forks = ForkProjectsFinder.new(
project,
params: params.merge(search: params[:filter_projects]),
current_user: current_user
).execute
forks.includes(:route, :creator, :group, namespace: [:route, :owner])
end
2020-04-08 14:13:33 +05:30
def fork_service
strong_memoize(:fork_service) do
::Projects::ForkService.new(project, current_user, namespace: fork_namespace)
end
end
def fork_namespace
strong_memoize(:fork_namespace) do
Namespace.find(params[:namespace_key]) if params[:namespace_key].present?
end
end
def authorize_fork_namespace!
access_denied! unless fork_namespace && fork_service.valid_fork_target?
end
2018-03-17 18:26:18 +05:30
def whitelist_query_limiting
2019-12-04 20:38:33 +05:30
Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab-foss/issues/42335')
2018-03-17 18:26:18 +05:30
end
2020-11-24 15:15:51 +05:30
def load_namespaces_with_associations
@load_namespaces_with_associations ||= fork_service.valid_fork_targets(only_groups: true).preload(:route)
end
def memberships_hash
current_user.members.where(source: load_namespaces_with_associations).index_by(&:source_id)
end
2015-04-26 12:48:37 +05:30
end
2020-04-22 19:07:51 +05:30
Projects::ForksController.prepend_if_ee('EE::Projects::ForksController')