diff options
-rw-r--r-- | app/controllers/import/gitlab_project_controller.rb | 45 | ||||
-rw-r--r-- | lib/gitlab/import_export/import_export_reader.rb | 17 | ||||
-rw-r--r-- | lib/gitlab/import_export/import_service.rb | 6 | ||||
-rw-r--r-- | lib/gitlab/import_export/project.json (renamed from fixtures/import_export/project.json) | 0 | ||||
-rw-r--r-- | lib/gitlab/import_export/project_factory.rb | 9 | ||||
-rw-r--r-- | lib/gitlab/import_export/project_tree_restorer.rb | 10 | ||||
-rw-r--r-- | lib/gitlab/import_export/repo_restorer.rb | 2 | ||||
-rw-r--r-- | spec/lib/gitlab/import_export/project_tree_restorer_spec.rb | 3 | ||||
-rw-r--r-- | spec/spec_helper.rb | 4 |
9 files changed, 31 insertions, 65 deletions
diff --git a/app/controllers/import/gitlab_project_controller.rb b/app/controllers/import/gitlab_project_controller.rb deleted file mode 100644 index ab0da196ac1..00000000000 --- a/app/controllers/import/gitlab_project_controller.rb +++ /dev/null @@ -1,45 +0,0 @@ -class Import::GitlabProjectController < Import::BaseController - before_action :verify_gitlab_project_import_enabled - before_action :gitlab_project_auth, except: :callback - - rescue_from OAuth::Error, with: :gitlab_project_unauthorized - - #TODO permissions stuff - - def callback - - redirect_to status_import_gitlab_project_url - end - - def status - @repos = client.projects - @incompatible_repos = client.incompatible_projects - - @already_added_projects = current_user.created_projects.where(import_type: "gitlab_project") - already_added_projects_names = @already_added_projects.pluck(:import_source) - - @repos.to_a.reject!{ |repo| already_added_projects_names.include? "#{repo["owner"]}/#{repo["slug"]}" } - end - - def jobs - jobs = current_user.created_projects.where(import_type: "gitlab_project").to_json(only: [:id, :import_status]) - render json: jobs - end - - def create - @file = params[:file] - - repo_owner = current_user.username - @target_namespace = params[:new_namespace].presence || repo_owner - - # namespace = get_or_create_namespace || (render and return) - - @project = Gitlab::ImportExport::ImportService.execute(archive_file: file, owner: repo_owner) - end - - private - - def verify_gitlab_project_import_enabled - render_404 unless gitlab_project_import_enabled? - end -end diff --git a/lib/gitlab/import_export/import_export_reader.rb b/lib/gitlab/import_export/import_export_reader.rb index 4e46899ec7e..14049cb1bd2 100644 --- a/lib/gitlab/import_export/import_export_reader.rb +++ b/lib/gitlab/import_export/import_export_reader.rb @@ -41,7 +41,7 @@ module Gitlab 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) + add_to_class(current_key, included_classes_hash, value) else add_new_class(current_key, included_classes_hash, value) end @@ -58,13 +58,18 @@ module Gitlab def add_new_class(current_key, included_classes_hash, value) only_except_hash = check_only_and_except(value) - # TODO: refactor this - value = (value.is_a?(Hash) ? value.merge(only_except_hash) : { value => only_except_hash }) if only_except_hash - new_hash = { include: value } - included_classes_hash[current_key] = new_hash + parsed_hash = { include: value } + unless only_except_hash.empty? + if value.is_a?(Hash) + parsed_hash = { include: value.merge(only_except_hash) } + else + parsed_hash = { include: { value => only_except_hash } } + end + end + included_classes_hash[current_key] = parsed_hash end - def add_class(current_key, included_classes_hash, value) + def add_to_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] diff --git a/lib/gitlab/import_export/import_service.rb b/lib/gitlab/import_export/import_service.rb index 227053481cd..97ee8a7fc90 100644 --- a/lib/gitlab/import_export/import_service.rb +++ b/lib/gitlab/import_export/import_service.rb @@ -9,7 +9,7 @@ module Gitlab def initialize(archive_file:, owner:, namespace_id:, project_path:) @archive_file = archive_file @current_user = owner - @namespace_path = Namespace.find(namespace_id).path + @namespace = Namespace.find(namespace_id) @project_path = project_path end @@ -25,7 +25,7 @@ module Gitlab end def project_tree - @project_tree ||= Gitlab::ImportExport::ProjectTreeRestorer.new(path: storage_path, user: @current_user, project_path: @project_path) + @project_tree ||= Gitlab::ImportExport::ProjectTreeRestorer.new(path: storage_path, user: @current_user, project_path: @project_path, namespace_id: @namespace.id) end def restore_repo @@ -37,7 +37,7 @@ module Gitlab end def path_with_namespace - File.join(@namespace_path, @project_path) + File.join(@namespace.path, @project_path) end end end diff --git a/fixtures/import_export/project.json b/lib/gitlab/import_export/project.json index 4a12c951dcb..4a12c951dcb 100644 --- a/fixtures/import_export/project.json +++ b/lib/gitlab/import_export/project.json diff --git a/lib/gitlab/import_export/project_factory.rb b/lib/gitlab/import_export/project_factory.rb index c7137844a0a..6cd4736649b 100644 --- a/lib/gitlab/import_export/project_factory.rb +++ b/lib/gitlab/import_export/project_factory.rb @@ -3,17 +3,19 @@ module Gitlab module ProjectFactory extend self - def create(project_params:, user:) + def create(project_params:, user:, namespace_id:) project = Project.new(project_params.except('id')) project.creator = user - check_namespace(project_params['namespace_id'], project, user) + check_namespace(namespace_id, project, user) end def check_namespace(namespace_id, project, user) if namespace_id # Find matching namespace and check if it allowed # for current user if namespace_id passed. - unless allowed_namespace?(user, namespace_id) + if allowed_namespace?(user, namespace_id) + project.namespace_id = namespace_id + else project.namespace_id = nil deny_namespace(project) end @@ -34,7 +36,6 @@ module Gitlab def deny_namespace(project) project.errors.add(:namespace, "is not valid") end - end end end diff --git a/lib/gitlab/import_export/project_tree_restorer.rb b/lib/gitlab/import_export/project_tree_restorer.rb index 0f2e3716779..0bc23dfaa24 100644 --- a/lib/gitlab/import_export/project_tree_restorer.rb +++ b/lib/gitlab/import_export/project_tree_restorer.rb @@ -2,10 +2,11 @@ module Gitlab module ImportExport class ProjectTreeRestorer - def initialize(path:, user:, project_path:) + def initialize(path:, user:, project_path:, namespace_id:) @path = File.join(path, 'project.json') @user = user @project_path = project_path + @namespace_id = namespace_id end def restore @@ -30,9 +31,8 @@ module Gitlab saved = [] relation_list.each do |relation| next if !relation.is_a?(Hash) && tree_hash[relation.to_s].blank? - if relation.is_a?(Hash) - create_sub_relations(relation, tree_hash) - end + create_sub_relations(relation, tree_hash) if relation.is_a?(Hash) + relation_key = relation.is_a?(Hash) ? relation.keys.first : relation relation_hash = create_relation(relation_key, tree_hash[relation_key.to_s]) saved << project.update_attribute(relation_key, relation_hash) @@ -47,7 +47,7 @@ module Gitlab def create_project project_params = @tree_hash.reject { |_key, value| value.is_a?(Array) } project = Gitlab::ImportExport::ProjectFactory.create( - project_params: project_params, user: @user) + project_params: project_params, user: @user, namespace_id: @namespace_id) project.path = @project_path project.name = @project_path project.save diff --git a/lib/gitlab/import_export/repo_restorer.rb b/lib/gitlab/import_export/repo_restorer.rb index 315ad88ee0c..b34581deeb5 100644 --- a/lib/gitlab/import_export/repo_restorer.rb +++ b/lib/gitlab/import_export/repo_restorer.rb @@ -3,7 +3,7 @@ module Gitlab class RepoRestorer include Gitlab::ImportExport::CommandLineUtil - def initialize(project: , path: ) + def initialize(project:, path:) @project = project # TODO remove magic keyword and move it to a shared config @path = File.join(path, 'project.bundle') diff --git a/spec/lib/gitlab/import_export/project_tree_restorer_spec.rb b/spec/lib/gitlab/import_export/project_tree_restorer_spec.rb index f3d3a57ddd7..aa8a12c4caa 100644 --- a/spec/lib/gitlab/import_export/project_tree_restorer_spec.rb +++ b/spec/lib/gitlab/import_export/project_tree_restorer_spec.rb @@ -4,7 +4,8 @@ describe Gitlab::ImportExport::ProjectTreeRestorer, services: true do describe :restore do let(:user) { create(:user) } - let(:project_tree_restorer) { Gitlab::ImportExport::ProjectTreeRestorer.new(path: "fixtures/import_export/project.json", user: user) } + let(:namespace) { create(:namespace, owner: user) } + let(:project_tree_restorer) { Gitlab::ImportExport::ProjectTreeRestorer.new(path: "lib/gitlab/import_export/", user: user, project_path: 'project', namespace_id: namespace.id) } context 'JSON' do let(:restored_project_json) do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 596d607f2a1..b48e54b78fb 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -58,3 +58,7 @@ RSpec::Rails::ViewRendering::EmptyTemplatePathSetDecorator.class_eval do end ActiveRecord::Migration.maintain_test_schema! + +Capybara.register_driver :poltergeist do |app| + Capybara::Poltergeist::Driver.new(app, timeout: 1.minute) +end
\ No newline at end of file |