summaryrefslogtreecommitdiff
path: root/lib/api
diff options
context:
space:
mode:
authorJames Lopez <james@jameslopez.es>2018-02-12 12:42:33 +0100
committerJames Lopez <james@jameslopez.es>2018-02-13 15:25:47 +0100
commit82ff66ef31d6ff8ba2332f51b38f79b3bd7d64a5 (patch)
tree9ec9455ce776ad397d82e86c7c5eccd30e2569dd /lib/api
parent848f49801d2c45227c525fb5ecdf8b71e7711ded (diff)
downloadgitlab-ce-82ff66ef31d6ff8ba2332f51b38f79b3bd7d64a5.tar.gz
add post import API endpoint
Diffstat (limited to 'lib/api')
-rw-r--r--lib/api/project_import.rb48
1 files changed, 48 insertions, 0 deletions
diff --git a/lib/api/project_import.rb b/lib/api/project_import.rb
new file mode 100644
index 00000000000..4bbb78a62f9
--- /dev/null
+++ b/lib/api/project_import.rb
@@ -0,0 +1,48 @@
+module API
+ class ProjectImport < Grape::API
+ include PaginationParams
+
+ helpers do
+ def import_params
+ declared_params(include_missing: false)
+ end
+
+ def file_is_valid?
+ import_params[:file] && import_params[:file].respond_to?(:read)
+ end
+ end
+
+ before do
+ not_found! unless Gitlab::CurrentSettings.import_sources.include?('gitlab_project')
+ end
+
+ params do
+ optional :namespace, type: String, desc: 'The ID or name of the namespace that the project will be imported into. Defaults to the user namespace.'
+ requires :file, type: File, desc: 'The project export file to be imported'
+ end
+ resource :projects do
+ desc 'Get export status' do
+ success Entities::ProjectImportStatus
+ end
+ post 'import' do
+ render_api_error!('The branch refname is invalid', 400) unless file_is_valid?
+
+ namespace = import_params[:namespace]
+
+ namespace = if namespace && namespace =~ /^\d+$/
+ Namespace.find_by(id: namespace)
+ elsif namespace.blank?
+ current_user.namespace
+ else
+ Namespace.find_by_path_or_name(namespace)
+ end
+
+ project = ::Projects::GitlabProjectsImportService.new(current_user, import_params).execute
+
+ render_api_error!(link.project.full_messages.first, 400) unless project.saved?
+
+ present project, with: Entities::ProjectImportStatus
+ end
+ end
+ end
+end