File: project_import.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (350 lines) | stat: -rw-r--r-- 15,540 bytes parent folder | download
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# frozen_string_literal: true

module API
  class ProjectImport < ::API::Base
    include PaginationParams

    helpers Helpers::ProjectsHelpers
    helpers Helpers::FileUploadHelpers

    feature_category :importers
    urgency :low

    before { authenticate! }

    helpers do
      def import_params
        declared_params(include_missing: false)
      end

      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
    end

    resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
      desc 'Workhorse authorize the project import upload' do
        detail 'This feature was introduced in GitLab 12.9'
        tags ['project_import']
      end
      post 'import/authorize' do
        forbidden! unless Gitlab::CurrentSettings.import_sources.include?('gitlab_project')

        require_gitlab_workhorse!

        status 200
        content_type Gitlab::Workhorse::INTERNAL_API_CONTENT_TYPE

        ImportExportUploader.workhorse_authorize(
          has_length: false,
          maximum_size: Gitlab::CurrentSettings.max_import_size.megabytes
        )
      end

      params do
        requires :path, type: String, desc: 'The new project path and name'
        requires :file, type: ::API::Validations::Types::WorkhorseFile, desc: 'The project export file to be imported', documentation: { type: 'file' }
        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
        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)'
      end
      desc 'Create a new project import' do
        detail 'This feature was introduced in GitLab 10.6.'
        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']
      end
      post 'import' do
        forbidden! unless Gitlab::CurrentSettings.import_sources.include?('gitlab_project')

        require_gitlab_workhorse!

        check_rate_limit! :project_import, scope: [current_user, :project_import]

        Gitlab::QueryLimiting.disable!('https://gitlab.com/gitlab-org/gitlab/-/issues/21041')

        validate_file!

        response = ::Import::GitlabProjects::CreateProjectService.new(
          current_user,
          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)
          }
        ).execute

        if response.success?
          present(response.payload, with: Entities::ProjectImportStatus, current_user: current_user)
        else
          render_api_error!(response.message, response.http_status)
        end
      end

      params do
        requires :id, types: [String, Integer], desc: 'The ID or URL-encoded path of the project'
      end
      desc 'Get a project import status' do
        detail 'This feature was introduced in GitLab 10.6.'
        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']
      end
      get ':id/import' do
        present user_project, with: Entities::ProjectImportStatus, current_user: current_user
      end

      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.'
        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' }
        ]
      end
      post 'remote-import' do
        forbidden! unless Gitlab::CurrentSettings.import_sources.include?('gitlab_project')

        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],
            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, current_user: current_user)
        else
          render_api_error!(response.message, response.http_status)
        end
      end

      desc 'Workhorse authorize the project relation import upload' do
        detail 'This feature was introduced in GitLab 16.11'
        tags ['project_import']
      end
      post 'import-relation/authorize' do
        forbidden! unless Gitlab::CurrentSettings.import_sources.include?('gitlab_project')

        require_gitlab_workhorse!

        status 200
        content_type Gitlab::Workhorse::INTERNAL_API_CONTENT_TYPE

        ImportExportUploader.workhorse_authorize(
          has_length: false,
          maximum_size: Gitlab::CurrentSettings.max_import_size.megabytes
        )
      end

      params do
        requires :path, type: String, desc: 'The project path and name'
        requires :file,
          type: ::API::Validations::Types::WorkhorseFile,
          desc: 'The project export file from which to extract the relation.',
          documentation: { type: 'file' }
        requires :relation,
          type: String,
          desc: 'The relation to import. Must be one of issues, merge_requests, ci_pipelines, or milestones.'
        optional 'file.path', type: String, desc: 'Path to locally stored body (generated by Workhorse)'
        optional 'file.name', type: String, desc: 'Real filename as sent 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)'
      end
      desc 'Re-import a relation into a project' do
        detail 'This feature was introduced in GitLab 16.11.'
        success code: 201, model: Entities::RelationImportTracker
        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']
      end
      post 'import-relation' do
        forbidden! unless Gitlab::CurrentSettings.import_sources.include?('gitlab_project')

        require_gitlab_workhorse!

        check_rate_limit! :project_import, scope: [current_user, :project_import]

        Gitlab::QueryLimiting.disable!('https://gitlab.com/gitlab-org/gitlab/-/issues/21041')

        validate_file!

        response = ::Projects::ImportExport::RelationImportService.new(
          current_user: current_user,
          params: {
            path: import_params[:path],
            file: import_params[:file],
            relation: import_params[:relation]
          }
        ).execute

        if response.success?
          present(response.payload, with: Entities::RelationImportTracker, current_user: current_user)
        else
          render_api_error!(response.message, response.http_status)
        end
      end

      params do
        requires :id, types: [String, Integer], desc: 'The ID or URL-encoded path of the project'
      end
      desc 'Get the statuses of relation imports for specified project' do
        detail 'This feature was introduced in GitLab 16.11.'
        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']
      end
      get ':id/relation-imports' do
        forbidden!('Project') unless user_project && can?(current_user, :admin_project, user_project)

        import_statuses = user_project.relation_import_trackers
        present import_statuses, with: Entities::RelationImportTracker, current_user: current_user
      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.'
        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' }
        ]
      end
      post 'remote-import-s3' do
        forbidden! unless Gitlab::CurrentSettings.import_sources.include?('gitlab_project')

        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
        ).execute

        if response.success?
          present(response.payload, with: Entities::ProjectImportStatus, current_user: current_user)
        else
          render_api_error!(response.message, response.http_status)
        end
      end
    end
  end
end