debian-mirror-gitlab/lib/api/project_import.rb

248 lines
10 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2018-03-27 19:54:05 +05:30
module API
2021-01-03 14:25:43 +05:30
class ProjectImport < ::API::Base
2018-03-27 19:54:05 +05:30
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
2021-01-29 00:20:46 +05:30
feature_category :importers
2022-07-16 23:28:13 +05:30
urgency :low
2021-01-29 00:20:46 +05:30
2021-10-29 20:43:33 +05:30
before { authenticate! unless route.settings[:skip_authentication] }
2018-03-27 19:54:05 +05:30
helpers do
def import_params
declared_params(include_missing: false)
end
2021-06-08 01:23:25 +05:30
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-03-27 19:54:05 +05:30
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
2020-04-08 14:13:33 +05:30
desc 'Workhorse authorize the project import upload' do
detail 'This feature was introduced in GitLab 12.9'
2023-01-13 00:05:48 +05:30
tags ['project_import']
2020-04-08 14:13:33 +05:30
end
post 'import/authorize' do
require_gitlab_workhorse!
status 200
content_type Gitlab::Workhorse::INTERNAL_API_CONTENT_TYPE
2020-06-23 00:09:42 +05:30
ImportExportUploader.workhorse_authorize(
has_length: false,
maximum_size: Gitlab::CurrentSettings.max_import_size.megabytes
)
2020-04-08 14:13:33 +05:30
end
2018-03-27 19:54:05 +05:30
params do
requires :path, type: String, desc: 'The new project path and name'
2022-11-25 23:54:43 +05:30
requires :file, type: ::API::Validations::Types::WorkhorseFile, desc: 'The project export file to be imported', documentation: { type: 'file' }
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
2020-04-08 14:13:33 +05:30
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-03-27 19:54:05 +05:30
end
desc 'Create a new project import' do
detail 'This feature was introduced in GitLab 10.6.'
2023-01-13 00:05:48 +05:30
success code: 201, model: Entities::ProjectImportStatus
failure [
{ code: 401, message: 'Unauthorized' },
{ code: 403, message: 'Forbidden' },
{ code: 400, message: 'Bad request' },
{ code: 404, message: 'Not found' },
{ code: 503, message: 'Service unavailable' }
]
tags ['project_import']
consumes ['multipart/form-data']
2018-03-27 19:54:05 +05:30
end
post 'import' do
2020-04-08 14:13:33 +05:30
require_gitlab_workhorse!
2022-01-26 12:08:38 +05:30
check_rate_limit! :project_import, scope: [current_user, :project_import]
2020-03-13 15:44:24 +05:30
2021-06-08 01:23:25 +05:30
Gitlab::QueryLimiting.disable!('https://gitlab.com/gitlab-org/gitlab/-/issues/21041')
2018-03-27 19:54:05 +05:30
2020-04-08 14:13:33 +05:30
validate_file!
2022-05-07 20:08:51 +05:30
response = ::Import::GitlabProjects::CreateProjectService.new(
2021-06-08 01:23:25 +05:30
current_user,
2022-05-07 20:08:51 +05:30
params: {
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-05-09 12:01:36 +05:30
).execute
2018-03-27 19:54:05 +05:30
2021-06-08 01:23:25 +05:30
if response.success?
present(response.payload, with: Entities::ProjectImportStatus)
else
render_api_error!(response.message, response.http_status)
end
2018-03-27 19:54:05 +05:30
end
params do
2023-01-13 00:05:48 +05:30
requires :id, types: [String, Integer], desc: 'The ID or URL-encoded path of the project'
2018-03-27 19:54:05 +05:30
end
2022-01-26 12:08:38 +05:30
desc 'Get a project import status' do
2018-03-27 19:54:05 +05:30
detail 'This feature was introduced in GitLab 10.6.'
2023-01-13 00:05:48 +05:30
success code: 200, model: Entities::ProjectImportStatus
failure [
{ code: 401, message: 'Unauthorized' },
{ code: 403, message: 'Forbidden' },
{ code: 400, message: 'Bad request' },
{ code: 404, message: 'Not found' },
{ code: 503, message: 'Service unavailable' }
]
tags ['project_import']
2018-03-27 19:54:05 +05:30
end
2021-10-29 20:43:33 +05:30
route_setting :skip_authentication, true
2018-03-27 19:54:05 +05:30
get ':id/import' do
present user_project, with: Entities::ProjectImportStatus
end
2021-06-08 01:23:25 +05:30
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.'
2023-01-13 00:05:48 +05:30
consumes ['multipart/form-data']
tags ['project_import']
success code: 201, model: Entities::ProjectImportStatus
failure [
{ code: 401, message: 'Unauthorized' },
{ code: 403, message: 'Forbidden' },
{ code: 400, message: 'Bad request' },
{ code: 404, message: 'Not found' },
{ code: 429, message: 'Too many requests' },
{ code: 503, message: 'Service unavailable' }
]
2021-06-08 01:23:25 +05:30
end
post 'remote-import' do
2022-01-26 12:08:38 +05:30
check_rate_limit! :project_import, scope: [current_user, :project_import]
2021-06-08 01:23:25 +05:30
2022-05-07 20:08:51 +05:30
response = ::Import::GitlabProjects::CreateProjectService.new(
2021-06-08 01:23:25 +05:30
current_user,
2022-05-07 20:08:51 +05:30
params: {
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)
},
file_acquisition_strategy: ::Import::GitlabProjects::FileAcquisitionStrategies::RemoteFile
).execute
if response.success?
present(response.payload, with: Entities::ProjectImportStatus)
else
render_api_error!(response.message, response.http_status)
end
end
params do
requires :region, type: String, desc: 'AWS region'
requires :bucket_name, type: String, desc: 'Bucket name'
requires :file_key, type: String, desc: 'File key'
requires :access_key_id, type: String, desc: 'Access key id'
requires :secret_access_key, type: String, desc: 'Secret access key'
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 file from AWS S3' do
detail 'This feature was introduced in GitLab 14.9.'
2023-01-13 00:05:48 +05:30
consumes ['multipart/form-data']
tags ['project_import']
success code: 201, model: Entities::ProjectImportStatus
failure [
{ code: 401, message: 'Unauthorized' },
{ code: 403, message: 'Forbidden' },
{ code: 400, message: 'Bad request' },
{ code: 404, message: 'Not found' },
{ code: 429, message: 'Too many requests' },
{ code: 503, message: 'Service unavailable' }
]
2022-05-07 20:08:51 +05:30
end
post 'remote-import-s3' do
check_rate_limit! :project_import, scope: [current_user, :project_import]
response = ::Import::GitlabProjects::CreateProjectService.new(
current_user,
params: {
path: import_params[:path],
namespace: namespace_from(import_params, current_user),
name: import_params[:name],
overwrite: import_params[:overwrite],
override: filtered_override_params(import_params),
region: import_params[:region],
bucket_name: import_params[:bucket_name],
file_key: import_params[:file_key],
access_key_id: import_params[:access_key_id],
secret_access_key: import_params[:secret_access_key]
},
file_acquisition_strategy: ::Import::GitlabProjects::FileAcquisitionStrategies::RemoteFileS3
2021-06-08 01:23:25 +05:30
).execute
if response.success?
present(response.payload, with: Entities::ProjectImportStatus)
else
render_api_error!(response.message, response.http_status)
end
end
2018-03-27 19:54:05 +05:30
end
end
end