Commit ced572c328507e3d327f23e9673ba33584845501
Exists in
master
and in
4 other branches
Merge pull request #196 from mutewinter/bulk_repo_import
Bulk repo import Rake task
Showing
1 changed file
with
112 additions
and
0 deletions
Show diff stats
| @@ -0,0 +1,112 @@ | @@ -0,0 +1,112 @@ | ||
| 1 | +IMPORT_DIRECTORY = 'import_projects' | ||
| 2 | +REPOSITORY_DIRECTORY = '/home/git/repositories' | ||
| 3 | + | ||
| 4 | +desc "Imports existing Git repos into new projects from the import_projects folder" | ||
| 5 | +task :import_projects, [:email] => :environment do |t, args| | ||
| 6 | + user_email = args.email | ||
| 7 | + repos_to_import = Dir.glob("#{IMPORT_DIRECTORY}/*") | ||
| 8 | + | ||
| 9 | + puts "Found #{repos_to_import.length} repos to import" | ||
| 10 | + | ||
| 11 | + imported_count = 0 | ||
| 12 | + skipped_count = 0 | ||
| 13 | + failed_count = 0 | ||
| 14 | + repos_to_import.each do |repo_path| | ||
| 15 | + repo_name = File.basename repo_path | ||
| 16 | + repo_full_path = File.join(Rails.root, repo_path) | ||
| 17 | + | ||
| 18 | + puts " Processing #{repo_name}" | ||
| 19 | + | ||
| 20 | + clone_path = "#{REPOSITORY_DIRECTORY}/#{repo_name}.git" | ||
| 21 | + | ||
| 22 | + if Dir.exists? clone_path | ||
| 23 | + if Project.find_by_code(repo_name) | ||
| 24 | + puts " INFO: #{clone_path} already exists in repositories directory, skipping." | ||
| 25 | + skipped_count += 1 | ||
| 26 | + next | ||
| 27 | + else | ||
| 28 | + puts " INFO: Project doesn't exist for #{repo_name} (but the repo does)." | ||
| 29 | + end | ||
| 30 | + else | ||
| 31 | + # Clone the repo | ||
| 32 | + unless clone_bare_repo_as_git(repo_full_path, clone_path) | ||
| 33 | + failed_count += 1 | ||
| 34 | + next | ||
| 35 | + end | ||
| 36 | + end | ||
| 37 | + | ||
| 38 | + # Create the project and repo | ||
| 39 | + if create_repo_project(repo_name, user_email) | ||
| 40 | + imported_count += 1 | ||
| 41 | + else | ||
| 42 | + failed_count += 1 | ||
| 43 | + end | ||
| 44 | + | ||
| 45 | + end | ||
| 46 | + | ||
| 47 | + puts "Finished importing #{imported_count} projects (skipped #{skipped_count}, failed #{failed_count})." | ||
| 48 | +end | ||
| 49 | + | ||
| 50 | +# Clones a repo as bare git repo using the git user | ||
| 51 | +def clone_bare_repo_as_git(existing_path, new_path) | ||
| 52 | + begin | ||
| 53 | + sh "sudo -u git -i git clone --bare '#{existing_path}' #{new_path}" | ||
| 54 | + true | ||
| 55 | + rescue | ||
| 56 | + puts " ERROR: Faild to clone #{existing_path} to #{new_path}" | ||
| 57 | + false | ||
| 58 | + end | ||
| 59 | +end | ||
| 60 | + | ||
| 61 | +# Creats a project in Gitlag given a @project_name@ to use (for name, web url, and code | ||
| 62 | +# url) and a @user_email@ that will be assigned as the owner of the project. | ||
| 63 | +def create_repo_project(project_name, user_email) | ||
| 64 | + user = User.find_by_email(user_email) | ||
| 65 | + if user | ||
| 66 | + # Using find_by_code since that's the most important identifer to be unique | ||
| 67 | + if Project.find_by_code(project_name) | ||
| 68 | + puts " INFO: Project #{project_name} already exists in Gitlab, skipping." | ||
| 69 | + false | ||
| 70 | + else | ||
| 71 | + project = nil | ||
| 72 | + if Project.find_by_code(project_name) | ||
| 73 | + puts " ERROR: Project already exists #{project_name}" | ||
| 74 | + return false | ||
| 75 | + project = Project.find_by_code(project_name) | ||
| 76 | + else | ||
| 77 | + project = Project.create( | ||
| 78 | + name: project_name, | ||
| 79 | + code: project_name, | ||
| 80 | + path: project_name, | ||
| 81 | + owner: user, | ||
| 82 | + description: "Automatically created from Rake on #{Time.now.to_s}" | ||
| 83 | + ) | ||
| 84 | + end | ||
| 85 | + | ||
| 86 | + unless project.valid? | ||
| 87 | + puts " ERROR: Failed to create project #{project} because #{project.errors.first}" | ||
| 88 | + return false | ||
| 89 | + end | ||
| 90 | + | ||
| 91 | + # Add user as admin for project | ||
| 92 | + project.users_projects.create!( | ||
| 93 | + :repo_access => Repository::REPO_RW, | ||
| 94 | + :project_access => Project::PROJECT_RWA, | ||
| 95 | + :user => user | ||
| 96 | + ) | ||
| 97 | + | ||
| 98 | + # Per projects_controller.rb#37 | ||
| 99 | + project.update_repository | ||
| 100 | + | ||
| 101 | + if project.valid? | ||
| 102 | + true | ||
| 103 | + else | ||
| 104 | + puts " ERROR: Failed to create project #{project} because #{project.errors.first}" | ||
| 105 | + false | ||
| 106 | + end | ||
| 107 | + end | ||
| 108 | + else | ||
| 109 | + puts " ERROR: #{user_email} not found, skipping" | ||
| 110 | + false | ||
| 111 | + end | ||
| 112 | +end |