2018-02-12 06:42:33 -05:00
module API
class ProjectImport < Grape :: API
include PaginationParams
helpers do
def import_params
declared_params ( include_missing : false )
end
def file_is_valid?
2018-02-12 10:02:15 -05:00
import_params [ :file ] && import_params [ :file ] [ 'tempfile' ] . respond_to? ( :read )
2018-02-12 06:42:33 -05:00
end
2018-02-14 08:46:40 -05:00
def validate_file!
render_api_error! ( 'The file is invalid' , 400 ) unless file_is_valid?
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-02-14 08:46:40 -05:00
resource :projects , requirements : API :: PROJECT_ENDPOINT_REQUIREMENTS do
2018-02-13 03:24:10 -05:00
params do
requires :path , type : String , desc : 'The new project path and name'
requires :file , type : File , desc : 'The project export file to be imported'
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-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
2018-02-14 08:59:11 -05:00
validate_file!
2018-02-12 06:42:33 -05:00
2018-02-13 09:18:52 -05:00
Gitlab :: QueryLimiting . whitelist ( 'https://gitlab.com/gitlab-org/gitlab-ce/issues/42437' )
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
2018-02-16 12:03:34 -05:00
project_params = {
path : import_params [ :path ] ,
namespace_id : namespace . id ,
file : import_params [ :file ] [ 'tempfile' ]
}
2018-02-12 08:46:47 -05:00
project = :: Projects :: GitlabProjectsImportService . new ( current_user , project_params ) . execute
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