Commit 3fd3e1fcdf69e8544b25193a6e2a53716c65096c

Authored by Jeremy Mack
1 parent 60fee48e

Added detection and handling of exsiting repos

Showing 1 changed file with 37 additions and 18 deletions   Show diff stats
lib/tasks/bulk_import.rake
... ... @@ -20,21 +20,28 @@ task :import_projects, [:email] => :environment do |t, args|
20 20 clone_path = "#{REPOSITORY_DIRECTORY}/#{repo_name}.git"
21 21  
22 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
  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
33 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)
34 33 failed_count += 1
  34 + next
35 35 end
36 36 end
37 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 +
38 45 end
39 46  
40 47 puts "Finished importing #{imported_count} projects (skipped #{skipped_count}, failed #{failed_count})."
... ... @@ -61,13 +68,25 @@ def create_repo_project(project_name, user_email)
61 68 puts " INFO: Project #{project_name} already exists in Gitlab, skipping."
62 69 false
63 70 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 Rake on #{Time.now.to_s}"
70   - )
  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
71 90  
72 91 # Add user as admin for project
73 92 project.users_projects.create!(
... ... @@ -82,7 +101,7 @@ def create_repo_project(project_name, user_email)
82 101 if project.valid?
83 102 true
84 103 else
85   - puts " ERROR: Failed to create project #{project} because #{project.errors}"
  104 + puts " ERROR: Failed to create project #{project} because #{project.errors.first}"
86 105 false
87 106 end
88 107 end
... ...