2018-09-29 22:34:47 +00:00
# frozen_string_literal: true
2018-02-12 11:42:33 +00:00
module API
2020-10-15 00:08:42 +00:00
class ProjectImport < :: API :: Base
2018-02-12 11:42:33 +00:00
include PaginationParams
2019-05-30 21:55:17 +00:00
helpers Helpers :: ProjectsHelpers
2020-02-11 09:08:39 +00:00
helpers Helpers :: FileUploadHelpers
2020-04-15 15:09:17 +00:00
helpers Helpers :: RateLimiter
2018-02-12 11:42:33 +00:00
2020-10-30 15:08:59 +00:00
feature_category :importers
2018-02-12 11:42:33 +00:00
helpers do
def import_params
declared_params ( include_missing : false )
end
2021-05-05 21:09:59 +00:00
def namespace_from ( params , current_user )
if params [ :namespace ]
find_namespace! ( params [ :namespace ] )
else
current_user . namespace
end
end
def filtered_override_params ( params )
override_params = params . delete ( :override_params )
filter_attributes_using_license! ( override_params ) if override_params
override_params
end
2018-02-12 11:42:33 +00:00
end
before do
2018-02-13 14:18:52 +00:00
forbidden! unless Gitlab :: CurrentSettings . import_sources . include? ( 'gitlab_project' )
2018-02-12 11:42:33 +00:00
end
2018-11-08 12:18:17 +00:00
resource :projects , requirements : API :: NAMESPACE_OR_PROJECT_REQUIREMENTS do
2020-02-27 09:09:01 +00: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
2020-06-09 15:08:05 +00:00
ImportExportUploader . workhorse_authorize (
has_length : false ,
maximum_size : Gitlab :: CurrentSettings . max_import_size . megabytes
)
2020-02-27 09:09:01 +00:00
end
2018-02-13 08:24:10 +00:00
params do
requires :path , type : String , desc : 'The new project path and name'
2020-03-13 18:09:39 +00:00
requires :file , type : :: API :: Validations :: Types :: WorkhorseFile , desc : 'The project export file to be imported'
2019-09-20 00:05:59 +00: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 13:37:26 +00: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 15:23:49 +00: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 11:57:21 +00: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 09:09:01 +00: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 08:24:10 +00:00
end
2018-02-13 14:18:52 +00:00
desc 'Create a new project import' do
2018-02-14 13:46:40 +00:00
detail 'This feature was introduced in GitLab 10.6.'
2018-02-12 11:42:33 +00:00
success Entities :: ProjectImportStatus
end
post 'import' do
2020-03-13 18:09:39 +00:00
require_gitlab_workhorse!
2020-02-27 09:09:01 +00:00
2020-04-15 15:09:17 +00:00
check_rate_limit! :project_import , [ current_user , :project_import ]
2020-01-27 09:08:32 +00:00
2021-05-05 21:09:59 +00:00
Gitlab :: QueryLimiting . disable! ( 'https://gitlab.com/gitlab-org/gitlab/-/issues/21041' )
2018-02-13 14:18:52 +00:00
2020-03-13 18:09:39 +00:00
validate_file!
2021-05-05 21:09:59 +00:00
response = :: Import :: GitlabProjects :: CreateProjectFromUploadedFileService . new (
current_user ,
path : import_params [ :path ] ,
namespace : namespace_from ( import_params , current_user ) ,
name : import_params [ :name ] ,
file : import_params [ :file ] ,
overwrite : import_params [ :overwrite ] ,
override : filtered_override_params ( import_params )
2018-03-29 11:57:21 +00:00
) . execute
2018-02-12 13:46:47 +00:00
2021-05-05 21:09:59 +00:00
if response . success?
present ( response . payload , with : Entities :: ProjectImportStatus )
else
render_api_error! ( response . message , response . http_status )
end
2018-02-12 11:42:33 +00:00
end
2018-02-13 08:24:10 +00:00
params do
requires :id , type : String , desc : 'The ID of a project'
end
2018-02-13 14:18:52 +00:00
desc 'Get a project export status' do
2018-02-14 13:46:40 +00:00
detail 'This feature was introduced in GitLab 10.6.'
2018-02-13 08:24:10 +00:00
success Entities :: ProjectImportStatus
end
get ':id/import' do
present user_project , with : Entities :: ProjectImportStatus
end
2021-05-05 21:09:59 +00:00
params do
requires :url , type : String , desc : 'The URL for the file.'
requires :path , type : String , desc : 'The new project path and name'
optional :name , type : String , desc : 'The name of the project to be imported. Defaults to the path of the project if not provided.'
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. "
optional :overwrite , type : Boolean , default : false , desc : 'If there is a project in the same namespace and with the same name overwrite it'
optional :override_params ,
type : Hash ,
desc : 'New project params to override values in the export' do
use :optional_project_params
end
end
desc 'Create a new project import using a remote object storage path' do
detail 'This feature was introduced in GitLab 13.2.'
success Entities :: ProjectImportStatus
end
post 'remote-import' do
not_found! unless :: Feature . enabled? ( :import_project_from_remote_file )
check_rate_limit! :project_import , [ current_user , :project_import ]
response = :: Import :: GitlabProjects :: CreateProjectFromRemoteFileService . new (
current_user ,
path : import_params [ :path ] ,
namespace : namespace_from ( import_params , current_user ) ,
name : import_params [ :name ] ,
remote_import_url : import_params [ :url ] ,
overwrite : import_params [ :overwrite ] ,
override : filtered_override_params ( import_params )
) . execute
if response . success?
present ( response . payload , with : Entities :: ProjectImportStatus )
else
render_api_error! ( response . message , response . http_status )
end
end
2018-02-12 11:42:33 +00:00
end
end
end