Create rake task to create project templates

First iteration, and some stuff is missing. But basically this rake task
does a clone of a project we've pointed it to. Than creates a project on
the GDK, which should be running in the background. This project is
exported, after which we move that archive to the location we need it.
We clean up by removing the generated project.

The first idea was to export the project on .com too, however than we
might run into ImportExport versions mismatch. This could've been
circumvented by checkout out an older commit locally. This however is
not needed yet, so we opted to not go this route yet, instead we will
iterate on what we got.
This commit is contained in:
Z.J. van de Weg 2017-08-01 12:38:10 +02:00
parent 74b58131d4
commit 09974de3e3
No known key found for this signature in database
GPG key ID: 65F6A8D64A88ABAC
3 changed files with 59 additions and 4 deletions

View file

@ -11,13 +11,17 @@ module Gitlab
end end
def file def file
template_archive.open archive_path.open
end end
def template_archive def archive_path
Rails.root.join("vendor/project_templates/#{name}.tar.gz") Rails.root.join("vendor/project_templates/#{name}.tar.gz")
end end
def clone_url
"https://gitlab.com/gitlab-org/project-templates/#{name}.git"
end
def ==(other) def ==(other)
name == other.name && title == other.title name == other.name && title == other.title
end end
@ -34,6 +38,10 @@ module Gitlab
def find(name) def find(name)
all.find { |template| template.name == name.to_s } all.find { |template| template.name == name.to_s }
end end
def archive_directory
Rails.root.join("vendor_directory/project_templates")
end
end end
end end
end end

View file

@ -4,6 +4,53 @@ namespace :gitlab do
TEMPLATE_DATA.each { |template| update(template) } TEMPLATE_DATA.each { |template| update(template) }
end end
desc "GitLab | Update project templates"
task :update_project_templates do
if Rails.env.production?
puts "This rake task is not meant fo production instances".red
exit(1)
end
admin = User.find_by(admin: true)
unless admin
puts "No admin user could be found".red
exit(1)
end
Gitlab::ProjectTemplate.all.each do |template|
params = {
import_url: template.clone_url,
namespace_id: admin.namespace.id,
path: template.title,
skip_wiki: true
}
puts "Creating project for #{template.name}"
project = Projects::CreateService.new(admin, project).execute
loop do
if project.import_status == "finished"
puts "Import finished for #{template.name}"
break
end
if project.import_status == "failed"
puts "Failed to import from #{project_params[:import_url]}".red
exit(1)
end
puts "Waiting for the import to finish"
sleep(5)
project = project.reload
end
Projects::ImportExport::ExportService.new(project, admin).execute
FileUtils.cp(project.export_project_path, template.archive_path)
Projects::DestroyService.new(admin, project).execute
puts "Exported #{template.name}".green
end
puts "Done".green
end
def update(template) def update(template)
sub_dir = template.repo_url.match(/([A-Za-z-]+)\.git\z/)[1] sub_dir = template.repo_url.match(/([A-Za-z-]+)\.git\z/)[1]
dir = File.join(vendor_directory, sub_dir) dir = File.join(vendor_directory, sub_dir)

View file

@ -31,13 +31,13 @@ describe Gitlab::ProjectTemplate do
describe 'instance methods' do describe 'instance methods' do
subject { described_class.new('phoenix', 'Phoenix Framework') } subject { described_class.new('phoenix', 'Phoenix Framework') }
it { is_expected.to respond_to(:logo_path, :file, :template_archive) } it { is_expected.to respond_to(:logo_path, :file, :archive_path) }
end end
describe 'validate all templates' do describe 'validate all templates' do
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.template_archive archive = template.archive_path
logo = Rails.root.join("app/assets/images/#{template.logo_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)