2017-04-06 08:47:40 -04:00
|
|
|
require 'benchmark'
|
|
|
|
require 'rainbow/ext/string'
|
|
|
|
|
|
|
|
class GithubImport
|
|
|
|
def self.run!(*args)
|
|
|
|
new(*args).run!
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(token, gitlab_username, project_path, extras)
|
2017-10-18 15:46:05 -04:00
|
|
|
@options = { token: token }
|
2017-04-06 08:47:40 -04:00
|
|
|
@project_path = project_path
|
2018-10-18 05:06:44 -04:00
|
|
|
@current_user = UserFinder.new(gitlab_username).find_by_username
|
2018-06-11 11:51:56 -04:00
|
|
|
|
|
|
|
raise "GitLab user #{gitlab_username} not found. Please specify a valid username." unless @current_user
|
|
|
|
|
2017-04-06 08:47:40 -04:00
|
|
|
@github_repo = extras.empty? ? nil : extras.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def run!
|
2017-11-08 12:17:27 -05:00
|
|
|
@repo = GithubRepos
|
|
|
|
.new(@options[:token], @current_user, @github_repo)
|
|
|
|
.choose_one!
|
2017-04-06 08:47:40 -04:00
|
|
|
|
|
|
|
raise 'No repo found!' unless @repo
|
|
|
|
|
|
|
|
show_warning!
|
|
|
|
|
|
|
|
@project = Project.find_by_full_path(@project_path) || new_project
|
|
|
|
|
|
|
|
import!
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def show_warning!
|
2017-11-08 12:17:27 -05:00
|
|
|
puts "This will import GitHub #{@repo.full_name.bright} into GitLab #{@project_path.bright} as #{@current_user.name}"
|
2017-04-06 08:47:40 -04:00
|
|
|
puts "Permission checks are ignored. Press any key to continue.".color(:red)
|
|
|
|
|
|
|
|
STDIN.getch
|
|
|
|
|
2017-04-19 21:54:09 -04:00
|
|
|
puts 'Starting the import (this could take a while)'.color(:green)
|
2017-04-06 08:47:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def import!
|
2017-06-01 10:27:35 -04:00
|
|
|
@project.force_import_start
|
2017-04-06 08:47:40 -04:00
|
|
|
|
2017-09-27 09:57:10 -04:00
|
|
|
import_success = false
|
|
|
|
|
2017-04-06 08:47:40 -04:00
|
|
|
timings = Benchmark.measure do
|
2017-10-18 15:46:05 -04:00
|
|
|
import_success = Gitlab::GithubImport::SequentialImporter
|
|
|
|
.new(@project, token: @options[:token])
|
|
|
|
.execute
|
2017-04-06 08:47:40 -04:00
|
|
|
end
|
|
|
|
|
2017-09-27 09:57:10 -04:00
|
|
|
if import_success
|
2018-06-10 01:35:06 -04:00
|
|
|
@project.after_import
|
2017-09-27 09:57:10 -04:00
|
|
|
puts "Import finished. Timings: #{timings}".color(:green)
|
|
|
|
else
|
|
|
|
puts "Import was not successful. Errors were as follows:"
|
|
|
|
puts @project.import_error
|
|
|
|
end
|
2017-04-06 08:47:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def new_project
|
|
|
|
Project.transaction do
|
|
|
|
namespace_path, _sep, name = @project_path.rpartition('/')
|
|
|
|
namespace = find_or_create_namespace(namespace_path)
|
|
|
|
|
2017-10-05 04:46:30 -04:00
|
|
|
project = Projects::CreateService.new(
|
2017-04-19 21:54:09 -04:00
|
|
|
@current_user,
|
2017-04-06 08:47:40 -04:00
|
|
|
name: name,
|
|
|
|
path: name,
|
2017-11-08 12:17:27 -05:00
|
|
|
description: @repo.description,
|
2017-04-19 21:54:09 -04:00
|
|
|
namespace_id: namespace.id,
|
2017-04-06 08:47:40 -04:00
|
|
|
visibility_level: visibility_level,
|
2017-11-08 12:17:27 -05:00
|
|
|
skip_wiki: @repo.has_wiki
|
2017-04-19 21:54:09 -04:00
|
|
|
).execute
|
2017-10-05 04:46:30 -04:00
|
|
|
|
|
|
|
project.update!(
|
|
|
|
import_type: 'github',
|
2017-11-08 12:17:27 -05:00
|
|
|
import_source: @repo.full_name,
|
|
|
|
import_url: @repo.clone_url.sub('://', "://#{@options[:token]}@")
|
2017-10-05 04:46:30 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
project
|
2017-04-06 08:47:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_or_create_namespace(names)
|
|
|
|
return @current_user.namespace if names == @current_user.namespace_path
|
|
|
|
return @current_user.namespace unless @current_user.can_create_group?
|
|
|
|
|
2017-08-22 06:13:25 -04:00
|
|
|
Groups::NestedCreateService.new(@current_user, group_path: names).execute
|
2017-04-06 08:47:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def full_path_namespace(names)
|
|
|
|
@full_path_namespace ||= Namespace.find_by_full_path(names)
|
|
|
|
end
|
|
|
|
|
|
|
|
def visibility_level
|
2017-11-08 12:17:27 -05:00
|
|
|
@repo.private ? Gitlab::VisibilityLevel::PRIVATE : Gitlab::CurrentSettings.current_application_settings.default_project_visibility
|
2017-04-06 08:47:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class GithubRepos
|
2017-11-08 12:17:27 -05:00
|
|
|
def initialize(token, current_user, github_repo)
|
2017-11-08 15:20:46 -05:00
|
|
|
@client = Gitlab::GithubImport::Client.new(token)
|
|
|
|
@client.octokit.auto_paginate = true
|
2017-11-08 12:17:27 -05:00
|
|
|
|
2017-04-06 08:47:40 -04:00
|
|
|
@current_user = current_user
|
|
|
|
@github_repo = github_repo
|
|
|
|
end
|
|
|
|
|
|
|
|
def choose_one!
|
|
|
|
return found_github_repo if @github_repo
|
|
|
|
|
|
|
|
repos.each do |repo|
|
2017-11-08 12:17:27 -05:00
|
|
|
print "ID: #{repo.id.to_s.bright}".color(:green)
|
|
|
|
print "\tName: #{repo.full_name}\n".color(:green)
|
2017-04-06 08:47:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
print 'ID? '.bright
|
|
|
|
|
2017-11-08 12:17:27 -05:00
|
|
|
repos.find { |repo| repo.id == repo_id }
|
2017-04-06 08:47:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def found_github_repo
|
2017-11-08 12:17:27 -05:00
|
|
|
repos.find { |repo| repo.full_name == @github_repo }
|
2017-04-06 08:47:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def repo_id
|
|
|
|
@repo_id ||= STDIN.gets.chomp.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
def repos
|
2017-11-08 15:20:46 -05:00
|
|
|
@client.octokit.list_repositories
|
2017-04-06 08:47:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
namespace :import do
|
|
|
|
desc 'Import a GitHub project - Example: import:github[ToKeN,root,root/blah,my/github_repo] (optional my/github_repo)'
|
|
|
|
task :github, [:token, :gitlab_username, :project_path] => :environment do |_t, args|
|
|
|
|
abort 'Project path must be: namespace(s)/project_name'.color(:red) unless args.project_path.include?('/')
|
|
|
|
|
|
|
|
GithubImport.run!(args.token, args.gitlab_username, args.project_path, args.extras)
|
|
|
|
end
|
|
|
|
end
|