2018-09-25 23:45:43 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-11-13 15:06:19 -05:00
|
|
|
class Projects::ForksController < Projects::ApplicationController
|
2016-03-07 04:36:16 -05:00
|
|
|
include ContinueParams
|
2019-09-10 17:29:32 -04:00
|
|
|
include RendersMemberAccess
|
2020-02-20 04:09:13 -05:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2016-03-07 04:06:54 -05:00
|
|
|
|
2014-11-13 15:06:19 -05:00
|
|
|
# Authorize
|
2018-01-15 10:21:04 -05:00
|
|
|
before_action :whitelist_query_limiting, only: [:create]
|
2015-04-16 08:03:37 -04:00
|
|
|
before_action :require_non_empty_project
|
|
|
|
before_action :authorize_download_code!
|
2016-11-14 17:59:11 -05:00
|
|
|
before_action :authenticate_user!, only: [:new, :create]
|
2020-01-17 13:08:41 -05:00
|
|
|
before_action :authorize_fork_project!, only: [:new, :create]
|
2020-02-20 04:09:13 -05:00
|
|
|
before_action :authorize_fork_namespace!, only: [:create]
|
2014-11-13 15:06:19 -05:00
|
|
|
|
2019-09-10 17:29:32 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-01-11 14:44:31 -05:00
|
|
|
def index
|
2019-08-17 05:14:51 -04:00
|
|
|
@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-01-14 19:58:57 -05:00
|
|
|
|
2019-08-17 05:14:51 -04:00
|
|
|
@forks = ForkProjectsFinder.new(project, params: params.merge(search: params[:filter_projects]), current_user: current_user).execute
|
2019-09-10 17:29:32 -04:00
|
|
|
@forks = @forks.includes(:route, :creator, :group, namespace: [:route, :owner])
|
|
|
|
.page(params[:page])
|
|
|
|
|
|
|
|
prepare_projects_for_rendering(@forks)
|
2016-03-03 07:09:58 -05:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
|
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
html: view_to_html_string("projects/forks/_projects", projects: @forks)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2016-01-11 14:44:31 -05:00
|
|
|
end
|
2019-09-10 17:29:32 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-01-11 14:44:31 -05:00
|
|
|
|
2014-11-13 15:06:19 -05:00
|
|
|
def new
|
2020-03-03 13:08:16 -05:00
|
|
|
@namespaces = fork_service.valid_fork_targets - [project.namespace]
|
2014-11-13 15:06:19 -05:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2014-11-13 15:06:19 -05:00
|
|
|
def create
|
2020-02-20 04:09:13 -05:00
|
|
|
@forked_project = fork_namespace.projects.find_by(path: project.path)
|
2015-12-21 05:57:51 -05:00
|
|
|
@forked_project = nil unless @forked_project && @forked_project.forked_from_project == project
|
|
|
|
|
2020-02-20 04:09:13 -05:00
|
|
|
@forked_project ||= fork_service.execute
|
2014-11-13 15:06:19 -05:00
|
|
|
|
2019-06-20 13:13:02 -04:00
|
|
|
if !@forked_project.saved? || !@forked_project.forked?
|
2014-11-13 15:06:19 -05:00
|
|
|
render :error
|
2019-06-20 13:13:02 -04:00
|
|
|
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."
|
2014-11-13 15:06:19 -05:00
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-01-15 10:21:04 -05:00
|
|
|
|
2020-01-17 13:08:41 -05:00
|
|
|
private
|
|
|
|
|
2020-02-20 04:09:13 -05:00
|
|
|
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-01-15 10:21:04 -05:00
|
|
|
def whitelist_query_limiting
|
2019-09-18 10:02:45 -04:00
|
|
|
Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab-foss/issues/42335')
|
2018-01-15 10:21:04 -05:00
|
|
|
end
|
2014-11-13 15:06:19 -05:00
|
|
|
end
|