BigW Consortium Gitlab

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

4
  def new
5 6
    @namespace = Namespace.find(project_params[:namespace_id])
    return render_404 unless current_user.can?(:create_projects, @namespace)
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

15
    import_upload_path = Gitlab::ImportExport.import_upload_path(filename: project_params[:file].original_filename)
16

17 18
    FileUtils.mkdir_p(File.dirname(import_upload_path))
    FileUtils.copy_entry(project_params[:file].path, import_upload_path)
19

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

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

35
  private
36

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

41 42 43
  def verify_gitlab_project_import_enabled
    render_404 unless gitlab_project_import_enabled?
  end
44 45

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