Commit e38fa32822a34f8dbb7bb9af6885f30a5f0ce2a7

Authored by Jeremy Mack
1 parent 5bc97fbd

Calling update_repository

- Better comment too
Showing 1 changed file with 94 additions and 0 deletions   Show diff stats
lib/tasks/bulk_import.rake 0 → 100644
... ... @@ -0,0 +1,94 @@
  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 + puts " INFO: #{clone_path} already exists in repositories directory, skipping."
  24 + skipped_count += 1
  25 + next
  26 + else
  27 + if clone_bare_repo_as_git(repo_full_path, clone_path)
  28 + if create_repo_project(repo_name, user_email)
  29 + imported_count += 1
  30 + else
  31 + failed_count += 1
  32 + end
  33 + else
  34 + failed_count += 1
  35 + end
  36 + end
  37 +
  38 + end
  39 +
  40 + puts "Finished importing #{imported_count} projects (skipped #{skipped_count}, failed #{failed_count})."
  41 +end
  42 +
  43 +# Clones a repo as bare git repo using the git user
  44 +def clone_bare_repo_as_git(existing_path, new_path)
  45 + begin
  46 + sh "sudo -u git -i git clone --bare '#{existing_path}' #{new_path}"
  47 + true
  48 + rescue
  49 + puts " ERROR: Faild to clone #{existing_path} to #{new_path}"
  50 + false
  51 + end
  52 +end
  53 +
  54 +# Creats a project in Gitlag given a @project_name@ to use (for name, web url, and code
  55 +# url) and a @user_email@ that will be assigned as the owner of the project.
  56 +def create_repo_project(project_name, user_email)
  57 + user = User.find_by_email(user_email)
  58 + if user
  59 + # Using find_by_code since that's the most important identifer to be unique
  60 + if Project.find_by_code(project_name)
  61 + puts " INFO: Project #{project_name} already exists in Gitlab, skipping."
  62 + false
  63 + else
  64 + project = Project.create(
  65 + name: project_name,
  66 + code: project_name,
  67 + path: project_name,
  68 + owner: user,
  69 + description: "Automatically created from CVS on #{Time.now.to_s}"
  70 + )
  71 +
  72 + # Add user as admin for project
  73 + project.users_projects.create!(
  74 + :repo_access => Repository::REPO_RW,
  75 + :project_access => Project::PROJECT_RWA,
  76 + :user => user
  77 + )
  78 +
  79 + # Per projects_controller.rb#37
  80 + project.update_repository
  81 +
  82 + if project.valid?
  83 + true
  84 + else
  85 + puts " ERROR: Failed to create project #{project} because #{project.errors}"
  86 + false
  87 + end
  88 + end
  89 + else
  90 + puts " ERROR: #{user_email} not found, skipping"
  91 + false
  92 + end
  93 +end
  94 +
... ...