Read about our upcoming Code of Conduct on this issue

imports_controller.rb 1.68 KB
Newer Older
1
2
# frozen_string_literal: true

3
class Projects::ImportsController < Projects::ApplicationController
4
  include ContinueParams
5

6
  # Authorize
7
  before_action :authorize_admin_project!
8
9
  before_action :require_no_repo, only: [:new, :create]
  before_action :redirect_if_progress, only: [:new, :create]
10
  before_action :redirect_if_no_import, only: :show
11
12
13
14
15

  def new
  end

  def create
16
17
    if @project.update(safe_import_params)
      @project.import_state.reload.schedule
18
19
    end

20
    redirect_to project_import_path(@project)
21
22
23
  end

  def show
24
    if @project.import_finished?
25
      if continue_params && continue_params[:to]
26
        redirect_to continue_params[:to], notice: continue_params[:notice]
27
      else
28
        redirect_to project_path(@project), notice: finished_notice
29
      end
30
    elsif @project.import_failed?
31
      redirect_to new_project_import_path(@project)
32
33
34
35
    else
      if continue_params && continue_params[:notice_now]
        flash.now[:notice] = continue_params[:notice_now]
      end
36

37
      # Render
38
39
40
41
42
    end
  end

  private

43
44
45
46
47
48
49
50
  def finished_notice
    if @project.forked?
      'The project was successfully forked.'
    else
      'The project was successfully imported.'
    end
  end

51
  def require_no_repo
52
    if @project.repository_exists?
53
      redirect_to project_path(@project)
54
55
56
57
58
    end
  end

  def redirect_if_progress
    if @project.import_in_progress?
59
      redirect_to project_import_path(@project)
60
61
62
63
64
    end
  end

  def redirect_if_no_import
    if @project.repository_exists? && @project.no_import?
65
      redirect_to project_path(@project)
66
67
    end
  end
68
69
70
71
72
73
74
75

  def import_params
    params.require(:project).permit(:import_url)
  end

  def safe_import_params
    import_params
  end
76
end