Commit 3baf3dc9 authored by Z.J. van de Weg's avatar Z.J. van de Weg

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 a853d3e9
...@@ -329,7 +329,7 @@ class ProjectsController < Projects::ApplicationController ...@@ -329,7 +329,7 @@ class ProjectsController < Projects::ApplicationController
:runners_token, :runners_token,
:tag_list, :tag_list,
:visibility_level, :visibility_level,
:template_title, :template_name,
project_feature_attributes: %i[ project_feature_attributes: %i[
builds_access_level builds_access_level
...@@ -352,7 +352,7 @@ class ProjectsController < Projects::ApplicationController ...@@ -352,7 +352,7 @@ class ProjectsController < Projects::ApplicationController
end end
def project_from_template? def project_from_template?
project_params[:template_title]&.present? project_params[:template_name]&.present?
end end
def project_view_files? def project_view_files?
......
...@@ -71,7 +71,7 @@ class Project < ActiveRecord::Base ...@@ -71,7 +71,7 @@ class Project < ActiveRecord::Base
attr_accessor :new_default_branch attr_accessor :new_default_branch
attr_accessor :old_path_with_namespace attr_accessor :old_path_with_namespace
attr_accessor :template_title attr_accessor :template_name
attr_writer :pipeline_status attr_writer :pipeline_status
alias_attribute :title, :name alias_attribute :title, :name
......
...@@ -5,10 +5,11 @@ module Projects ...@@ -5,10 +5,11 @@ module Projects
end end
def execute def execute
params[:file] = Gitlab::ProjectTemplate.find(params[:template_title]).file params[:file] = Gitlab::ProjectTemplate.find(params[:template_name]).file
@params[:from_template] = true GitlabProjectsImportService.new(@current_user, @params).execute
GitlabProjectsImporterService.new(@current_user, @params).execute ensure
params[:file]&.close
end end
end end
end end
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
# creating a project from a template. # creating a project from a template.
# The latter will under the hood just import an archive supplied by GitLab. # The latter will under the hood just import an archive supplied by GitLab.
module Projects module Projects
class GitlabProjectsImporterService class GitlabProjectsImportService
attr_reader :current_user, :params attr_reader :current_user, :params
def initialize(user, params) def initialize(user, params)
......
...@@ -17,7 +17,7 @@ module Gitlab ...@@ -17,7 +17,7 @@ module Gitlab
def import_upload_path(filename:) def import_upload_path(filename:)
milliseconds = Process.clock_gettime(Process::CLOCK_REALTIME, :millisecond) milliseconds = Process.clock_gettime(Process::CLOCK_REALTIME, :millisecond)
File.join(storage_path, 'uploads', "#{millisecond}-#{filename}") File.join(storage_path, 'uploads', "#{milliseconds}-#{filename}")
end end
def project_filename def project_filename
......
...@@ -24,13 +24,13 @@ module Gitlab ...@@ -24,13 +24,13 @@ module Gitlab
name == other.name && title == other.title name == other.name && title == other.title
end end
TemplatesTable = [ TEMPLATES_TABLE = [
ProjectTemplate.new('rails', 'Ruby on Rails') ProjectTemplate.new('rails', 'Ruby on Rails')
].freeze ].freeze
class << self class << self
def all def all
TemplatesTable TEMPLATES_TABLE
end end
def find(name) def find(name)
......
...@@ -10,15 +10,15 @@ feature 'Project', feature: true do ...@@ -10,15 +10,15 @@ feature 'Project', feature: true do
visit new_project_path visit new_project_path
end end
it "allows creation from the #{template.name} template" do it "allows creation from templates" do
fill_in("project_template_title", with: template.title) fill_in("project_template_name", with: template.title)
fill_in("project_path", with: template.name) fill_in("project_path", with: template.name)
page.within '#content-body' do page.within '#content-body' do
click_button "Create project" click_button "Create project"
end end
expect(page).to have_content 'Import' expect(page).to have_content 'Import in progress'
end end
end end
......
...@@ -35,13 +35,28 @@ describe Gitlab::ProjectTemplate do ...@@ -35,13 +35,28 @@ describe Gitlab::ProjectTemplate do
end end
describe 'validate all templates' do describe 'validate all templates' do
set(:admin) { create(:admin) }
described_class.all.each do |template| described_class.all.each do |template|
it "#{template.name} has a valid archive" do it "#{template.name} has a valid archive" do
archive = template.archive_path archive = template.archive_path
logo = Rails.root.join("app/assets/images/#{template.logo_path}")
expect(File.exist?(archive)).to be(true) 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 end
end end
......
...@@ -12,7 +12,7 @@ describe Projects::CreateFromTemplateService do ...@@ -12,7 +12,7 @@ describe Projects::CreateFromTemplateService do
subject { described_class.new(user, project_params) } subject { described_class.new(user, project_params) }
it 'calls the importer service' do 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 subject.execute
end end
......
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