summaryrefslogtreecommitdiff
path: root/app/controllers/import/gitlab_groups_controller.rb
blob: aca71f6d57acefa43f6fdabe8efdae2a743d3243 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# frozen_string_literal: true

class Import::GitlabGroupsController < ApplicationController
  include WorkhorseAuthorization

  before_action :ensure_group_import_enabled
  before_action :check_import_rate_limit!, only: %i[create]

  feature_category :importers

  def create
    unless file_is_valid?(group_params[:file])
      return redirect_to new_group_path(anchor: 'import-group-pane'), alert: s_('GroupImport|Unable to process group import file')
    end

    group_data = group_params.except(:file).merge(
      visibility_level: closest_allowed_visibility_level,
      import_export_upload: ImportExportUpload.new(import_file: group_params[:file])
    )

    group = ::Groups::CreateService.new(current_user, group_data).execute

    if group.persisted?
      if Groups::ImportExport::ImportService.new(group: group, user: current_user).async_execute
        redirect_to(
          group_path(group),
          notice: s_("GroupImport|Group '%{group_name}' is being imported.") % { group_name: group.name }
        )
      else
        redirect_to group_path(group), alert: _("Group import could not be scheduled")
      end
    else
      redirect_to new_group_path(anchor: 'import-group-pane'),
        alert: s_("GroupImport|Group could not be imported: %{errors}") % { errors: group.errors.full_messages.to_sentence }
    end
  end

  private

  def group_params
    params.permit(:path, :name, :parent_id, :file)
  end

  def closest_allowed_visibility_level
    if group_params[:parent_id].present?
      parent_group = Group.find(group_params[:parent_id])

      Gitlab::VisibilityLevel.closest_allowed_level(parent_group.visibility_level)
    else
      Gitlab::VisibilityLevel::PRIVATE
    end
  end

  def ensure_group_import_enabled
    render_404 unless Feature.enabled?(:group_import_export, @group, default_enabled: true)
  end

  def check_import_rate_limit!
    check_rate_limit!(:group_import, scope: current_user) do
      redirect_to new_group_path, alert: _('This endpoint has been requested too many times. Try again later.')
    end
  end

  def uploader_class
    ImportExportUploader
  end

  def maximum_size
    Gitlab::CurrentSettings.max_import_size.megabytes
  end
end