2018-09-29 18:34:47 -04:00
# frozen_string_literal: true
2018-02-12 06:42:33 -05:00
module API
class ProjectImport < Grape :: API
include PaginationParams
2019-05-30 17:55:17 -04:00
2020-02-27 04:09:01 -05:00
MAXIMUM_FILE_SIZE = 50 . megabytes
2019-05-30 17:55:17 -04:00
helpers Helpers :: ProjectsHelpers
2020-02-11 04:08:39 -05:00
helpers Helpers :: FileUploadHelpers
2018-02-12 06:42:33 -05:00
helpers do
def import_params
declared_params ( include_missing : false )
end
2020-01-27 04:08:32 -05:00
def throttled? ( key , scope )
rate_limiter . throttled? ( key , scope : scope )
end
def rate_limiter
:: Gitlab :: ApplicationRateLimiter
end
2020-02-27 04:09:01 -05:00
def with_workhorse_upload_acceleration?
request . headers [ Gitlab :: Workhorse :: INTERNAL_API_REQUEST_HEADER ] . present?
end
2018-02-12 06:42:33 -05:00
end
before do
2018-02-13 09:18:52 -05:00
forbidden! unless Gitlab :: CurrentSettings . import_sources . include? ( 'gitlab_project' )
2018-02-12 06:42:33 -05:00
end
2018-11-08 07:18:17 -05:00
resource :projects , requirements : API :: NAMESPACE_OR_PROJECT_REQUIREMENTS do
2020-02-27 04:09:01 -05:00
desc 'Workhorse authorize the project import upload' do
detail 'This feature was introduced in GitLab 12.9'
end
post 'import/authorize' do
require_gitlab_workhorse!
status 200
content_type Gitlab :: Workhorse :: INTERNAL_API_CONTENT_TYPE
ImportExportUploader . workhorse_authorize ( has_length : false , maximum_size : MAXIMUM_FILE_SIZE )
end
2018-02-13 03:24:10 -05:00
params do
requires :path , type : String , desc : 'The new project path and name'
2019-09-18 10:02:45 -04:00
# TODO: remove rubocop disable - https://gitlab.com/gitlab-org/gitlab/issues/14960
2020-02-27 04:09:01 -05:00
# and mark WH fields as required (instead of optional) after the WH version including
# https://gitlab.com/gitlab-org/gitlab-workhorse/-/merge_requests/459
# is deployed and GITLAB_WORKHORSE_VERSION is updated accordingly.
requires :file , types : [ :: API :: Validations :: Types :: WorkhorseFile , File ] , desc : 'The project export file to be imported' # rubocop:disable Scalability/FileUploads
2019-09-19 20:05:59 -04:00
optional :name , type : String , desc : 'The name of the project to be imported. Defaults to the path of the project if not provided.'
2018-02-16 08:37:26 -05:00
optional :namespace , type : String , desc : " The ID or name of the namespace that the project will be imported into. Defaults to the current user's namespace. "
2018-04-06 11:23:49 -04:00
optional :overwrite , type : Boolean , default : false , desc : 'If there is a project in the same namespace and with the same name overwrite it'
2018-03-29 07:57:21 -04:00
optional :override_params ,
type : Hash ,
desc : 'New project params to override values in the export' do
use :optional_project_params
end
2020-02-27 04:09:01 -05:00
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)'
optional 'file.etag' , type : String , desc : 'Etag of the file (generated by Workhorse)'
optional 'file.remote_id' , type : String , desc : 'Remote_id of the file (generated by Workhorse)'
optional 'file.remote_url' , type : String , desc : 'Remote_url of the file (generated by Workhorse)'
2018-02-13 03:24:10 -05:00
end
2018-02-13 09:18:52 -05:00
desc 'Create a new project import' do
2018-02-14 08:46:40 -05:00
detail 'This feature was introduced in GitLab 10.6.'
2018-02-12 06:42:33 -05:00
success Entities :: ProjectImportStatus
end
post 'import' do
2020-02-27 04:09:01 -05:00
require_gitlab_workhorse! if with_workhorse_upload_acceleration?
2020-01-27 04:08:32 -05:00
key = " project_import " . to_sym
if throttled? ( key , [ current_user , key ] )
rate_limiter . log_request ( request , " #{ key } _request_limit " . to_sym , current_user )
render_api_error! ( { error : _ ( 'This endpoint has been requested too many times. Try again later.' ) } , 429 )
end
2019-09-18 10:02:45 -04:00
Gitlab :: QueryLimiting . whitelist ( 'https://gitlab.com/gitlab-org/gitlab-foss/issues/42437' )
2018-02-13 09:18:52 -05:00
2018-02-14 08:46:40 -05:00
namespace = if import_params [ :namespace ]
find_namespace! ( import_params [ :namespace ] )
2018-02-12 06:42:33 -05:00
else
2018-02-14 08:46:40 -05:00
current_user . namespace
2018-02-12 06:42:33 -05:00
end
2020-02-27 04:09:01 -05:00
# TODO: remove the condition after the WH version including
# https://gitlab.com/gitlab-org/gitlab-workhorse/-/merge_requests/459
# is deployed and GITLAB_WORKHORSE_VERSION is updated accordingly.
file = if with_workhorse_upload_acceleration?
import_params [ :file ] || bad_request! ( 'Unable to process project import file' )
else
validate_file!
import_params [ :file ] [ 'tempfile' ]
end
2018-02-16 12:03:34 -05:00
project_params = {
path : import_params [ :path ] ,
namespace_id : namespace . id ,
2019-09-19 20:05:59 -04:00
name : import_params [ :name ] ,
2020-02-27 04:09:01 -05:00
file : file ,
2018-04-06 11:23:49 -04:00
overwrite : import_params [ :overwrite ]
2018-02-16 12:03:34 -05:00
}
2018-03-29 07:57:21 -04:00
override_params = import_params . delete ( :override_params )
2019-08-07 10:01:51 -04:00
filter_attributes_using_license! ( override_params ) if override_params
2018-03-29 07:57:21 -04:00
project = :: Projects :: GitlabProjectsImportService . new (
current_user , project_params , override_params
) . execute
2018-02-12 08:46:47 -05:00
2018-02-13 04:54:04 -05:00
render_api_error! ( project . errors . full_messages & . first , 400 ) unless project . saved?
2018-02-12 06:42:33 -05:00
present project , with : Entities :: ProjectImportStatus
end
2018-02-13 03:24:10 -05:00
params do
requires :id , type : String , desc : 'The ID of a project'
end
2018-02-13 09:18:52 -05:00
desc 'Get a project export status' do
2018-02-14 08:46:40 -05:00
detail 'This feature was introduced in GitLab 10.6.'
2018-02-13 03:24:10 -05:00
success Entities :: ProjectImportStatus
end
get ':id/import' do
present user_project , with : Entities :: ProjectImportStatus
end
2018-02-12 06:42:33 -05:00
end
end
end