gitlab-org--gitlab-foss/app/controllers/projects/imports_controller.rb
Markus Koller 8fd2c08472
Make checks for continue_params more robust
The check for continue_params&.key?(:to) in Projects::ImportsController
caused an exception in redirect_to if this key contained a nil value.

Since url_for won't add any params for an empty hash, we can just return
that in continue_params if params[:continue] isn't present, and simplify
the code in the controllers to check for the values we actually want to
use.
2019-06-25 13:19:29 +02:00

75 lines
1.7 KiB
Ruby

# frozen_string_literal: true
class Projects::ImportsController < Projects::ApplicationController
include ContinueParams
include ImportUrlParams
# Authorize
before_action :authorize_admin_project!
before_action :require_no_repo, only: [:new, :create]
before_action :redirect_if_progress, only: [:new, :create]
before_action :redirect_if_no_import, only: :show
def new
end
def create
if @project.update(import_params)
@project.import_state.reset.schedule
end
redirect_to project_import_path(@project)
end
def show
if @project.import_finished?
if continue_params[:to]
redirect_to continue_params[:to], notice: continue_params[:notice]
else
redirect_to project_path(@project), notice: finished_notice
end
elsif @project.import_failed?
redirect_to new_project_import_path(@project)
else
flash.now[:notice] = continue_params[:notice_now]
end
end
private
def finished_notice
if @project.forked?
_('The project was successfully forked.')
else
_('The project was successfully imported.')
end
end
def require_no_repo
if @project.repository_exists?
redirect_to project_path(@project)
end
end
def redirect_if_progress
if @project.import_in_progress?
redirect_to project_import_path(@project)
end
end
def redirect_if_no_import
if @project.repository_exists? && @project.no_import?
redirect_to project_path(@project)
end
end
def import_params_attributes
[]
end
def import_params
params.require(:project)
.permit(import_params_attributes)
.merge(import_url_params)
end
end