diff options
Diffstat (limited to 'lib/api')
-rw-r--r-- | lib/api/api.rb | 1 | ||||
-rw-r--r-- | lib/api/group_import.rb | 77 | ||||
-rw-r--r-- | lib/api/helpers/file_upload_helpers.rb | 15 | ||||
-rw-r--r-- | lib/api/project_import.rb | 9 |
4 files changed, 94 insertions, 8 deletions
diff --git a/lib/api/api.rb b/lib/api/api.rb index e75fd7e88a1..9a1e0e3f8e9 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -130,6 +130,7 @@ module API mount ::API::GroupBoards mount ::API::GroupClusters mount ::API::GroupExport + mount ::API::GroupImport mount ::API::GroupLabels mount ::API::GroupMilestones mount ::API::Groups diff --git a/lib/api/group_import.rb b/lib/api/group_import.rb new file mode 100644 index 00000000000..de7fdc27243 --- /dev/null +++ b/lib/api/group_import.rb @@ -0,0 +1,77 @@ +# frozen_string_literal: true + +module API + class GroupImport < Grape::API + MAXIMUM_FILE_SIZE = 50.megabytes.freeze + + helpers do + def authorize_create_group! + parent_group = find_group!(params[:parent_id]) if params[:parent_id].present? + + if parent_group + authorize! :create_subgroup, parent_group + else + authorize! :create_group + end + end + 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 + + ImportExportUploader.workhorse_authorize(has_length: false, maximum_size: MAXIMUM_FILE_SIZE) + 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' + 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." + optional 'file.path', type: String, desc: 'Path to locally stored body (generated by Workhorse)' + optional 'file.name', type: String, desc: 'Real filename as send in Content-Disposition (generated by Workhorse)' + optional 'file.type', type: String, desc: 'Real content type as send in Content-Type (generated by Workhorse)' + optional 'file.size', type: Integer, desc: 'Real size of file (generated by Workhorse)' + optional 'file.md5', type: String, desc: 'MD5 checksum of the file (generated by Workhorse)' + optional 'file.sha1', type: String, desc: 'SHA1 checksum of the file (generated by Workhorse)' + optional 'file.sha256', type: String, desc: 'SHA256 checksum of the file (generated by Workhorse)' + end + post 'import' do + authorize_create_group! + require_gitlab_workhorse! + + uploaded_file = UploadedFile.from_params(params, :file, ImportExportUploader.workhorse_local_upload_path) + + bad_request!('Unable to process group import file') unless uploaded_file + + group_params = { + path: params[:path], + name: params[:name], + parent_id: params[:parent_id], + import_export_upload: ImportExportUpload.new(import_file: uploaded_file) + } + + group = ::Groups::CreateService.new(current_user, group_params).execute + + if group.persisted? + GroupImportWorker.perform_async(current_user.id, group.id) + + accepted! + else + render_api_error!("Failed to save group #{group.errors.messages}", 400) + end + end + end + end +end diff --git a/lib/api/helpers/file_upload_helpers.rb b/lib/api/helpers/file_upload_helpers.rb new file mode 100644 index 00000000000..c5fb291a2b7 --- /dev/null +++ b/lib/api/helpers/file_upload_helpers.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module API + module Helpers + module FileUploadHelpers + def file_is_valid? + params[:file] && params[:file]['tempfile'].respond_to?(:read) + end + + def validate_file! + render_api_error!('Uploaded file is invalid', 400) unless file_is_valid? + end + end + end +end diff --git a/lib/api/project_import.rb b/lib/api/project_import.rb index 7e0bd299761..ea793a09f6c 100644 --- a/lib/api/project_import.rb +++ b/lib/api/project_import.rb @@ -5,20 +5,13 @@ module API include PaginationParams helpers Helpers::ProjectsHelpers + helpers Helpers::FileUploadHelpers helpers do def import_params declared_params(include_missing: false) end - def file_is_valid? - import_params[:file] && import_params[:file]['tempfile'].respond_to?(:read) - end - - def validate_file! - render_api_error!('The file is invalid', 400) unless file_is_valid? - end - def throttled?(key, scope) rate_limiter.throttled?(key, scope: scope) end |