Commit d855d098 authored by George Koltsov's avatar George Koltsov Committed by Sean McGivern

Add API endpoint to trigger Group Structure Export

- Introduce 2 new API endpoints for Group Structure Export
- POST /groups/:id/export
- GET /groups/:id/export/download

- Add new API endpoint to trigger Group Relations export
- Similar to Project Export, relations only
parent 9ebdacda
...@@ -451,6 +451,14 @@ class Group < Namespace ...@@ -451,6 +451,14 @@ class Group < Namespace
false false
end end
def export_file_exists?
export_file&.file
end
def export_file
import_export_upload&.export_file
end
private private
def update_two_factor_requirement def update_two_factor_requirement
......
---
title: Add API endpoint to trigger Group Structure Export
merge_request: 19779
author:
type: added
...@@ -113,6 +113,7 @@ module API ...@@ -113,6 +113,7 @@ module API
mount ::API::Files mount ::API::Files
mount ::API::GroupBoards mount ::API::GroupBoards
mount ::API::GroupClusters mount ::API::GroupClusters
mount ::API::GroupExport
mount ::API::GroupLabels mount ::API::GroupLabels
mount ::API::GroupMilestones mount ::API::GroupMilestones
mount ::API::Groups mount ::API::Groups
......
# frozen_string_literal: true
module API
class GroupExport < Grape::API
before do
authorize! :admin_group, user_group
end
params do
requires :id, type: String, desc: 'The ID of a group'
end
resource :groups, requirements: { id: %r{[^/]+} } do
desc 'Download export' do
detail 'This feature was introduced in GitLab 12.5.'
end
get ':id/export/download' do
if user_group.export_file_exists?
present_carrierwave_file!(user_group.export_file)
else
render_api_error!('404 Not found or has expired', 404)
end
end
desc 'Start export' do
detail 'This feature was introduced in GitLab 12.5.'
end
post ':id/export' do
GroupExportWorker.perform_async(current_user.id, user_group.id, params)
accepted!
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe API::GroupExport do
let_it_be(:group) { create(:group) }
let_it_be(:user) { create(:user) }
let(:path) { "/groups/#{group.id}/export" }
let(:download_path) { "/groups/#{group.id}/export/download" }
let(:export_path) { "#{Dir.tmpdir}/group_export_spec" }
before do
allow_next_instance_of(Gitlab::ImportExport) do |import_export|
expect(import_export).to receive(:storage_path).and_return(export_path)
end
end
after do
FileUtils.rm_rf(export_path, secure: true)
end
describe 'GET /groups/:group_id/export/download' do
let(:upload) { ImportExportUpload.new(group: group) }
before do
stub_uploads_object_storage(ImportExportUploader)
group.add_owner(user)
end
context 'when export file exists' do
before do
upload.export_file = fixture_file_upload('spec/fixtures/group_export.tar.gz', "`/tar.gz")
upload.save!
end
it 'downloads exported group archive' do
get api(download_path, user)
expect(response).to have_gitlab_http_status(200)
end
context 'when export_file.file does not exist' do
before do
expect_next_instance_of(ImportExportUploader) do |uploader|
expect(uploader).to receive(:file).and_return(nil)
end
end
it 'returns 404' do
get api(download_path, user)
expect(response).to have_gitlab_http_status(404)
end
end
end
context 'when export file does not exist' do
it 'returns 404' do
get api(download_path, user)
expect(response).to have_gitlab_http_status(404)
end
end
end
describe 'POST /groups/:group_id/export' do
context 'when user is a group owner' do
before do
group.add_owner(user)
end
it 'accepts download' do
post api(path, user)
expect(response).to have_gitlab_http_status(202)
end
end
context 'when user is not a group owner' do
before do
group.add_developer(user)
end
it 'forbids the request' do
post api(path, user)
expect(response).to have_gitlab_http_status(403)
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment