diff --git a/app/services/projects/import_export/export_service.rb b/app/services/projects/import_export/export_service.rb index 99aa8489f65228b044cf4f8c204b19c1826712b1..ce13942c5d723f6bac714ccad8bdad589f458fe5 100644 --- a/app/services/projects/import_export/export_service.rb +++ b/app/services/projects/import_export/export_service.rb @@ -2,19 +2,24 @@ module Projects module ImportExport class ExportService < BaseService def execute(options = {}) - @shared = Projects::ImportExport::Shared.new(relative_path: project.path_with_namespace) + @shared = Gitlab::ImportExport::Shared.new(relative_path: project.path_with_namespace) save_project_tree bundle_repo + save_all end private def save_project_tree - Projects::ImportExport::ProjectTreeSaver.new(project: project, shared: @shared).save + Gitlab::ImportExport::ProjectTreeSaver.new(project: project, shared: @shared).save end def bundle_repo - Projects::ImportExport::RepoBundler.new(project: project, shared: @shared).bundle + Gitlab::ImportExport::RepoBundler.new(project: project, shared: @shared).bundle + end + + def save_all + Gitlab::ImportExport::Saver.save(storage_path: @shared.export_path) end end end diff --git a/app/services/projects/import_export.rb b/lib/gitlab/import_export.rb similarity index 68% rename from app/services/projects/import_export.rb rename to lib/gitlab/import_export.rb index c74f1d3149072eaf06ff2ba389018ff33a9fefab..fe88850c33dca4ed81fb69dbfae72feda67ecb00 100644 --- a/app/services/projects/import_export.rb +++ b/lib/gitlab/import_export.rb @@ -1,9 +1,9 @@ -module Projects +module Gitlab module ImportExport extend self def export_path(relative_path:) - File.join(storage_path, "#{Time.now.strftime('%Y-%m-%d_%H-%M-%3N')}_gitlab_export/#{relative_path}") + File.join(storage_path, relative_path, "#{Time.now.strftime('%Y-%m-%d_%H-%M-%3N')}_gitlab_export") end def project_atts @@ -11,7 +11,7 @@ module Projects end def project_tree - Projects::ImportExport::ImportExportReader.project_tree + Gitlab::ImportExport::ImportExportReader.project_tree end private diff --git a/app/services/projects/import_export/command_line_util.rb b/lib/gitlab/import_export/command_line_util.rb similarity index 58% rename from app/services/projects/import_export/command_line_util.rb rename to lib/gitlab/import_export/command_line_util.rb index 3ca49e76c7fd041e372128ad69fe9e8025a064bd..7bf4b476b6c8d6695688cae82f3af994675c9a0c 100644 --- a/app/services/projects/import_export/command_line_util.rb +++ b/lib/gitlab/import_export/command_line_util.rb @@ -1,10 +1,12 @@ -module Projects +module Gitlab module ImportExport module CommandLineUtil def tar_cf(archive:, dir:) - cmd = %W(tar -cf #{archive} -C #{dir} .) - _output, status = Gitlab::Popen.popen(cmd) - status.zero? + tar_with_options(archive: archive, dir: dir, options: 'cf') + end + + def tar_czf(archive:, dir:) + tar_with_options(archive: archive, dir: dir, options: 'czf') end def git_bundle(git_bin_path: Gitlab.config.git.bin_path, repo_path:, bundle_path:) @@ -12,6 +14,12 @@ module Projects _output, status = Gitlab::Popen.popen(cmd) status.zero? end + + def tar_with_options(archive:, dir:, options:) + cmd = %W(tar -#{options} #{archive} -C #{dir} .) + _output, status = Gitlab::Popen.popen(cmd) + status.zero? + end end end end diff --git a/app/services/projects/import_export/import_export.yml b/lib/gitlab/import_export/import_export.yml similarity index 100% rename from app/services/projects/import_export/import_export.yml rename to lib/gitlab/import_export/import_export.yml diff --git a/app/services/projects/import_export/import_export_reader.rb b/lib/gitlab/import_export/import_export_reader.rb similarity index 96% rename from app/services/projects/import_export/import_export_reader.rb rename to lib/gitlab/import_export/import_export_reader.rb index 001d24af604d8a155b7971e7d4ec497a58930c20..717d3026f9e668a5f7f2d5c75d401963800fef54 100644 --- a/app/services/projects/import_export/import_export_reader.rb +++ b/lib/gitlab/import_export/import_export_reader.rb @@ -1,4 +1,4 @@ -module Projects +module Gitlab module ImportExport module ImportExportReader extend self @@ -14,7 +14,7 @@ module Projects private def config - @config ||= YAML.load_file('app/services/projects/import_export/import_export.yml') + @config ||= YAML.load_file('lib/gitlab/import_export/import_export.yml') end def atts_only diff --git a/app/services/projects/import_export/project_tree_saver.rb b/lib/gitlab/import_export/project_tree_saver.rb similarity index 85% rename from app/services/projects/import_export/project_tree_saver.rb rename to lib/gitlab/import_export/project_tree_saver.rb index 37bf3e8e54552da237c53293324524f72675ce2b..b2615f8273b382bc50f194be5a1f69c1782b3e16 100644 --- a/app/services/projects/import_export/project_tree_saver.rb +++ b/lib/gitlab/import_export/project_tree_saver.rb @@ -1,4 +1,4 @@ -module Projects +module Gitlab module ImportExport class ProjectTreeSaver attr_reader :full_path @@ -25,12 +25,11 @@ module Projects end def project_filename - # TODO sanitize name "#{@project.name}.json" end def project_json_tree - @project.to_json(Projects::ImportExport.project_tree) + @project.to_json(Gitlab::ImportExport.project_tree) end end end diff --git a/app/services/projects/import_export/repo_bundler.rb b/lib/gitlab/import_export/repo_bundler.rb similarity index 91% rename from app/services/projects/import_export/repo_bundler.rb rename to lib/gitlab/import_export/repo_bundler.rb index d43fb4ea09e64852f567c6ef23d0fab96d88c342..7a1c2a12a536136329cf1080088e11ac137d2061 100644 --- a/app/services/projects/import_export/repo_bundler.rb +++ b/lib/gitlab/import_export/repo_bundler.rb @@ -1,7 +1,7 @@ -module Projects +module Gitlab module ImportExport class RepoBundler - include Projects::ImportExport::CommandLineUtil + include Gitlab::ImportExport::CommandLineUtil attr_reader :full_path diff --git a/lib/gitlab/import_export/saver.rb b/lib/gitlab/import_export/saver.rb new file mode 100644 index 0000000000000000000000000000000000000000..f26804d240289e974e79142b6d91dcfa013d7c85 --- /dev/null +++ b/lib/gitlab/import_export/saver.rb @@ -0,0 +1,38 @@ +module Gitlab + module ImportExport + class Saver + include Gitlab::ImportExport::CommandLineUtil + + def self.save(*args) + new(*args).save + end + + def initialize(storage_path:) + @storage_path = storage_path + end + + def save + if compress_and_save + remove_storage_path + archive_file + else + false + end + end + + private + + def compress_and_save + tar_czf(archive: archive_file, dir: @storage_path) + end + + def remove_storage_path + FileUtils.rm_rf(@storage_path) + end + + def archive_file + @archive_file ||= File.join(@storage_path, '..', 'project.tar.gz') + end + end + end +end diff --git a/app/services/projects/import_export/shared.rb b/lib/gitlab/import_export/shared.rb similarity index 56% rename from app/services/projects/import_export/shared.rb rename to lib/gitlab/import_export/shared.rb index 04fe01c0d6c2d4e6e4d12979a4c30cde4b02199a..a4ec33a6cf7fc928a203215a4f358c93fc8cd0c0 100644 --- a/app/services/projects/import_export/shared.rb +++ b/lib/gitlab/import_export/shared.rb @@ -1,4 +1,4 @@ -module Projects +module Gitlab module ImportExport class Shared def initialize(opts) @@ -6,7 +6,7 @@ module Projects end def export_path - @export_path ||= Projects::ImportExport.export_path(relative_path: @opts[:relative_path]) + @export_path ||= Gitlab::ImportExport.export_path(relative_path: @opts[:relative_path]) end end end diff --git a/app/services/projects/import_export/wiki_repo_bundler.rb b/lib/gitlab/import_export/wiki_repo_bundler.rb similarity index 97% rename from app/services/projects/import_export/wiki_repo_bundler.rb rename to lib/gitlab/import_export/wiki_repo_bundler.rb index bf69936503d1bfa39ad9f062e3480036a0b66626..9ef0febee54f1cd89bba488fa4ffc3dbdfb12542 100644 --- a/app/services/projects/import_export/wiki_repo_bundler.rb +++ b/lib/gitlab/import_export/wiki_repo_bundler.rb @@ -1,4 +1,4 @@ -module Projects +module Gitlab module ImportExport class WikiRepoBundler < RepoBundler def bundle diff --git a/spec/services/projects/import_export/import_export_reader_spec.rb b/spec/lib/gitlab/import_export/import_export_reader_spec.rb similarity index 92% rename from spec/services/projects/import_export/import_export_reader_spec.rb rename to spec/lib/gitlab/import_export/import_export_reader_spec.rb index f825292ce7e79e22273726f1093c03a723155437..e71c7b60b80d987cd3726cba78664ddb37edc6bd 100644 --- a/spec/services/projects/import_export/import_export_reader_spec.rb +++ b/spec/lib/gitlab/import_export/import_export_reader_spec.rb @@ -1,6 +1,6 @@ require 'rspec' -describe Projects::ImportExport::ImportExportReader do +describe Gitlab::ImportExport::ImportExportReader do let(:test_config) { 'spec/support/import_export/import_export.yml' } let(:project_tree_hash) do diff --git a/spec/services/projects/import_export/project_tree_saver_spec.rb b/spec/lib/gitlab/import_export/project_tree_saver_spec.rb similarity index 89% rename from spec/services/projects/import_export/project_tree_saver_spec.rb rename to spec/lib/gitlab/import_export/project_tree_saver_spec.rb index 05ece41341069a87b2cb0a96877bd879bc0dc601..8a923dde46792d811e0634d6cd28802ee3e2b14b 100644 --- a/spec/services/projects/import_export/project_tree_saver_spec.rb +++ b/spec/lib/gitlab/import_export/project_tree_saver_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Projects::ImportExport::ProjectTreeSaver, services: true do +describe Gitlab::ImportExport::ProjectTreeSaver, services: true do describe :save do # TODO refactor this into a setup method @@ -26,14 +26,14 @@ describe Projects::ImportExport::ProjectTreeSaver, services: true do let!(:ci_commit) { create(:ci_commit, project: project, sha: merge_request.last_commit.id, ref: merge_request.source_branch, statuses: [commit_status]) } let!(:milestone) { create(:milestone, title: "Milestone v1.2", project: project) } let(:export_path) { "#{Dir::tmpdir}/project_tree_saver_spec" } - let(:shared) { Projects::ImportExport::Shared.new(relative_path: project.path_with_namespace) } - let(:project_tree_saver) { Projects::ImportExport::ProjectTreeSaver.new(project: project, shared: shared) } + let(:shared) { Gitlab::ImportExport::Shared.new(relative_path: project.path_with_namespace) } + let(:project_tree_saver) { Gitlab::ImportExport::ProjectTreeSaver.new(project: project, shared: shared) } let!(:issue_note) { create(:note, note: ":+1: issue", noteable: issue) } let!(:merge_request_note) { create(:note, note: ":+1: merge_request", noteable: merge_request) } before(:each) do project.team << [user, :master] - allow_any_instance_of(Projects::ImportExport).to receive(:storage_path).and_return(export_path) + allow_any_instance_of(Gitlab::ImportExport).to receive(:storage_path).and_return(export_path) end after(:each) do diff --git a/spec/services/projects/import_export/repo_bundler_spec.rb b/spec/lib/gitlab/import_export/repo_bundler_spec.rb similarity index 55% rename from spec/services/projects/import_export/repo_bundler_spec.rb rename to spec/lib/gitlab/import_export/repo_bundler_spec.rb index 1f8ed41718f82f6cf8a1c36de9466f9f5b1ae316..23447d878a8bb98d8ba85b93ccac52f731e8f1d3 100644 --- a/spec/services/projects/import_export/repo_bundler_spec.rb +++ b/spec/lib/gitlab/import_export/repo_bundler_spec.rb @@ -1,17 +1,17 @@ require 'spec_helper' -describe Projects::ImportExport::RepoBundler, services: true do +describe Gitlab::ImportExport::RepoBundler, services: true do describe :bundle do let(:user) { create(:user) } let!(:project) { create(:project, :public, name: 'searchable_project') } let(:export_path) { "#{Dir::tmpdir}/project_tree_saver_spec" } - let(:shared) { Projects::ImportExport::Shared.new(relative_path: project.path_with_namespace) } - let(:bundler) { Projects::ImportExport::RepoBundler.new(project: project, shared: shared) } + let(:shared) { Gitlab::ImportExport::Shared.new(relative_path: project.path_with_namespace) } + let(:bundler) { Gitlab::ImportExport::RepoBundler.new(project: project, shared: shared) } before(:each) do project.team << [user, :master] - allow_any_instance_of(Projects::ImportExport).to receive(:storage_path).and_return(export_path) + allow_any_instance_of(Gitlab::ImportExport).to receive(:storage_path).and_return(export_path) end after(:each) do diff --git a/spec/services/projects/import_export/wiki_repo_bundler_spec.rb b/spec/lib/gitlab/import_export/wiki_repo_bundler_spec.rb similarity index 61% rename from spec/services/projects/import_export/wiki_repo_bundler_spec.rb rename to spec/lib/gitlab/import_export/wiki_repo_bundler_spec.rb index a589f81c5f9f4611005c619f6fac1211eee038fd..6cc95edc2178c28923be061524928f92679e22f6 100644 --- a/spec/services/projects/import_export/wiki_repo_bundler_spec.rb +++ b/spec/lib/gitlab/import_export/wiki_repo_bundler_spec.rb @@ -1,18 +1,18 @@ require 'spec_helper' -describe Projects::ImportExport::WikiRepoBundler, services: true do +describe Gitlab::ImportExport::WikiRepoBundler, services: true do describe :bundle do let(:user) { create(:user) } let!(:project) { create(:project, :public, name: 'searchable_project') } let(:export_path) { "#{Dir::tmpdir}/project_tree_saver_spec" } - let(:shared) { Projects::ImportExport::Shared.new(relative_path: project.path_with_namespace) } - let(:wiki_bundler) { Projects::ImportExport::WikiRepoBundler.new(project: project, shared: shared) } + let(:shared) { Gitlab::ImportExport::Shared.new(relative_path: project.path_with_namespace) } + let(:wiki_bundler) { Gitlab::ImportExport::WikiRepoBundler.new(project: project, shared: shared) } let!(:project_wiki) { ProjectWiki.new(project, user) } before(:each) do project.team << [user, :master] - allow_any_instance_of(Projects::ImportExport).to receive(:storage_path).and_return(export_path) + allow_any_instance_of(Gitlab::ImportExport).to receive(:storage_path).and_return(export_path) project_wiki.wiki project_wiki.create_page("index", "test content") end