cleanup rake tasks

This commit is contained in:
Nihad Abbasov 2012-09-26 04:18:10 -07:00
parent 30ee53624b
commit 83696b127b
8 changed files with 77 additions and 120 deletions

View file

@ -1,20 +1,17 @@
desc "Imports existing Git repos from a directory into new projects in git_base_path"
task :import_projects, [:directory,:email] => :environment do |t, args|
user_email = args.email
import_directory = args.directory
user_email, import_directory = args.email, args.directory
repos_to_import = Dir.glob("#{import_directory}/*")
git_base_path = Gitlab.config.git_base_path
puts "Found #{repos_to_import.length} repos to import"
imported_count, skipped_count, failed_count = 0
puts "Found #{repos_to_import.size} repos to import"
imported_count = 0
skipped_count = 0
failed_count = 0
repos_to_import.each do |repo_path|
repo_name = File.basename repo_path
clone_path = "#{git_base_path}#{repo_name}.git"
puts " Processing #{repo_name}"
clone_path = "#{git_base_path}#{repo_name}.git"
if Dir.exists? clone_path
if Project.find_by_code(repo_name)
@ -38,7 +35,6 @@ task :import_projects, [:directory,:email] => :environment do |t, args|
else
failed_count += 1
end
end
puts "Finished importing #{imported_count} projects (skipped #{skipped_count}, failed #{failed_count})."
@ -49,63 +45,39 @@ def clone_bare_repo_as_git(existing_path, new_path)
git_user = Gitlab.config.ssh_user
begin
sh "sudo -u #{git_user} -i git clone --bare '#{existing_path}' #{new_path}"
true
rescue Exception=> msg
puts " ERROR: Faild to clone #{existing_path} to #{new_path}"
puts " Make sure #{git_user} can reach #{existing_path}"
puts " Exception-MSG: #{msg}"
false
rescue Exception => msg
puts " ERROR: Failed to clone #{existing_path} to #{new_path}"
puts " Make sure #{git_user} can reach #{existing_path}"
puts " Exception-MSG: #{msg}"
end
end
# Creats a project in Gitlag given a @project_name@ to use (for name, web url, and code
# url) and a @user_email@ that will be assigned as the owner of the project.
# Creates a project in GitLab given a `project_name` to use
# (for name, web url, and code url) and a `user_email` that will be
# assigned as the owner of the project.
def create_repo_project(project_name, user_email)
user = User.find_by_email(user_email)
if user
if user = User.find_by_email(user_email)
# Using find_by_code since that's the most important identifer to be unique
if Project.find_by_code(project_name)
puts " INFO: Project #{project_name} already exists in Gitlab, skipping."
false
else
project = nil
if Project.find_by_code(project_name)
puts " ERROR: Project already exists #{project_name}"
return false
project = Project.find_by_code(project_name)
else
project = Project.create(
name: project_name,
code: project_name,
path: project_name,
owner: user,
description: "Automatically created from Rake on #{Time.now.to_s}"
)
end
unless project.valid?
puts " ERROR: Failed to create project #{project} because #{project.errors.first}"
return false
end
# Add user as admin for project
project.users_projects.create!(
:project_access => UsersProject::MASTER,
:user => user
project = Project.create(
name: project_name,
code: project_name,
path: project_name,
owner: user,
description: "Automatically created from 'import_projects' rake task on #{Time.now}"
)
# Per projects_controller.rb#37
project.update_repository
if project.valid?
true
# Add user as admin for project
project.users_projects.create!(:project_access => UsersProject::MASTER, :user => user)
project.update_repository
else
puts " ERROR: Failed to create project #{project} because #{project.errors.first}"
false
end
end
else
puts " ERROR: #{user_email} not found, skipping"
false
puts " ERROR: user with #{user_email} not found, skipping"
end
end