2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2018-03-27 19:54:05 +05:30
module API
class ProjectImport < Grape :: API
include PaginationParams
2019-09-04 21:01:54 +05:30
helpers Helpers :: ProjectsHelpers
2020-03-13 15:44:24 +05:30
helpers Helpers :: FileUploadHelpers
2018-03-27 19:54:05 +05:30
helpers do
def import_params
declared_params ( include_missing : false )
end
2020-03-13 15:44:24 +05:30
def throttled? ( key , scope )
rate_limiter . throttled? ( key , scope : scope )
2018-03-27 19:54:05 +05:30
end
2020-03-13 15:44:24 +05:30
def rate_limiter
:: Gitlab :: ApplicationRateLimiter
2018-03-27 19:54:05 +05:30
end
end
before do
forbidden! unless Gitlab :: CurrentSettings . import_sources . include? ( 'gitlab_project' )
end
2019-02-15 15:39:39 +05:30
resource :projects , requirements : API :: NAMESPACE_OR_PROJECT_REQUIREMENTS do
2018-03-27 19:54:05 +05:30
params do
requires :path , type : String , desc : 'The new project path and name'
2019-12-04 20:38:33 +05:30
# TODO: remove rubocop disable - https://gitlab.com/gitlab-org/gitlab/issues/14960
requires :file , type : File , desc : 'The project export file to be imported' # rubocop:disable Scalability/FileUploads
2019-12-21 20:55:43 +05:30
optional :name , type : String , desc : 'The name of the project to be imported. Defaults to the path of the project if not provided.'
2018-03-27 19:54:05 +05:30
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-05-09 12:01:36 +05:30
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
2018-03-27 19:54:05 +05:30
end
desc 'Create a new project import' do
detail 'This feature was introduced in GitLab 10.6.'
success Entities :: ProjectImportStatus
end
post 'import' do
2020-03-13 15:44:24 +05:30
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
2018-03-27 19:54:05 +05:30
validate_file!
2019-12-04 20:38:33 +05:30
Gitlab :: QueryLimiting . whitelist ( 'https://gitlab.com/gitlab-org/gitlab-foss/issues/42437' )
2018-03-27 19:54:05 +05:30
namespace = if import_params [ :namespace ]
find_namespace! ( import_params [ :namespace ] )
else
current_user . namespace
end
project_params = {
path : import_params [ :path ] ,
namespace_id : namespace . id ,
2019-12-21 20:55:43 +05:30
name : import_params [ :name ] ,
2018-05-09 12:01:36 +05:30
file : import_params [ :file ] [ 'tempfile' ] ,
overwrite : import_params [ :overwrite ]
2018-03-27 19:54:05 +05:30
}
2018-05-09 12:01:36 +05:30
override_params = import_params . delete ( :override_params )
2019-10-12 21:52:04 +05:30
filter_attributes_using_license! ( override_params ) if override_params
2018-05-09 12:01:36 +05:30
project = :: Projects :: GitlabProjectsImportService . new (
current_user , project_params , override_params
) . execute
2018-03-27 19:54:05 +05:30
render_api_error! ( project . errors . full_messages & . first , 400 ) unless project . saved?
present project , with : Entities :: ProjectImportStatus
end
params do
requires :id , type : String , desc : 'The ID of a project'
end
desc 'Get a project export status' do
detail 'This feature was introduced in GitLab 10.6.'
success Entities :: ProjectImportStatus
end
get ':id/import' do
present user_project , with : Entities :: ProjectImportStatus
end
end
end
end