BigW Consortium Gitlab

gitlab_projects_controller.rb 1.51 KB
Newer Older
1
class Import::GitlabProjectsController < Import::BaseController
2 3
  before_action :verify_gitlab_project_import_enabled

4
  def new
5
    @namespace_id = project_params[:namespace_id]
6
    @namespace_name = Namespace.find(project_params[:namespace_id]).name
7
    @path = project_params[:path]
8 9
  end

10
  def create
11 12 13 14
    unless file_is_valid?
      return redirect_back_or_default(options: { alert: "You need to upload a GitLab project export archive." })
    end

James Lopez committed
15
    imported_file = project_params[:file].path + "-import"
16

James Lopez committed
17
    FileUtils.copy_entry(project_params[:file].path, imported_file)
18

19
    @project = Gitlab::ImportExport::ProjectCreator.new(project_params[:namespace_id],
20
                                                        current_user,
21
                                                        File.expand_path(imported_file),
22
                                                        project_params[:path]).execute
23 24 25 26 27 28 29

    if @project.saved?
      redirect_to(
        project_path(@project),
        notice: "Project '#{@project.name}' is being imported."
      )
    else
30
      redirect_to(
31 32
        new_import_gitlab_project_path,
        alert: "Project could not be imported: #{@project.errors.full_messages.join(', ')}"
33
      )
34
    end
35 36
  end

37
  private
38

39
  def file_is_valid?
40
    project_params[:file] && project_params[:file].respond_to?(:read)
41 42
  end

43 44 45
  def verify_gitlab_project_import_enabled
    render_404 unless gitlab_project_import_enabled?
  end
46 47

  def project_params
48
    params.permit(
49
      :path, :namespace_id, :file
50 51
    )
  end
52
end