Welcome to mirror list, hosted at ThFree Co, Russian Federation.

project_export.rb « api « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 25848d91550bedc26194d5e5e0646edd354c51e9 (plain)
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
# frozen_string_literal: true

module API
  class ProjectExport < ::API::Base
    feature_category :importers
    urgency :low

    params do
      requires :id, types: [String, Integer], desc: 'The ID or URL-encoded path of the project'
    end
    resource :projects, requirements: { id: %r{[^/]+} } do
      resource do
        before do
          not_found! unless Gitlab::CurrentSettings.project_export_enabled?

          authorize_admin_project
        end

        desc 'Get export status' do
          detail 'This feature was introduced in GitLab 10.6.'
          success code: 200, model: Entities::ProjectExportStatus
          failure [
            { code: 401, message: 'Unauthorized' },
            { code: 403, message: 'Forbidden' },
            { code: 404, message: 'Not found' },
            { code: 503, message: 'Service unavailable' }
          ]
          tags ['project_export']
        end
        get ':id/export' do
          present user_project, with: Entities::ProjectExportStatus
        end

        desc 'Download export' do
          detail 'This feature was introduced in GitLab 10.6.'
          success code: 200
          failure [
            { code: 401, message: 'Unauthorized' },
            { code: 403, message: 'Forbidden' },
            { code: 404, message: 'Not found' },
            { code: 503, message: 'Service unavailable' }
          ]
          tags ['project_export']
          produces %w[application/octet-stream application/json]
        end
        get ':id/export/download' do
          check_rate_limit! :project_download_export, scope: [current_user, user_project.namespace]

          if user_project.export_file_exists?
            if user_project.export_archive_exists?
              present_carrierwave_file!(user_project.export_file)
            else
              render_api_error!('The project export file is not available yet', 404)
            end
          else
            render_api_error!('404 Not found or has expired', 404)
          end
        end

        desc 'Start export' do
          detail 'This feature was introduced in GitLab 10.6.'
          success code: 202
          failure [
            { code: 400, message: 'Bad request' },
            { code: 401, message: 'Unauthorized' },
            { code: 403, message: 'Forbidden' },
            { code: 404, message: 'Not found' },
            { code: 429, message: 'Too many requests' },
            { code: 503, message: 'Service unavailable' }
          ]
          tags ['project_export']
        end
        params do
          optional :description, type: String, desc: 'Override the project description'
          optional :upload, type: Hash do
            optional :url, type: String, desc: 'The URL to upload the project'
            optional :http_method, type: String, default: 'PUT', values: %w[PUT POST],
                                   desc: 'HTTP method to upload the exported project'
          end
        end
        post ':id/export' do
          check_rate_limit! :project_export, scope: current_user

          user_project.remove_exports

          project_export_params = declared_params(include_missing: false)
          after_export_params = project_export_params.delete(:upload) || {}

          export_strategy = if after_export_params[:url].present?
                              params = after_export_params.slice(:url, :http_method).symbolize_keys

                              Gitlab::ImportExport::AfterExportStrategies::WebUploadStrategy.new(**params)
                            end

          if export_strategy&.invalid?
            render_validation_error!(export_strategy)
          else
            begin
              user_project.add_export_job(current_user: current_user,
                                          after_export_strategy: export_strategy,
                                          params: project_export_params)
            rescue Project::ExportLimitExceeded => e
              render_api_error!(e.message, 400)
            end
          end

          accepted!
        end
      end

      resource do
        before do
          not_found! unless Gitlab::CurrentSettings.bulk_import_enabled? ||
            Feature.enabled?(:override_bulk_import_disabled, current_user, type: :ops)

          authorize_admin_project
        end

        desc 'Start relations export' do
          detail 'This feature was introduced in GitLab 14.4'
          success code: 202
          failure [
            { code: 400, message: 'Bad request' },
            { code: 401, message: 'Unauthorized' },
            { code: 403, message: 'Forbidden' },
            { code: 404, message: 'Not found' },
            { code: 503, message: 'Service unavailable' }
          ]
          tags ['project_export']
        end
        params do
          optional :batched, type: Boolean, desc: 'Whether to export in batches'
        end
        post ':id/export_relations' do
          response = ::BulkImports::ExportService
            .new(portable: user_project, user: current_user, batched: params[:batched])
            .execute

          if response.success?
            accepted!
          else
            render_api_error!('Project relations export could not be started.', 500)
          end
        end

        desc 'Download relations export' do
          detail 'This feature was introduced in GitLab 14.4'
          success code: 200
          failure [
            { code: 400, message: 'Bad request' },
            { code: 401, message: 'Unauthorized' },
            { code: 403, message: 'Forbidden' },
            { code: 404, message: 'Not found' },
            { code: 500, message: 'Internal Server Error' },
            { code: 503, message: 'Service unavailable' }
          ]
          tags ['project_export']
          produces %w[application/octet-stream application/gzip application/json]
        end
        params do
          requires :relation, type: String, project_portable: true, desc: 'Project relation name'
          optional :batched, type: Boolean, desc: 'Whether to download in batches'
          optional :batch_number, type: Integer, desc: 'Batch number to download'

          all_or_none_of :batched, :batch_number
        end
        get ':id/export_relations/download' do
          export = user_project.bulk_import_exports.find_by_relation(params[:relation])

          break render_api_error!('Export not found', 404) unless export

          if params[:batched]
            batch = export.batches.find_by_batch_number(params[:batch_number])
            batch_file = batch&.upload&.export_file

            break render_api_error!('Export is not batched', 400) unless export.batched?
            break render_api_error!('Batch not found', 404) unless batch
            break render_api_error!('Batch file not found', 404) unless batch_file

            present_carrierwave_file!(batch_file)
          else
            file = export&.upload&.export_file

            break render_api_error!('Export is batched', 400) if export.batched?
            break render_api_error!('Export file not found', 404) unless file

            present_carrierwave_file!(file)
          end
        end

        desc 'Relations export status' do
          detail 'This feature was introduced in GitLab 14.4'
          is_array true
          success code: 200, model: Entities::BulkImports::ExportStatus
          failure [
            { code: 401, message: 'Unauthorized' },
            { code: 403, message: 'Forbidden' },
            { code: 404, message: 'Not found' },
            { code: 503, message: 'Service unavailable' }
          ]
          tags ['project_export']
        end
        params do
          optional :relation, type: String, desc: 'Project relation name'
        end
        get ':id/export_relations/status' do
          if params[:relation]
            export = user_project.bulk_import_exports.find_by_relation(params[:relation])

            break render_api_error!('Export not found', 404) unless export

            present export, with: Entities::BulkImports::ExportStatus
          else
            present user_project.bulk_import_exports, with: Entities::BulkImports::ExportStatus
          end
        end
      end
    end
  end
end