BigW Consortium Gitlab

gitlab_projects_controller.rb 1.7 KB
Newer Older
1
class Import::GitlabProjectsController < Import::BaseController
2
  before_action :verify_gitlab_project_import_enabled
3
  before_action :authenticate_admin!
4

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

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

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

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

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

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

36
  private
37

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

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

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

  def authenticate_admin!
    render_404 unless current_user.is_admin?
  end
55
end