Commit a5d59f07 authored by James Lopez's avatar James Lopez

added better error handling. Also refactored some of the code and fixed a few...

added better error handling. Also refactored some of the code and fixed a few issues in project_tree_saver
parent 6a12ff63
...@@ -4,8 +4,8 @@ module Projects ...@@ -4,8 +4,8 @@ module Projects
def execute(options = {}) def execute(options = {})
@shared = Gitlab::ImportExport::Shared.new(relative_path: File.join(project.path_with_namespace, 'work')) @shared = Gitlab::ImportExport::Shared.new(relative_path: File.join(project.path_with_namespace, 'work'))
# TODO handle errors
save_all if [save_project_tree, bundle_repo, bundle_wiki_repo].all? save_all if [save_project_tree, bundle_repo, bundle_wiki_repo].all?
notify_worker if @shared.errors.any?
end end
private private
...@@ -23,7 +23,11 @@ module Projects ...@@ -23,7 +23,11 @@ module Projects
end end
def save_all def save_all
Gitlab::ImportExport::Saver.save(storage_path: @shared.export_path) Gitlab::ImportExport::Saver.save(shared: @shared)
end
def notify_worker
raise Gitlab::ImportExport::Error @shared.errors.join(', ')
end end
end end
end end
......
...@@ -6,10 +6,6 @@ module Gitlab ...@@ -6,10 +6,6 @@ module Gitlab
File.join(storage_path, relative_path) File.join(storage_path, relative_path)
end end
def project_tree
Gitlab::ImportExport::ImportExportReader.new.project_tree
end
def storage_path def storage_path
File.join(Settings.shared['path'], 'tmp/project_exports') File.join(Settings.shared['path'], 'tmp/project_exports')
end end
......
module Gitlab
module ImportExport
class Error < StandardError; end
end
end
\ No newline at end of file
...@@ -2,16 +2,18 @@ module Gitlab ...@@ -2,16 +2,18 @@ module Gitlab
module ImportExport module ImportExport
class ImportExportReader class ImportExportReader
def initialize(config: 'lib/gitlab/import_export/import_export.yml') def initialize(config: 'lib/gitlab/import_export/import_export.yml', shared:)
config_hash = YAML.load_file(config).with_indifferent_access @shared = shared
config_hash = YAML.load_file(config).deep_symbolize_keys
@tree = config_hash[:project_tree] @tree = config_hash[:project_tree]
@attributes_parser = Gitlab::ImportExport::AttributesFinder.new(included_attributes: config_hash[:included_attributes], @attributes_parser = Gitlab::ImportExport::AttributesFinder.new(included_attributes: config_hash[:included_attributes],
excluded_attributes: config_hash[:excluded_attributes]) excluded_attributes: config_hash[:excluded_attributes])
@json_config_hash = {}
end end
def project_tree def project_tree
@attributes_parser.find_included(:project).merge(include: build_hash(@tree)) @attributes_parser.find_included(:project).merge(include: build_hash(@tree))
rescue => e
@shared.error(e.message)
end end
private private
...@@ -27,6 +29,8 @@ module Gitlab ...@@ -27,6 +29,8 @@ module Gitlab
end end
def build_json_config_hash(model_object_hash) def build_json_config_hash(model_object_hash)
@json_config_hash = {}
model_object_hash.values.flatten.each do |model_object| model_object_hash.values.flatten.each do |model_object|
current_key = model_object_hash.keys.first current_key = model_object_hash.keys.first
...@@ -61,7 +65,7 @@ module Gitlab ...@@ -61,7 +65,7 @@ module Gitlab
end end
def hash_or_merge(value, hash) def hash_or_merge(value, hash)
value.is_a?(Hash) ? value.merge(hash) : hash value.is_a?(Hash) ? value.merge(hash) : { value => hash }
end end
end end
end end
......
...@@ -5,16 +5,16 @@ module Gitlab ...@@ -5,16 +5,16 @@ module Gitlab
def initialize(project:, shared:) def initialize(project:, shared:)
@project = project @project = project
@export_path = shared.export_path @shared = shared
@full_path = File.join(@export_path, project_filename) @full_path = File.join(@shared.export_path, project_filename)
end end
def save def save
FileUtils.mkdir_p(@export_path) FileUtils.mkdir_p(@shared.export_path)
File.write(full_path, project_json_tree) File.write(full_path, project_json_tree)
true true
rescue rescue => e
# TODO: handle error @shared.error(e.message)
false false
end end
...@@ -26,7 +26,7 @@ module Gitlab ...@@ -26,7 +26,7 @@ module Gitlab
end end
def project_json_tree def project_json_tree
@project.to_json(Gitlab::ImportExport.project_tree) @project.to_json(Gitlab::ImportExport::ImportExportReader.new(shared: @shared).project_tree)
end end
end end
end end
......
...@@ -7,21 +7,22 @@ module Gitlab ...@@ -7,21 +7,22 @@ module Gitlab
def initialize(project: , shared: ) def initialize(project: , shared: )
@project = project @project = project
@export_path = shared.export_path @shared = shared
end end
def bundle def bundle
return false if @project.empty_repo? return false if @project.empty_repo?
@full_path = File.join(@export_path, project_filename) @full_path = File.join(@shared.export_path, project_filename)
bundle_to_disk bundle_to_disk
end end
private private
def bundle_to_disk def bundle_to_disk
FileUtils.mkdir_p(@export_path) FileUtils.mkdir_p(@shared.export_path)
git_bundle(repo_path: path_to_repo, bundle_path: @full_path) git_bundle(repo_path: path_to_repo, bundle_path: @full_path)
rescue rescue => e
@shared.error(e.message)
false false
end end
......
...@@ -7,32 +7,35 @@ module Gitlab ...@@ -7,32 +7,35 @@ module Gitlab
new(*args).save new(*args).save
end end
def initialize(storage_path:) def initialize(shared:)
@storage_path = storage_path @shared = shared
end end
def save def save
if compress_and_save if compress_and_save
remove_storage_path remove_@shared.storage_path
Rails.logger.info("Saved project export #{archive_file}") Rails.logger.info("Saved project export #{archive_file}")
archive_file archive_file
else else
false false
end end
rescue => e
@shared.error(e.message)
false
end end
private private
def compress_and_save def compress_and_save
tar_czf(archive: archive_file, dir: @storage_path) tar_czf(archive: archive_file, dir: @shared.storage_path)
end end
def remove_storage_path def remove_shared.storage_path
FileUtils.rm_rf(@storage_path) FileUtils.rm_rf(@shared.storage_path)
end end
def archive_file def archive_file
@archive_file ||= File.join(@storage_path, '..', "#{Time.now.strftime('%Y-%m-%d_%H-%M-%3N')}_project_export.tar.gz") @archive_file ||= File.join(@shared.storage_path, '..', "#{Time.now.strftime('%Y-%m-%d_%H-%M-%3N')}_project_export.tar.gz")
end end
end end
end end
......
module Gitlab module Gitlab
module ImportExport module ImportExport
class Shared class Shared
attr_reader :errors
def initialize(opts) def initialize(opts)
@opts = opts @opts = opts
@errors = []
end end
def export_path def export_path
@export_path ||= Gitlab::ImportExport.export_path(relative_path: @opts[:relative_path]) @export_path ||= Gitlab::ImportExport.export_path(relative_path: @opts[:relative_path])
end end
def error(message)
error_out(message, caller[0].dup)
@errors << message
end
private
def error_out(message, caller)
Rails.logger.error("Import/Export error raised on #{caller}: #{message}")
end
end end
end end
end end
...@@ -4,14 +4,15 @@ module Gitlab ...@@ -4,14 +4,15 @@ module Gitlab
def bundle def bundle
@wiki = ProjectWiki.new(@project) @wiki = ProjectWiki.new(@project)
return true if !wiki? # it's okay to have no Wiki return true if !wiki? # it's okay to have no Wiki
@full_path = File.join(@export_path, project_filename) @full_path = File.join(@shared.export_path, project_filename)
bundle_to_disk bundle_to_disk
end end
def bundle_to_disk def bundle_to_disk
FileUtils.mkdir_p(@export_path) FileUtils.mkdir_p(@shared.export_path)
git_bundle(repo_path: path_to_repo, bundle_path: @full_path) git_bundle(repo_path: path_to_repo, bundle_path: @full_path)
rescue rescue => e
@shared.error(e.message)
false false
end end
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::ImportExport::ImportExportReader do describe Gitlab::ImportExport::ImportExportReader do
let(:shared) { Gitlab::ImportExport::Shared.new(relative_path:'') }
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
{ {
...@@ -17,6 +17,6 @@ describe Gitlab::ImportExport::ImportExportReader do ...@@ -17,6 +17,6 @@ describe Gitlab::ImportExport::ImportExportReader do
end end
it 'should generate hash from project tree config' do it 'should generate hash from project tree config' do
expect(described_class.new(config: test_config).project_tree) =~ (project_tree_hash) expect(described_class.new(config: test_config, shared: shared).project_tree) =~ (project_tree_hash)
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