From 1466997755f704b1f8af49ced136e91827a0892c Mon Sep 17 00:00:00 2001 From: James Lopez Date: Thu, 19 May 2016 15:36:20 +0200 Subject: [PATCH] import uploads. Fixed a few things to do with members, triggers, etc... --- app/models/member.rb | 9 +++++---- lib/gitlab/import_export.rb | 4 ++++ lib/gitlab/import_export/import_service.rb | 6 +++++- lib/gitlab/import_export/members_mapper.rb | 4 ++-- lib/gitlab/import_export/relation_factory.rb | 10 +++++++++- lib/gitlab/import_export/uploads_restorer.rb | 19 +++++++++++++++++++ lib/gitlab/import_export/uploads_saver.rb | 13 +++++++++---- 7 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 lib/gitlab/import_export/uploads_restorer.rb diff --git a/app/models/member.rb b/app/models/member.rb index cca82da89f1..bef0b545c70 100644 --- a/app/models/member.rb +++ b/app/models/member.rb @@ -22,6 +22,7 @@ class Member < ActiveRecord::Base include Gitlab::Access attr_accessor :raw_invite_token + attr_accessor :importing belongs_to :created_by, class_name: "User" belongs_to :user @@ -54,10 +55,10 @@ class Member < ActiveRecord::Base scope :owners, -> { where(access_level: OWNER) } before_validation :generate_invite_token, on: :create, if: -> (member) { member.invite_email.present? } - after_create :send_invite, if: :invite? - after_create :create_notification_setting, unless: :invite? - after_create :post_create_hook, unless: :invite? - after_update :post_update_hook, unless: :invite? + after_create :send_invite, if: :invite?, unless: :importing + after_create :create_notification_setting, unless: [:invite?, :importing] + after_create :post_create_hook, unless: [:invite?, :importing] + after_update :post_update_hook, unless: [:invite?, :importing] after_destroy :post_destroy_hook, unless: :invite? delegate :name, :username, :email, to: :user, prefix: true diff --git a/lib/gitlab/import_export.rb b/lib/gitlab/import_export.rb index 65a8fcfadd0..9c04a3d6995 100644 --- a/lib/gitlab/import_export.rb +++ b/lib/gitlab/import_export.rb @@ -27,5 +27,9 @@ module Gitlab def version VERSION end + + def reset_tokens? + true + end end end diff --git a/lib/gitlab/import_export/import_service.rb b/lib/gitlab/import_export/import_service.rb index b025bff29bd..acd734f0890 100644 --- a/lib/gitlab/import_export/import_service.rb +++ b/lib/gitlab/import_export/import_service.rb @@ -16,7 +16,7 @@ module Gitlab def execute Gitlab::ImportExport::Importer.import(archive_file: @archive_file, shared: @shared) - if [restore_version, restore_project_tree, restore_repo, restore_wiki_repo].all? + if [restore_version, restore_project_tree, restore_repo, restore_wiki_repo, restore_uploads].all? project_tree.project else project_tree.project.destroy if project_tree.project @@ -52,6 +52,10 @@ module Gitlab project: ProjectWiki.new(project_tree.project)).restore end + def restore_uploads + Gitlab::ImportExport::UploadsRestorer.restore(project: project_tree.project, shared: @shared) + end + def path_with_namespace(project_path) File.join(@namespace.path, project_path) end diff --git a/lib/gitlab/import_export/members_mapper.rb b/lib/gitlab/import_export/members_mapper.rb index 4401fab3d23..28e57867158 100644 --- a/lib/gitlab/import_export/members_mapper.rb +++ b/lib/gitlab/import_export/members_mapper.rb @@ -50,11 +50,11 @@ module Gitlab end def member_hash(member) - member.except('id').merge(source_id: @project.id) + member.except('id').merge(source_id: @project.id, importing: true) end def default_project_member_hash - { user: @user, access_level: ProjectMember::MASTER, source_id: @project.id } + { user: @user, access_level: ProjectMember::MASTER, source_id: @project.id, importing: true } end def find_project_user_query(member) diff --git a/lib/gitlab/import_export/relation_factory.rb b/lib/gitlab/import_export/relation_factory.rb index d4e49ae17f0..566777e8fc9 100644 --- a/lib/gitlab/import_export/relation_factory.rb +++ b/lib/gitlab/import_export/relation_factory.rb @@ -12,7 +12,7 @@ module Gitlab builds: 'Ci::Build', hooks: 'ProjectHook' }.freeze - USER_REFERENCES = %w(author_id assignee_id updated_by_id).freeze + USER_REFERENCES = %w(author_id assignee_id updated_by_id user_id).freeze def create(relation_sym:, relation_hash:, members_mapper:, user_admin:) relation_sym = parse_relation_sym(relation_sym) @@ -21,6 +21,7 @@ module Gitlab update_missing_author(relation_hash, members_mapper, user_admin) if relation_sym == :notes update_user_references(relation_hash, members_mapper.map) update_project_references(relation_hash, klass) + reset_tokens(relation_hash) if relation_sym == 'Ci::Trigger' generate_imported_object(klass, relation_hash, relation_sym) end @@ -88,6 +89,13 @@ module Gitlab relation_hash['gl_project_id'] = project_id if relation_hash ['gl_project_id'] end + def reset_tokens(relation_hash) + return unless Gitlab::ImportExport.reset_tokens? + + # If we import/export a project to the same instance, tokens will have to be reseated. + relation_hash['token'] = nil + end + def relation_class(relation_sym) relation_sym.to_s.classify.constantize end diff --git a/lib/gitlab/import_export/uploads_restorer.rb b/lib/gitlab/import_export/uploads_restorer.rb new file mode 100644 index 00000000000..fdcbc48eb1b --- /dev/null +++ b/lib/gitlab/import_export/uploads_restorer.rb @@ -0,0 +1,19 @@ +module Gitlab + module ImportExport + class UploadsRestorer < UploadsSaver + + class << self + alias_method :restore, :save + end + + def save + return true unless File.directory?(uploads_export_path) + + copy_files(uploads_export_path, uploads_path) + rescue => e + @shared.error(e) + false + end + end + end +end diff --git a/lib/gitlab/import_export/uploads_saver.rb b/lib/gitlab/import_export/uploads_saver.rb index 3420d2ea4cb..93bc626b363 100644 --- a/lib/gitlab/import_export/uploads_saver.rb +++ b/lib/gitlab/import_export/uploads_saver.rb @@ -14,21 +14,26 @@ module Gitlab def save return true unless File.directory?(uploads_path) - FileUtils.copy_entry(uploads_path, uploads_export_path) - true + copy_files(uploads_path, uploads_export_path) rescue => e - @shared.error(e.message) + @shared.error(e) false end private + def copy_files(source, destination) + FileUtils.mkdir_p(destination) + FileUtils.copy_entry(source, destination) + true + end + def uploads_export_path File.join(@shared.export_path, 'uploads') end def uploads_path - File.join(Rails.root.join('public/uploads'), project.path_with_namespace) + File.join(Rails.root.join('public/uploads'), @project.path_with_namespace) end end end