Commit 4ef671ae authored by Z.J. van de Weg's avatar Z.J. van de Weg
Browse files

Rename GitLabProjectImporterService and misc fixes

First round of review, main changes:
- templates.title is human readable, #name will be passed around
- GitLabProjectImporterService has been renamed
parent 1764b198ced2
......@@ -329,7 +329,7 @@
:runners_token,
:tag_list,
:visibility_level,
:template_title,
:template_name,
project_feature_attributes: %i[
builds_access_level
......@@ -352,7 +352,7 @@
end
def project_from_template?
project_params[:template_title]&.present?
project_params[:template_name]&.present?
end
def project_view_files?
......
......@@ -71,7 +71,7 @@
attr_accessor :new_default_branch
attr_accessor :old_path_with_namespace
attr_accessor :template_title
attr_accessor :template_name
attr_writer :pipeline_status
alias_attribute :title, :name
......
......@@ -5,5 +5,5 @@
end
def execute
params[:file] = Gitlab::ProjectTemplate.find(params[:template_title]).file
params[:file] = Gitlab::ProjectTemplate.find(params[:template_name]).file
......@@ -9,6 +9,7 @@
@params[:from_template] = true
GitlabProjectsImporterService.new(@current_user, @params).execute
GitlabProjectsImportService.new(@current_user, @params).execute
ensure
params[:file]&.close
end
end
end
# This service is an adapter used to for the GitLab Import feature, and
# creating a project from a template.
# The latter will under the hood just import an archive supplied by GitLab.
module Projects
class GitlabProjectsImportService
attr_reader :current_user, :params
def initialize(user, params)
@current_user, @params = user, params.dup
end
def execute
FileUtils.mkdir_p(File.dirname(import_upload_path))
FileUtils.copy_entry(file.path, import_upload_path)
Gitlab::ImportExport::ProjectCreator.new(params[:namespace_id],
current_user,
import_upload_path,
params[:path]).execute
end
private
def import_upload_path
@import_upload_path ||= Gitlab::ImportExport
.import_upload_path(filename: "#{params[:namespace_id]}-#{params[:path]}")
end
def file
params[:file]
end
end
end
# This service is an adapter used to for the GitLab Import feature, and
# creating a project from a template.
# The latter will under the hood just import an archive supplied by GitLab.
module Projects
class GitlabProjectsImporterService
attr_reader :current_user, :params
def initialize(user, params)
@current_user, @params = user, params.dup
end
def execute
FileUtils.mkdir_p(File.dirname(import_upload_path))
FileUtils.copy_entry(file.path, import_upload_path)
Gitlab::ImportExport::ProjectCreator.new(params[:namespace_id],
current_user,
import_upload_path,
params[:path]).execute
end
private
def import_upload_path
@import_upload_path ||= Gitlab::ImportExport
.import_upload_path(filename: "#{params[:namespace_id]}-#{params[:path]}")
end
def file
params[:file]
end
end
end
......@@ -17,7 +17,7 @@
def import_upload_path(filename:)
milliseconds = Process.clock_gettime(Process::CLOCK_REALTIME, :millisecond)
File.join(storage_path, 'uploads', "#{millisecond}-#{filename}")
File.join(storage_path, 'uploads', "#{milliseconds}-#{filename}")
end
def project_filename
......
......@@ -24,9 +24,9 @@
name == other.name && title == other.title
end
TemplatesTable = [
TEMPLATES_TABLE = [
ProjectTemplate.new('rails', 'Ruby on Rails')
].freeze
class << self
def all
......@@ -28,9 +28,9 @@
ProjectTemplate.new('rails', 'Ruby on Rails')
].freeze
class << self
def all
TemplatesTable
TEMPLATES_TABLE
end
def find(name)
......
......@@ -10,11 +10,11 @@
visit new_project_path
end
it "allows creation from the #{template.name} template" do
fill_in("project_template_title", with: template.title)
it "allows creation from templates" do
fill_in("project_template_name", with: template.title)
fill_in("project_path", with: template.name)
page.within '#content-body' do
click_button "Create project"
end
......@@ -15,10 +15,10 @@
fill_in("project_path", with: template.name)
page.within '#content-body' do
click_button "Create project"
end
expect(page).to have_content 'Import'
expect(page).to have_content 'Import in progress'
end
end
......
......@@ -35,6 +35,8 @@
end
describe 'validate all templates' do
set(:admin) { create(:admin) }
described_class.all.each do |template|
it "#{template.name} has a valid archive" do
archive = template.archive_path
......@@ -38,6 +40,5 @@
described_class.all.each do |template|
it "#{template.name} has a valid archive" do
archive = template.archive_path
logo = Rails.root.join("app/assets/images/#{template.logo_path}")
expect(File.exist?(archive)).to be(true)
......@@ -42,6 +43,20 @@
expect(File.exist?(archive)).to be(true)
expect(File.exist?(logo)).to be(true)
end
context 'with valid parameters' do
it 'can be imported' do
params = {
template_name: template.name,
namespace_id: admin.namespace.id,
path: template.name
}
project = Projects::CreateFromTemplateService.new(admin, params).execute
expect(project).to be_valid
expect(project).to be_persisted
end
end
end
end
......
......@@ -12,7 +12,7 @@
subject { described_class.new(user, project_params) }
it 'calls the importer service' do
expect_any_instance_of(Projects::GitlabProjectsImporterService).to receive(:execute)
expect_any_instance_of(Projects::GitlabProjectsImportService).to receive(:execute)
subject.execute
end
......
Supports Markdown
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