From a4b6707e63ca10eb64a92d45797eaad49e9c9f46 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Fri, 6 Jul 2018 16:21:22 +0300 Subject: Refactor manifest import code Signed-off-by: Dmitriy Zaporozhets --- app/controllers/import/manifest_controller.rb | 2 +- doc/user/project/import/img/manifest_upload.png | Bin 17455 -> 11595 bytes doc/user/project/import/manifest.md | 2 +- lib/gitlab/manifest_import/importer.rb | 46 --------------------- lib/gitlab/manifest_import/project_creator.rb | 42 +++++++++++++++++++ spec/lib/gitlab/import_sources_spec.rb | 3 +- spec/lib/gitlab/manifest_import/importer_spec.rb | 33 --------------- .../gitlab/manifest_import/project_creator_spec.rb | 33 +++++++++++++++ 8 files changed, 78 insertions(+), 83 deletions(-) delete mode 100644 lib/gitlab/manifest_import/importer.rb create mode 100644 lib/gitlab/manifest_import/project_creator.rb delete mode 100644 spec/lib/gitlab/manifest_import/importer_spec.rb create mode 100644 spec/lib/gitlab/manifest_import/project_creator_spec.rb diff --git a/app/controllers/import/manifest_controller.rb b/app/controllers/import/manifest_controller.rb index 0be9cc2468f..42e661325cf 100644 --- a/app/controllers/import/manifest_controller.rb +++ b/app/controllers/import/manifest_controller.rb @@ -47,7 +47,7 @@ class Import::ManifestController < Import::BaseController project[:id] == params[:repo_id].to_i end - project = Gitlab::ManifestImport::Importer.new(repository, group, current_user).execute + project = Gitlab::ManifestImport::ProjectCreator.new(repository, group, current_user).execute if project.persisted? render json: ProjectSerializer.new.represent(project) diff --git a/doc/user/project/import/img/manifest_upload.png b/doc/user/project/import/img/manifest_upload.png index 385df3aa8ce..ea68e7ffc6a 100644 Binary files a/doc/user/project/import/img/manifest_upload.png and b/doc/user/project/import/img/manifest_upload.png differ diff --git a/doc/user/project/import/manifest.md b/doc/user/project/import/manifest.md index 16c7e77d033..136ba310da1 100644 --- a/doc/user/project/import/manifest.md +++ b/doc/user/project/import/manifest.md @@ -10,7 +10,7 @@ You can do it by following next steps: 1. Click on the **Manifest file** button 1. Provide GitLab with a manifest xml file 1. Select a group you want to import to (you need to create a group first if you don't have one) -1. Click **Import projects** +1. Click **Continue to the next step** 1. You will be redirected to the import status page with projects list based on manifest file 1. Check the list and click 'Import all repositories' to start import. diff --git a/lib/gitlab/manifest_import/importer.rb b/lib/gitlab/manifest_import/importer.rb deleted file mode 100644 index 8ffeb0a4143..00000000000 --- a/lib/gitlab/manifest_import/importer.rb +++ /dev/null @@ -1,46 +0,0 @@ -module Gitlab - module ManifestImport - class Importer - attr_reader :repository, :destination, :user - - def initialize(repository, destination, user) - @repository = repository - @destination = destination - @user = user - end - - def execute - import_project - end - - private - - def import_project - group_full_path, _, project_path = repository[:path].rpartition('/') - group_full_path = File.join(destination.full_path, group_full_path) if destination - group = Group.find_by_full_path(group_full_path) || - create_group_with_parents(group_full_path) - - params = { - import_url: repository[:url], - import_type: 'manifest', - namespace_id: group.id, - path: project_path, - name: project_path, - visibility_level: destination.visibility_level - } - - Projects::CreateService.new(user, params).execute - end - - def create_group_with_parents(full_path) - params = { - group_path: full_path, - visibility_level: destination.visibility_level - } - - Groups::NestedCreateService.new(user, params).execute - end - end - end -end diff --git a/lib/gitlab/manifest_import/project_creator.rb b/lib/gitlab/manifest_import/project_creator.rb new file mode 100644 index 00000000000..9ccd32c3a3b --- /dev/null +++ b/lib/gitlab/manifest_import/project_creator.rb @@ -0,0 +1,42 @@ +module Gitlab + module ManifestImport + class ProjectCreator + attr_reader :repository, :destination, :current_user + + def initialize(repository, destination, current_user) + @repository = repository + @destination = destination + @current_user = current_user + end + + def execute + group_full_path, _, project_path = repository[:path].rpartition('/') + group_full_path = File.join(destination.full_path, group_full_path) if destination + group = Group.find_by_full_path(group_full_path) || + create_group_with_parents(group_full_path) + + params = { + import_url: repository[:url], + import_type: 'manifest', + namespace_id: group.id, + path: project_path, + name: project_path, + visibility_level: destination.visibility_level + } + + Projects::CreateService.new(current_user, params).execute + end + + private + + def create_group_with_parents(full_path) + params = { + group_path: full_path, + visibility_level: destination.visibility_level + } + + Groups::NestedCreateService.new(current_user, params).execute + end + end + end +end diff --git a/spec/lib/gitlab/import_sources_spec.rb b/spec/lib/gitlab/import_sources_spec.rb index 7734f48de86..25827423914 100644 --- a/spec/lib/gitlab/import_sources_spec.rb +++ b/spec/lib/gitlab/import_sources_spec.rb @@ -50,7 +50,6 @@ describe Gitlab::ImportSources do fogbugz gitlab_project gitea - manifest ) expect(described_class.importer_names).to eq(expected) @@ -67,7 +66,7 @@ describe Gitlab::ImportSources do 'git' => nil, 'gitlab_project' => Gitlab::ImportExport::Importer, 'gitea' => Gitlab::LegacyGithubImport::Importer, - 'manifest' => Gitlab::ManifestImport::Importer + 'manifest' => nil } import_sources.each do |name, klass| diff --git a/spec/lib/gitlab/manifest_import/importer_spec.rb b/spec/lib/gitlab/manifest_import/importer_spec.rb deleted file mode 100644 index 55f3c347386..00000000000 --- a/spec/lib/gitlab/manifest_import/importer_spec.rb +++ /dev/null @@ -1,33 +0,0 @@ -require 'spec_helper' - -describe Gitlab::ManifestImport::Importer, :postgresql do - let(:group) { create(:group) } - let(:user) { create(:user) } - let(:repository) do - { - path: 'device/common', - url: 'https://android-review.googlesource.com/device/common' - } - end - - before do - group.add_owner(user) - end - - subject { described_class.new(repository, group, user) } - - describe '#execute' do - it { expect(subject.execute).to be_a(Project) } - it { expect { subject.execute }.to change { Project.count }.by(1) } - it { expect { subject.execute }.to change { Group.count }.by(1) } - - it 'creates project with valid full path and import url' do - subject.execute - - project = Project.last - - expect(project.full_path).to eq(File.join(group.path, 'device/common')) - expect(project.import_url).to eq('https://android-review.googlesource.com/device/common') - end - end -end diff --git a/spec/lib/gitlab/manifest_import/project_creator_spec.rb b/spec/lib/gitlab/manifest_import/project_creator_spec.rb new file mode 100644 index 00000000000..1d01d437535 --- /dev/null +++ b/spec/lib/gitlab/manifest_import/project_creator_spec.rb @@ -0,0 +1,33 @@ +require 'spec_helper' + +describe Gitlab::ManifestImport::ProjectCreator, :postgresql do + let(:group) { create(:group) } + let(:user) { create(:user) } + let(:repository) do + { + path: 'device/common', + url: 'https://android-review.googlesource.com/device/common' + } + end + + before do + group.add_owner(user) + end + + subject { described_class.new(repository, group, user) } + + describe '#execute' do + it { expect(subject.execute).to be_a(Project) } + it { expect { subject.execute }.to change { Project.count }.by(1) } + it { expect { subject.execute }.to change { Group.count }.by(1) } + + it 'creates project with valid full path and import url' do + subject.execute + + project = Project.last + + expect(project.full_path).to eq(File.join(group.path, 'device/common')) + expect(project.import_url).to eq('https://android-review.googlesource.com/device/common') + end + end +end -- cgit v1.2.1