Commit 419db8f66a7 by Georges Racinet

WIP rake task for BB import

--HG--
branch : heptapod
parent f9eaae60cb8
......@@ -134,6 +134,88 @@ class GithubRepos
end
end
class BitbucketImport
def self.run!(*args)
new(*args).run!
end
def initialize(token, gitlab_username, project_path, bb_repo_path)
puts "token: #{token}"
puts "gitlab username: #{gitlab_username}"
puts "project_path: #{project_path}"
puts "bb_repo_path: #{bb_repo_path}"
@project_path = project_path
@current_user = User.find_by_username(gitlab_username)
@bb_credentials = {
token: token,
# TODO might be required
# in any case, to get a valid token, user have to do the OAuth dance
# on their own rig first.
# expires_at: session[:bitbucket_expires_at],
# expires_in: session[:bitbucket_expires_in],
# refresh_token: session[:bitbucket_refresh_token]
}
@bb_client = Bitbucket::Client.new(@bb_credentials)
@bb_repo = @bb_client.repo(bb_repo_path)
end
def run!
show_warning!
@project = Project.find_by_full_path(@project_path) || new_project
import!
end
private
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?
Groups::NestedCreateService.new(@current_user, group_path: names).execute
end
def new_project
Project.transaction do
namespace_path, _slash, project_name = @project_path.rpartition('/')
@target_namespace = find_or_create_namespace(namespace_path)
Gitlab::BitbucketImport::ProjectCreator.new(@bb_repo, project_name, @target_namespace, @current_user, @bb_credentials).execute
end
end
def show_warning!
puts "This will import Bitbucket's #{@bb_repo}"
puts "into GitLab #{@project_path.bright}"
puts "as #{@current_user.name}"
puts "Permission checks are ignored. Press any key to continue.".color(:red)
STDIN.getch
puts 'Starting the import (this could take a while)'.color(:green)
end
def import!
@project.force_import_start
import_success = false
timings = Benchmark.measure do
import_success = Gitlab::BitbucketImport::Importer
.new(@project)
.execute
end
if import_success
@project.import_finish
puts "Import finished. Timings: #{timings}".color(:green)
else
puts "Import was not successful. Errors were as follows:"
puts @project.import_error
end
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|
......@@ -141,4 +223,10 @@ namespace :import do
GithubImport.run!(args.token, args.gitlab_username, args.project_path, args.extras)
end
desc 'Import a Bitbucket project - only public projects for the time being'
task :bitbucket, [:token, :gitlab_username, :project_path, :bb_project_path] => :environment do |_t, args|
puts "input args:", args
BitbucketImport.run!(args.token, args.gitlab_username, args.project_path, args.bb_project_path)
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment