diff options
author | James Lopez <james@jameslopez.es> | 2016-04-14 17:57:25 +0300 |
---|---|---|
committer | James Lopez <james@jameslopez.es> | 2016-04-14 17:57:25 +0300 |
commit | 0852f539aa389c66ef377b7d567c931f928e147f (patch) | |
tree | 82797f5e855f9235d445c943af7161b4cc7d2238 /app/services/projects/import_export | |
parent | 91ffd8028977984d3ea9a741a3a655ff6dae76b2 (diff) |
refactored stuff, added a save and compress all class and moved mostly everything to lib
Diffstat (limited to 'app/services/projects/import_export')
8 files changed, 8 insertions, 270 deletions
diff --git a/app/services/projects/import_export/command_line_util.rb b/app/services/projects/import_export/command_line_util.rb deleted file mode 100644 index 3ca49e76c7f..00000000000 --- a/app/services/projects/import_export/command_line_util.rb +++ /dev/null @@ -1,17 +0,0 @@ -module Projects - module ImportExport - module CommandLineUtil - def tar_cf(archive:, dir:) - cmd = %W(tar -cf #{archive} -C #{dir} .) - _output, status = Gitlab::Popen.popen(cmd) - status.zero? - end - - def git_bundle(git_bin_path: Gitlab.config.git.bin_path, repo_path:, bundle_path:) - cmd = %W(#{git_bin_path} --git-dir=#{repo_path} bundle create #{bundle_path} --all) - _output, status = Gitlab::Popen.popen(cmd) - status.zero? - end - end - end -end diff --git a/app/services/projects/import_export/export_service.rb b/app/services/projects/import_export/export_service.rb index 99aa8489f65..ce13942c5d7 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/import_export.yml b/app/services/projects/import_export/import_export.yml deleted file mode 100644 index 92f492e9013..00000000000 --- a/app/services/projects/import_export/import_export.yml +++ /dev/null @@ -1,37 +0,0 @@ -# Class relationships to be included in the project import/export -:project_tree: - - :issues: - - :notes - - :labels - - :milestones - - :snippets - - :releases - - :events - - :project_members: - - :user - - :merge_requests: - - :merge_request_diff - - :notes - - :ci_commits: - - :statuses - -:attributes_only: - :project: - - :name - - :path - - :description - - :issues_enabled - - :wall_enabled - - :merge_requests_enabled - - :wiki_enabled - - :snippets_enabled - - :visibility_level - - :archived - :user: - - :id - - :email - - :username - -:attributes_except: - :snippets: - - :expired_at
\ No newline at end of file diff --git a/app/services/projects/import_export/import_export_reader.rb b/app/services/projects/import_export/import_export_reader.rb deleted file mode 100644 index 001d24af604..00000000000 --- a/app/services/projects/import_export/import_export_reader.rb +++ /dev/null @@ -1,91 +0,0 @@ -module Projects - module ImportExport - module ImportExportReader - extend self - - def project_tree - { only: atts_only[:project], include: build_hash(tree) } - end - - def tree - config[:project_tree] - end - - private - - def config - @config ||= YAML.load_file('app/services/projects/import_export/import_export.yml') - end - - def atts_only - config[:attributes_only] - end - - def atts_except - config[:attributes_except] - end - - def build_hash(array) - array.map do |el| - if el.is_a?(Hash) - process_include(el) - else - only_except_hash = check_only_and_except(el) - only_except_hash.empty? ? el : { el => only_except_hash } - end - end - end - - def process_include(hash, included_classes_hash = {}) - hash.values.flatten.each do |value| - current_key = hash.keys.first - value = process_current_class(hash, included_classes_hash, value) - if included_classes_hash[current_key] - add_class(current_key, included_classes_hash, value) - else - add_new_class(current_key, included_classes_hash, value) - end - end - included_classes_hash - end - - def process_current_class(hash, included_classes_hash, value) - value = value.is_a?(Hash) ? process_include(hash, included_classes_hash) : value - only_except_hash = check_only_and_except(hash.keys.first) - included_classes_hash[hash.keys.first] ||= only_except_hash unless only_except_hash.empty? - value - end - - def add_new_class(current_key, included_classes_hash, value) - new_hash = { include: value } - new_hash.merge!(check_only_and_except(value)) - included_classes_hash[current_key] = new_hash - end - - def add_class(current_key, included_classes_hash, value) - only_except_hash = check_only_and_except(value) - value = { value => only_except_hash } unless only_except_hash.empty? - old_values = included_classes_hash[current_key][:include] - included_classes_hash[current_key][:include] = ([old_values] + [value]).compact.flatten - end - - def check_only_and_except(value) - check_only(value).merge(check_except(value)) - end - - def check_only(value) - key = key_from_hash(value) - atts_only[key].nil? ? {} : { only: atts_only[key] } - end - - def check_except(value) - key = key_from_hash(value) - atts_except[key].nil? ? {} : { except: atts_except[key] } - end - - def key_from_hash(value) - value.is_a?(Hash) ? value.keys.first : value - end - end - end -end diff --git a/app/services/projects/import_export/project_tree_saver.rb b/app/services/projects/import_export/project_tree_saver.rb deleted file mode 100644 index 37bf3e8e545..00000000000 --- a/app/services/projects/import_export/project_tree_saver.rb +++ /dev/null @@ -1,37 +0,0 @@ -module Projects - module ImportExport - class ProjectTreeSaver - attr_reader :full_path - - def initialize(project: , shared: ) - @project = project - @export_path = shared.export_path - end - - def save - @full_path = File.join(@export_path, project_filename) - save_to_disk - end - - private - - def save_to_disk - FileUtils.mkdir_p(@export_path) - File.write(full_path, project_json_tree) - true - rescue - # TODO: handle error - false - end - - def project_filename - # TODO sanitize name - "#{@project.name}.json" - end - - def project_json_tree - @project.to_json(Projects::ImportExport.project_tree) - end - end - end -end diff --git a/app/services/projects/import_export/repo_bundler.rb b/app/services/projects/import_export/repo_bundler.rb deleted file mode 100644 index d43fb4ea09e..00000000000 --- a/app/services/projects/import_export/repo_bundler.rb +++ /dev/null @@ -1,38 +0,0 @@ -module Projects - module ImportExport - class RepoBundler - include Projects::ImportExport::CommandLineUtil - - attr_reader :full_path - - def initialize(project: , shared: ) - @project = project - @export_path = shared.export_path - end - - def bundle - return false if @project.empty_repo? - @full_path = File.join(@export_path, project_filename) - bundle_to_disk - end - - private - - def bundle_to_disk - FileUtils.mkdir_p(@export_path) - tar_cf(archive: full_path, dir: path_to_repo) - rescue - #TODO: handle error - false - end - - def project_filename - "#{@project.name}.bundle" - end - - def path_to_repo - @project.repository.path_to_repo - end - end - end -end diff --git a/app/services/projects/import_export/shared.rb b/app/services/projects/import_export/shared.rb deleted file mode 100644 index 04fe01c0d6c..00000000000 --- a/app/services/projects/import_export/shared.rb +++ /dev/null @@ -1,13 +0,0 @@ -module Projects - module ImportExport - class Shared - def initialize(opts) - @opts = opts - end - - def export_path - @export_path ||= Projects::ImportExport.export_path(relative_path: @opts[:relative_path]) - end - end - end -end diff --git a/app/services/projects/import_export/wiki_repo_bundler.rb b/app/services/projects/import_export/wiki_repo_bundler.rb deleted file mode 100644 index bf69936503d..00000000000 --- a/app/services/projects/import_export/wiki_repo_bundler.rb +++ /dev/null @@ -1,34 +0,0 @@ -module Projects - module ImportExport - class WikiRepoBundler < RepoBundler - def bundle - @wiki = ProjectWiki.new(@project) - return false if !wiki? - @full_path = File.join(@export_path, project_filename) - bundle_to_disk - end - - def bundle_to_disk - FileUtils.mkdir_p(@export_path) - git_bundle(repo_path: path_to_repo, bundle_path: @full_path) - rescue - #TODO: handle error - false - end - - private - - def project_filename - "#{@project.name}.wiki.bundle" - end - - def path_to_repo - @wiki.repository.path_to_repo - end - - def wiki? - File.exists?(@wiki.repository.path_to_repo) && !@wiki.repository.empty? - end - end - end -end |