From 09974de3e3d6409e24786c116fc084c6d834fed8 Mon Sep 17 00:00:00 2001 From: "Z.J. van de Weg" Date: Tue, 1 Aug 2017 12:38:10 +0200 Subject: 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. --- lib/gitlab/project_template.rb | 12 ++++++-- lib/tasks/gitlab/update_templates.rake | 47 ++++++++++++++++++++++++++++++++ spec/lib/gitlab/project_template_spec.rb | 4 +-- 3 files changed, 59 insertions(+), 4 deletions(-) diff --git a/lib/gitlab/project_template.rb b/lib/gitlab/project_template.rb index 72fcda154c5..01f9492c860 100644 --- a/lib/gitlab/project_template.rb +++ b/lib/gitlab/project_template.rb @@ -11,13 +11,17 @@ module Gitlab end def file - template_archive.open + archive_path.open end - def template_archive + def archive_path Rails.root.join("vendor/project_templates/#{name}.tar.gz") end + def clone_url + "https://gitlab.com/gitlab-org/project-templates/#{name}.git" + end + def ==(other) name == other.name && title == other.title end @@ -34,6 +38,10 @@ module Gitlab def find(name) all.find { |template| template.name == name.to_s } end + + def archive_directory + Rails.root.join("vendor_directory/project_templates") + end end end end diff --git a/lib/tasks/gitlab/update_templates.rake b/lib/tasks/gitlab/update_templates.rake index 59c32bbe7a4..26f6276e84b 100644 --- a/lib/tasks/gitlab/update_templates.rake +++ b/lib/tasks/gitlab/update_templates.rake @@ -4,6 +4,53 @@ namespace :gitlab do TEMPLATE_DATA.each { |template| update(template) } 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) sub_dir = template.repo_url.match(/([A-Za-z-]+)\.git\z/)[1] dir = File.join(vendor_directory, sub_dir) diff --git a/spec/lib/gitlab/project_template_spec.rb b/spec/lib/gitlab/project_template_spec.rb index 5dc6059b49c..d95dab748fe 100644 --- a/spec/lib/gitlab/project_template_spec.rb +++ b/spec/lib/gitlab/project_template_spec.rb @@ -31,13 +31,13 @@ describe Gitlab::ProjectTemplate do describe 'instance methods' do 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 describe 'validate all templates' do described_class.all.each do |template| 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}") expect(File.exist?(archive)).to be(true) -- cgit v1.2.1