imports_controller.rb 1.75 KB
Newer Older
1 2
class Projects::ImportsController < Projects::ApplicationController
  # Authorize
3
  before_action :authorize_admin_project!
4 5
  before_action :require_no_repo, only: [:new, :create]
  before_action :redirect_if_progress, only: [:new, :create]
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

  def new
  end

  def create
    @project.import_url = params[:project][:import_url]

    if @project.save
      @project.reload

      if @project.import_failed?
        @project.import_retry
      else
        @project.import_start
      end
    end

Vinnie Okada's avatar
Vinnie Okada committed
23
    redirect_to namespace_project_import_path(@project.namespace, @project)
24 25 26
  end

  def show
27
    if @project.import_finished?
28 29
      if continue_params
        redirect_to continue_params[:to], notice: continue_params[:notice]
30
      else
31
        redirect_to namespace_project_path(@project.namespace, @project), notice: finished_notice
32
      end
33 34 35 36 37 38
    elsif @project.import_failed?
      redirect_to new_namespace_project_import_path(@project.namespace, @project)
    else
      if continue_params && continue_params[:notice_now]
        flash.now[:notice] = continue_params[:notice_now]
      end
39

40
      # Render
41 42 43 44 45
    end
  end

  private

46
  def continue_params
47
    continue_params = params[:continue]
48

49 50 51 52 53
    if continue_params
      continue_params.permit(:to, :notice, :notice_now)
    else
      nil
    end
54 55
  end

56 57 58 59 60 61 62 63
  def finished_notice
    if @project.forked?
      'The project was successfully forked.'
    else
      'The project was successfully imported.'
    end
  end

64
  def require_no_repo
65
    if @project.repository_exists?
66
      redirect_to(namespace_project_path(@project.namespace, @project))
67 68 69 70 71
    end
  end

  def redirect_if_progress
    if @project.import_in_progress?
Vinnie Okada's avatar
Vinnie Okada committed
72 73
      redirect_to namespace_project_import_path(@project.namespace, @project) &&
        return
74 75 76
    end
  end
end