2020-02-11 04:08:39 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module API
|
2020-10-14 20:08:42 -04:00
|
|
|
class GroupImport < ::API::Base
|
2020-10-29 08:08:50 -04:00
|
|
|
feature_category :importers
|
2022-04-27 14:10:15 -04:00
|
|
|
urgency :low
|
2020-10-29 08:08:50 -04:00
|
|
|
|
2020-05-27 17:08:05 -04:00
|
|
|
helpers Helpers::FileUploadHelpers
|
|
|
|
|
2020-02-11 04:08:39 -05:00
|
|
|
helpers do
|
2020-02-14 10:09:08 -05:00
|
|
|
def parent_group
|
|
|
|
find_group!(params[:parent_id]) if params[:parent_id].present?
|
|
|
|
end
|
2020-02-11 04:08:39 -05:00
|
|
|
|
2020-02-14 10:09:08 -05:00
|
|
|
def authorize_create_group!
|
2020-02-11 04:08:39 -05:00
|
|
|
if parent_group
|
|
|
|
authorize! :create_subgroup, parent_group
|
|
|
|
else
|
|
|
|
authorize! :create_group
|
|
|
|
end
|
|
|
|
end
|
2020-02-14 10:09:08 -05:00
|
|
|
|
|
|
|
def closest_allowed_visibility_level
|
|
|
|
if parent_group
|
|
|
|
Gitlab::VisibilityLevel.closest_allowed_level(parent_group.visibility_level)
|
|
|
|
else
|
|
|
|
Gitlab::VisibilityLevel::PRIVATE
|
|
|
|
end
|
|
|
|
end
|
2020-02-11 04:08:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
resource :groups, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
|
|
|
desc 'Workhorse authorize the group import upload' do
|
|
|
|
detail 'This feature was introduced in GitLab 12.8'
|
|
|
|
end
|
|
|
|
post 'import/authorize' do
|
|
|
|
require_gitlab_workhorse!
|
|
|
|
|
|
|
|
Gitlab::Workhorse.verify_api_request!(headers)
|
|
|
|
|
|
|
|
status 200
|
|
|
|
content_type Gitlab::Workhorse::INTERNAL_API_CONTENT_TYPE
|
|
|
|
|
2020-05-21 05:08:04 -04:00
|
|
|
ImportExportUploader.workhorse_authorize(
|
|
|
|
has_length: false,
|
2020-06-09 11:08:05 -04:00
|
|
|
maximum_size: Gitlab::CurrentSettings.max_import_size.megabytes
|
2020-05-21 05:08:04 -04:00
|
|
|
)
|
2020-02-11 04:08:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Create a new group import' do
|
|
|
|
detail 'This feature was introduced in GitLab 12.8'
|
|
|
|
success Entities::Group
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :path, type: String, desc: 'Group path'
|
|
|
|
requires :name, type: String, desc: 'Group name'
|
2020-05-27 17:08:05 -04:00
|
|
|
requires :file, type: ::API::Validations::Types::WorkhorseFile, desc: 'The group export file to be imported'
|
2020-02-11 04:08:39 -05:00
|
|
|
optional :parent_id, type: Integer, desc: "The ID of the parent group that the group will be imported into. Defaults to the current user's namespace."
|
|
|
|
end
|
|
|
|
post 'import' do
|
|
|
|
authorize_create_group!
|
|
|
|
require_gitlab_workhorse!
|
2020-05-27 17:08:05 -04:00
|
|
|
validate_file!
|
2020-02-11 04:08:39 -05:00
|
|
|
|
|
|
|
group_params = {
|
|
|
|
path: params[:path],
|
|
|
|
name: params[:name],
|
|
|
|
parent_id: params[:parent_id],
|
2020-02-14 10:09:08 -05:00
|
|
|
visibility_level: closest_allowed_visibility_level,
|
2020-05-27 17:08:05 -04:00
|
|
|
import_export_upload: ImportExportUpload.new(import_file: params[:file])
|
2020-02-11 04:08:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
group = ::Groups::CreateService.new(current_user, group_params).execute
|
|
|
|
|
|
|
|
if group.persisted?
|
2020-05-21 05:08:04 -04:00
|
|
|
::Groups::ImportExport::ImportService.new(group: group, user: current_user).async_execute
|
2020-02-11 04:08:39 -05:00
|
|
|
|
|
|
|
accepted!
|
|
|
|
else
|
|
|
|
render_api_error!("Failed to save group #{group.errors.messages}", 400)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|