2016-05-27 05:00:56 -04:00
|
|
|
namespace :gitlab do
|
|
|
|
desc "GitLab | Update templates"
|
|
|
|
task :update_templates do
|
2016-06-02 12:20:08 -04:00
|
|
|
TEMPLATE_DATA.each { |template| update(template) }
|
2016-05-27 05:00:56 -04:00
|
|
|
end
|
|
|
|
|
2017-08-01 06:38:10 -04:00
|
|
|
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
|
2018-01-11 11:34:01 -05:00
|
|
|
|
2017-08-01 06:38:10 -04:00
|
|
|
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,
|
2017-08-09 06:16:43 -04:00
|
|
|
path: template.name,
|
2017-08-01 06:38:10 -04:00
|
|
|
skip_wiki: true
|
|
|
|
}
|
2017-08-07 08:21:28 -04:00
|
|
|
|
2017-08-09 06:16:43 -04:00
|
|
|
puts "Creating project for #{template.title}"
|
2017-08-07 08:21:28 -04:00
|
|
|
project = Projects::CreateService.new(admin, params).execute
|
2017-08-01 06:38:10 -04:00
|
|
|
|
2017-08-09 06:16:43 -04:00
|
|
|
unless project.persisted?
|
|
|
|
puts project.errors.messages
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
|
2017-08-01 06:38:10 -04:00
|
|
|
loop do
|
2017-08-07 08:21:28 -04:00
|
|
|
if project.finished?
|
2017-08-01 06:38:10 -04:00
|
|
|
puts "Import finished for #{template.name}"
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
2017-08-07 08:21:28 -04:00
|
|
|
if project.failed?
|
2017-08-01 06:38:10 -04:00
|
|
|
puts "Failed to import from #{project_params[:import_url]}".red
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "Waiting for the import to finish"
|
2017-08-07 09:32:43 -04:00
|
|
|
|
2017-08-01 06:38:10 -04:00
|
|
|
sleep(5)
|
2017-08-07 09:32:43 -04:00
|
|
|
project.reload
|
2017-08-01 06:38:10 -04:00
|
|
|
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
|
|
|
|
|
2016-06-02 12:20:08 -04:00
|
|
|
def update(template)
|
2017-04-26 11:57:44 -04:00
|
|
|
sub_dir = template.repo_url.match(/([A-Za-z-]+)\.git\z/)[1]
|
2016-06-02 12:20:08 -04:00
|
|
|
dir = File.join(vendor_directory, sub_dir)
|
|
|
|
|
|
|
|
unless clone_repository(template.repo_url, dir)
|
|
|
|
puts "Cloning the #{sub_dir} templates failed".red
|
2016-05-27 05:00:56 -04:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-06-02 12:20:08 -04:00
|
|
|
remove_unneeded_files(dir, template.cleanup_regex)
|
2016-05-27 05:00:56 -04:00
|
|
|
puts "Done".green
|
|
|
|
end
|
|
|
|
|
2016-06-02 12:20:08 -04:00
|
|
|
def clone_repository(url, directory)
|
|
|
|
FileUtils.rm_rf(directory) if Dir.exist?(directory)
|
2016-05-27 05:00:56 -04:00
|
|
|
|
2016-06-02 12:20:08 -04:00
|
|
|
system("git clone #{url} --depth=1 --branch=master #{directory}")
|
2016-05-27 05:00:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Retain only certain files:
|
|
|
|
# - The LICENSE, because we have to
|
2016-06-02 12:20:08 -04:00
|
|
|
# - The sub dirs so we can organise the file by category
|
|
|
|
# - The templates themself
|
2016-06-17 02:37:19 -04:00
|
|
|
# - Dir.entries returns also the entries '.' and '..'
|
2016-06-02 12:20:08 -04:00
|
|
|
def remove_unneeded_files(directory, regex)
|
2016-05-27 05:00:56 -04:00
|
|
|
Dir.foreach(directory) do |file|
|
|
|
|
FileUtils.rm_rf(File.join(directory, file)) unless file =~ regex
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-06-02 12:20:08 -04:00
|
|
|
Template = Struct.new(:repo_url, :cleanup_regex)
|
2016-06-16 09:33:11 -04:00
|
|
|
TEMPLATE_DATA = [
|
|
|
|
Template.new(
|
|
|
|
"https://github.com/github/gitignore.git",
|
|
|
|
/(\.{1,2}|LICENSE|Global|\.gitignore)\z/
|
|
|
|
),
|
|
|
|
Template.new(
|
|
|
|
"https://gitlab.com/gitlab-org/gitlab-ci-yml.git",
|
2017-04-12 08:38:59 -04:00
|
|
|
/(\.{1,2}|LICENSE|CONTRIBUTING.md|Pages|autodeploy|\.gitlab-ci.yml)\z/
|
2017-04-26 11:57:44 -04:00
|
|
|
),
|
|
|
|
Template.new(
|
|
|
|
"https://gitlab.com/gitlab-org/Dockerfile.git",
|
|
|
|
/(\.{1,2}|LICENSE|CONTRIBUTING.md|\.Dockerfile)\z/
|
2017-05-03 07:22:03 -04:00
|
|
|
)
|
2017-02-22 13:18:40 -05:00
|
|
|
].freeze
|
2016-05-27 05:00:56 -04:00
|
|
|
|
|
|
|
def vendor_directory
|
|
|
|
Rails.root.join('vendor')
|
|
|
|
end
|
|
|
|
end
|