diff options
author | Z.J. van de Weg <git@zjvandeweg.nl> | 2017-08-01 12:38:10 +0200 |
---|---|---|
committer | Z.J. van de Weg <git@zjvandeweg.nl> | 2017-08-01 13:00:51 +0200 |
commit | 09974de3e3d6409e24786c116fc084c6d834fed8 (patch) | |
tree | 6078d00a08229946182fe9ad415dea36c4df0997 | |
parent | 74b58131d49ac1379cc8071b1df3867bdbf54eae (diff) | |
download | gitlab-ce-09974de3e3d6409e24786c116fc084c6d834fed8.tar.gz |
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.
-rw-r--r-- | lib/gitlab/project_template.rb | 12 | ||||
-rw-r--r-- | lib/tasks/gitlab/update_templates.rake | 47 | ||||
-rw-r--r-- | 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) |