1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
# frozen_string_literal: true
module API
class BulkImports < ::API::Base
include PaginationParams
feature_category :importers
urgency :low
helpers do
def bulk_imports
@bulk_imports ||= ::BulkImports::ImportsFinder.new(
user: current_user,
params: params.merge(include_configuration: true)
).execute
end
def bulk_import
@bulk_import ||= bulk_imports.find(params[:import_id])
end
def bulk_import_entities
@bulk_import_entities ||= ::BulkImports::EntitiesFinder.new(
user: current_user,
bulk_import: bulk_import,
params: params
).execute
end
def bulk_import_entity
@bulk_import_entity ||= bulk_import_entities.find(params[:entity_id])
end
end
before do
not_found! unless Gitlab::CurrentSettings.bulk_import_enabled? ||
Feature.enabled?(:override_bulk_import_disabled, current_user, type: :ops)
authenticate!
end
resource :bulk_imports do
desc 'Start a new GitLab Migration' do
detail 'This feature was introduced in GitLab 14.2.'
success code: 200, model: Entities::BulkImport
consumes ['application/x-www-form-urlencoded']
failure [
{ code: 401, message: 'Unauthorized' },
{ code: 400, message: 'Bad request' },
{ code: 404, message: 'Not found' },
{ code: 422, message: 'Unprocessable entity' },
{ code: 503, message: 'Service unavailable' }
]
end
params do
requires :configuration, type: Hash, desc: 'The source GitLab instance configuration' do
requires :url, type: String, desc: 'Source GitLab instance URL'
requires :access_token, type: String, desc: 'Access token to the source GitLab instance'
end
requires :entities, type: Array, desc: 'List of entities to import' do
requires :source_type,
type: String,
desc: 'Source entity type',
values: %w[group_entity project_entity]
requires :source_full_path,
type: String,
desc: 'Relative path of the source entity to import',
source_full_path: true,
documentation: { example: "'source/full/path' not 'https://example.com/source/full/path'" }
requires :destination_namespace,
type: String,
desc: 'Destination namespace for the entity',
destination_namespace_path: true,
documentation: { example: "'destination_namespace' or 'destination/namespace'" }
optional :destination_slug,
type: String,
desc: 'Destination slug for the entity',
destination_slug_path: true,
documentation: { example: "'destination_slug' not 'destination/slug'" }
optional :destination_name,
type: String,
desc: 'Deprecated: Use :destination_slug instead. Destination slug for the entity',
destination_slug_path: true,
documentation: { example: "'destination_slug' not 'destination/slug'" }
optional :migrate_projects,
type: Boolean,
default: true,
desc: 'Indicates group migration should include nested projects'
optional :migrate_memberships,
type: Boolean,
default: true,
desc: 'The option to migrate memberships or not'
mutually_exclusive :destination_slug, :destination_name
at_least_one_of :destination_slug, :destination_name
end
end
post do
check_rate_limit!(:bulk_import, scope: current_user)
params[:entities].each do |entity|
if entity[:destination_name]
entity[:destination_slug] ||= entity[:destination_name]
entity.delete(:destination_name)
end
end
response = ::BulkImports::CreateService.new(
current_user,
params[:entities],
url: params[:configuration][:url],
access_token: params[:configuration][:access_token]
).execute
if response.success?
present response.payload, with: Entities::BulkImport
else
render_api_error!(response.message, response.http_status)
end
end
desc 'List all GitLab Migrations' do
detail 'This feature was introduced in GitLab 14.1.'
is_array true
success code: 200, model: Entities::BulkImport
failure [
{ code: 401, message: 'Unauthorized' },
{ code: 404, message: 'Not found' },
{ code: 503, message: 'Service unavailable' }
]
end
params do
use :pagination
optional :sort, type: String, values: %w[asc desc], default: 'desc',
desc: 'Return GitLab Migrations sorted in created by `asc` or `desc` order.'
optional :status, type: String, values: BulkImport.all_human_statuses,
desc: 'Return GitLab Migrations with specified status'
end
get do
present paginate(bulk_imports), with: Entities::BulkImport
end
desc "List all GitLab Migrations' entities" do
detail 'This feature was introduced in GitLab 14.1.'
is_array true
success code: 200, model: Entities::BulkImports::Entity
failure [
{ code: 401, message: 'Unauthorized' },
{ code: 404, message: 'Not found' },
{ code: 503, message: 'Service unavailable' }
]
end
params do
use :pagination
optional :sort, type: String, values: %w[asc desc], default: 'desc',
desc: 'Return GitLab Migrations sorted in created by `asc` or `desc` order.'
optional :status, type: String, values: ::BulkImports::Entity.all_human_statuses,
desc: "Return all GitLab Migrations' entities with specified status"
end
get :entities do
entities = ::BulkImports::EntitiesFinder.new(
user: current_user,
params: params
).execute
present paginate(entities), with: Entities::BulkImports::Entity
end
desc 'Get GitLab Migration details' do
detail 'This feature was introduced in GitLab 14.1.'
success code: 200, model: Entities::BulkImport
failure [
{ code: 401, message: 'Unauthorized' },
{ code: 404, message: 'Not found' },
{ code: 503, message: 'Service unavailable' }
]
end
params do
requires :import_id, type: Integer, desc: "The ID of user's GitLab Migration"
end
get ':import_id' do
present bulk_import, with: Entities::BulkImport
end
desc "List GitLab Migration entities" do
detail 'This feature was introduced in GitLab 14.1.'
is_array true
success code: 200, model: Entities::BulkImports::Entity
failure [
{ code: 401, message: 'Unauthorized' },
{ code: 404, message: 'Not found' },
{ code: 503, message: 'Service unavailable' }
]
end
params do
requires :import_id, type: Integer, desc: "The ID of user's GitLab Migration"
optional :status, type: String, values: ::BulkImports::Entity.all_human_statuses,
desc: 'Return import entities with specified status'
use :pagination
end
get ':import_id/entities' do
present paginate(bulk_import_entities), with: Entities::BulkImports::Entity
end
desc 'Get GitLab Migration entity details' do
detail 'This feature was introduced in GitLab 14.1.'
success code: 200, model: Entities::BulkImports::Entity
failure [
{ code: 401, message: 'Unauthorized' },
{ code: 404, message: 'Not found' },
{ code: 503, message: 'Service unavailable' }
]
end
params do
requires :import_id, type: Integer, desc: "The ID of user's GitLab Migration"
requires :entity_id, type: Integer, desc: "The ID of GitLab Migration entity"
end
get ':import_id/entities/:entity_id' do
present bulk_import_entity, with: Entities::BulkImports::Entity
end
desc 'Get GitLab Migration entity failures' do
detail 'This feature was introduced in GitLab 16.6'
success code: 200, model: Entities::BulkImports::EntityFailure
failure [
{ code: 401, message: 'Unauthorized' },
{ code: 404, message: 'Not found' },
{ code: 503, message: 'Service unavailable' }
]
end
params do
requires :import_id, type: Integer, desc: "The ID of user's GitLab Migration"
requires :entity_id, type: Integer, desc: "The ID of GitLab Migration entity"
end
get ':import_id/entities/:entity_id/failures' do
present paginate(bulk_import_entity.failures), with: Entities::BulkImports::EntityFailure
end
desc 'Cancel GitLab Migration' do
detail 'This feature was introduced in GitLab 17.1'
success code: 200, model: Entities::BulkImport
failure [
{ code: 401, message: 'Unauthorized' },
{ code: 403, message: 'Forbidden' },
{ code: 404, message: 'Not found' },
{ code: 503, message: 'Service unavailable' }
]
end
params do
requires :import_id, type: Integer, desc: "The ID of user's GitLab Migration"
end
post ':import_id/cancel' do
bulk_import = BulkImport.find_by_id(params[:import_id])
not_found! unless bulk_import
authenticated_as_admin! unless bulk_import.user == current_user
bulk_import.cancel!
status :ok
present bulk_import, with: Entities::BulkImport
end
end
end
end
|