Commit 0852f539 authored by James Lopez's avatar James Lopez

refactored stuff, added a save and compress all class and moved mostly everything to lib

parent 91ffd802
...@@ -2,19 +2,24 @@ module Projects ...@@ -2,19 +2,24 @@ module Projects
module ImportExport module ImportExport
class ExportService < BaseService class ExportService < BaseService
def execute(options = {}) 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 save_project_tree
bundle_repo bundle_repo
save_all
end end
private private
def save_project_tree def save_project_tree
Projects::ImportExport::ProjectTreeSaver.new(project: project, shared: @shared).save Gitlab::ImportExport::ProjectTreeSaver.new(project: project, shared: @shared).save
end end
def bundle_repo 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 end
end end
......
module Projects module Gitlab
module ImportExport module ImportExport
extend self extend self
def export_path(relative_path:) 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 end
def project_atts def project_atts
...@@ -11,7 +11,7 @@ module Projects ...@@ -11,7 +11,7 @@ module Projects
end end
def project_tree def project_tree
Projects::ImportExport::ImportExportReader.project_tree Gitlab::ImportExport::ImportExportReader.project_tree
end end
private private
......
module Projects module Gitlab
module ImportExport module ImportExport
module CommandLineUtil module CommandLineUtil
def tar_cf(archive:, dir:) def tar_cf(archive:, dir:)
cmd = %W(tar -cf #{archive} -C #{dir} .) tar_with_options(archive: archive, dir: dir, options: 'cf')
_output, status = Gitlab::Popen.popen(cmd) end
status.zero?
def tar_czf(archive:, dir:)
tar_with_options(archive: archive, dir: dir, options: 'czf')
end end
def git_bundle(git_bin_path: Gitlab.config.git.bin_path, repo_path:, bundle_path:) def git_bundle(git_bin_path: Gitlab.config.git.bin_path, repo_path:, bundle_path:)
...@@ -12,6 +14,12 @@ module Projects ...@@ -12,6 +14,12 @@ module Projects
_output, status = Gitlab::Popen.popen(cmd) _output, status = Gitlab::Popen.popen(cmd)
status.zero? status.zero?
end 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 end
end end
module Projects module Gitlab
module ImportExport module ImportExport
module ImportExportReader module ImportExportReader
extend self extend self
...@@ -14,7 +14,7 @@ module Projects ...@@ -14,7 +14,7 @@ module Projects
private private
def config 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 end
def atts_only def atts_only
......
module Projects module Gitlab
module ImportExport module ImportExport
class ProjectTreeSaver class ProjectTreeSaver
attr_reader :full_path attr_reader :full_path
...@@ -25,12 +25,11 @@ module Projects ...@@ -25,12 +25,11 @@ module Projects
end end
def project_filename def project_filename
# TODO sanitize name
"#{@project.name}.json" "#{@project.name}.json"
end end
def project_json_tree def project_json_tree
@project.to_json(Projects::ImportExport.project_tree) @project.to_json(Gitlab::ImportExport.project_tree)
end end
end end
end end
......
module Projects module Gitlab
module ImportExport module ImportExport
class RepoBundler class RepoBundler
include Projects::ImportExport::CommandLineUtil include Gitlab::ImportExport::CommandLineUtil
attr_reader :full_path attr_reader :full_path
......
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
module Projects module Gitlab
module ImportExport module ImportExport
class Shared class Shared
def initialize(opts) def initialize(opts)
...@@ -6,7 +6,7 @@ module Projects ...@@ -6,7 +6,7 @@ module Projects
end end
def export_path 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 end
end end
......
module Projects module Gitlab
module ImportExport module ImportExport
class WikiRepoBundler < RepoBundler class WikiRepoBundler < RepoBundler
def bundle def bundle
......
require 'rspec' require 'rspec'
describe Projects::ImportExport::ImportExportReader do describe Gitlab::ImportExport::ImportExportReader do
let(:test_config) { 'spec/support/import_export/import_export.yml' } let(:test_config) { 'spec/support/import_export/import_export.yml' }
let(:project_tree_hash) do let(:project_tree_hash) do
......
require 'spec_helper' require 'spec_helper'
describe Projects::ImportExport::ProjectTreeSaver, services: true do describe Gitlab::ImportExport::ProjectTreeSaver, services: true do
describe :save do describe :save do
# TODO refactor this into a setup method # TODO refactor this into a setup method
...@@ -26,14 +26,14 @@ describe Projects::ImportExport::ProjectTreeSaver, services: true do ...@@ -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!(: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!(:milestone) { create(:milestone, title: "Milestone v1.2", project: project) }
let(:export_path) { "#{Dir::tmpdir}/project_tree_saver_spec" } let(:export_path) { "#{Dir::tmpdir}/project_tree_saver_spec" }
let(:shared) { Projects::ImportExport::Shared.new(relative_path: project.path_with_namespace) } let(:shared) { Gitlab::ImportExport::Shared.new(relative_path: project.path_with_namespace) }
let(:project_tree_saver) { Projects::ImportExport::ProjectTreeSaver.new(project: project, shared: shared) } let(:project_tree_saver) { Gitlab::ImportExport::ProjectTreeSaver.new(project: project, shared: shared) }
let!(:issue_note) { create(:note, note: ":+1: issue", noteable: issue) } let!(:issue_note) { create(:note, note: ":+1: issue", noteable: issue) }
let!(:merge_request_note) { create(:note, note: ":+1: merge_request", noteable: merge_request) } let!(:merge_request_note) { create(:note, note: ":+1: merge_request", noteable: merge_request) }
before(:each) do before(:each) do
project.team << [user, :master] 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 end
after(:each) do after(:each) do
......
require 'spec_helper' require 'spec_helper'
describe Projects::ImportExport::RepoBundler, services: true do describe Gitlab::ImportExport::RepoBundler, services: true do
describe :bundle do describe :bundle do
let(:user) { create(:user) } let(:user) { create(:user) }
let!(:project) { create(:project, :public, name: 'searchable_project') } let!(:project) { create(:project, :public, name: 'searchable_project') }
let(:export_path) { "#{Dir::tmpdir}/project_tree_saver_spec" } let(:export_path) { "#{Dir::tmpdir}/project_tree_saver_spec" }
let(:shared) { Projects::ImportExport::Shared.new(relative_path: project.path_with_namespace) } let(:shared) { Gitlab::ImportExport::Shared.new(relative_path: project.path_with_namespace) }
let(:bundler) { Projects::ImportExport::RepoBundler.new(project: project, shared: shared) } let(:bundler) { Gitlab::ImportExport::RepoBundler.new(project: project, shared: shared) }
before(:each) do before(:each) do
project.team << [user, :master] 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 end
after(:each) do after(:each) do
......
require 'spec_helper' require 'spec_helper'
describe Projects::ImportExport::WikiRepoBundler, services: true do describe Gitlab::ImportExport::WikiRepoBundler, services: true do
describe :bundle do describe :bundle do
let(:user) { create(:user) } let(:user) { create(:user) }
let!(:project) { create(:project, :public, name: 'searchable_project') } let!(:project) { create(:project, :public, name: 'searchable_project') }
let(:export_path) { "#{Dir::tmpdir}/project_tree_saver_spec" } let(:export_path) { "#{Dir::tmpdir}/project_tree_saver_spec" }
let(:shared) { Projects::ImportExport::Shared.new(relative_path: project.path_with_namespace) } let(:shared) { Gitlab::ImportExport::Shared.new(relative_path: project.path_with_namespace) }
let(:wiki_bundler) { Projects::ImportExport::WikiRepoBundler.new(project: project, shared: shared) } let(:wiki_bundler) { Gitlab::ImportExport::WikiRepoBundler.new(project: project, shared: shared) }
let!(:project_wiki) { ProjectWiki.new(project, user) } let!(:project_wiki) { ProjectWiki.new(project, user) }
before(:each) do before(:each) do
project.team << [user, :master] 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.wiki
project_wiki.create_page("index", "test content") project_wiki.create_page("index", "test content")
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