Commit a4d242b8 authored by James Lopez's avatar James Lopez

refactored some namespace stuff and fixed project tree restorer spec. also...

refactored some namespace stuff and fixed project tree restorer spec. also removing controller so that it belongs to the UI MR
parent 84c7b518
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
......@@ -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
......
......@@ -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
......@@ -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
......
......@@ -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')
......
......@@ -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
......
......@@ -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
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