BigW Consortium Gitlab

Commit 0852f539 by 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
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
......
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
......
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
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
......
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
......
module Projects
module Gitlab
module ImportExport
class RepoBundler
include Projects::ImportExport::CommandLineUtil
include Gitlab::ImportExport::CommandLineUtil
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
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
......
module Projects
module Gitlab
module ImportExport
class WikiRepoBundler < RepoBundler
def bundle
......
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
......
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
......
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
......
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
......
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