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

145 lines
4.4 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
2021-04-29 21:17:54 +05:30
before_action :disable_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
2021-01-03 14:25:43 +05:30
feature_category :source_code_management
2021-04-17 20:07:23 +05:30
before_action do
2021-09-30 23:02:18 +05:30
push_frontend_feature_flag(:fork_project_form, @project, default_enabled: :yaml)
2021-04-17 20:07:23 +05:30
end
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
2021-04-29 21:17:54 +05:30
@own_namespace = current_user.namespace if can_fork_to?(current_user.namespace)
2020-10-24 23:57:45 +05:30
@project = project
end
format.json do
2020-11-24 15:15:51 +05:30
namespaces = load_namespaces_with_associations - [project.namespace]
2021-04-29 21:17:54 +05:30
namespaces = [current_user.namespace] + namespaces if
Feature.enabled?(:fork_project_form, project, default_enabled: :yaml) &&
can_fork_to?(current_user.namespace)
2020-10-24 23:57:45 +05:30
render json: {
2021-09-04 01:27:46 +05:30
namespaces: ForkNamespaceSerializer.new.represent(
namespaces,
project: project,
current_user: current_user,
memberships: memberships_hash,
forked_projects: forked_projects_by_namespace(namespaces)
)
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
2021-04-29 21:17:54 +05:30
def can_fork_to?(namespace)
ForkTargetsFinder.new(@project, current_user).execute.id_in(current_user.namespace).any?
end
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
2021-03-11 19:13:27 +05:30
::Projects::ForkService.new(project, current_user, fork_params)
2020-04-08 14:13:33 +05:30
end
end
def fork_namespace
strong_memoize(:fork_namespace) do
Namespace.find(params[:namespace_key]) if params[:namespace_key].present?
end
end
2021-03-11 19:13:27 +05:30
def fork_params
params.permit(:path, :name, :description, :visibility).tap do |param|
param[:namespace] = fork_namespace
end
end
2020-04-08 14:13:33 +05:30
def authorize_fork_namespace!
access_denied! unless fork_namespace && fork_service.valid_fork_target?
end
2021-04-29 21:17:54 +05:30
def disable_query_limiting
Gitlab::QueryLimiting.disable!('https://gitlab.com/gitlab-org/gitlab/-/issues/20783')
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
2021-09-04 01:27:46 +05:30
def forked_projects_by_namespace(namespaces)
project.forks.where(namespace: namespaces).includes(:namespace).index_by(&:namespace_id)
end
2015-04-26 12:48:37 +05:30
end
2020-04-22 19:07:51 +05:30
2021-06-08 01:23:25 +05:30
Projects::ForksController.prepend_mod_with('Projects::ForksController')