debian-mirror-gitlab/lib/tasks/import.rake

134 lines
3.5 KiB
Ruby
Raw Normal View History

2021-03-11 19:13:27 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
class GithubImport
2023-04-23 21:23:45 +05:30
def self.run!(...)
new(...).run!
2017-08-17 22:00:37 +05:30
end
def initialize(token, gitlab_username, project_path, extras)
2018-03-17 18:26:18 +05:30
@options = { token: token }
2017-08-17 22:00:37 +05:30
@project_path = project_path
2018-12-13 13:39:08 +05:30
@current_user = UserFinder.new(gitlab_username).find_by_username
2018-11-08 19:23:39 +05:30
raise "GitLab user #{gitlab_username} not found. Please specify a valid username." unless @current_user
2017-08-17 22:00:37 +05:30
@github_repo = extras.empty? ? nil : extras.first
end
def run!
2018-03-17 18:26:18 +05:30
@repo = GithubRepos
.new(@options[:token], @current_user, @github_repo)
.choose_one!
2017-08-17 22:00:37 +05:30
raise 'No repo found!' unless @repo
show_warning!
@project = Project.find_by_full_path(@project_path) || new_project
import!
end
private
def show_warning!
2023-04-23 21:23:45 +05:30
puts "This will import GitHub #{@repo[:full_name].bright} into GitLab #{@project_path.bright} as #{@current_user.name}"
puts 'Permission checks are ignored. Press any key to continue.'.color(:red)
2017-08-17 22:00:37 +05:30
2021-09-04 01:27:46 +05:30
$stdin.getch
2017-08-17 22:00:37 +05:30
puts 'Starting the import (this could take a while)'.color(:green)
end
def import!
2019-02-15 15:39:39 +05:30
@project.import_state.force_start
2017-08-17 22:00:37 +05:30
timings = Benchmark.measure do
2021-11-18 22:05:49 +05:30
Gitlab::GithubImport::SequentialImporter
2018-03-17 18:26:18 +05:30
.new(@project, token: @options[:token])
.execute
2017-08-17 22:00:37 +05:30
end
2021-11-18 22:05:49 +05:30
@project.after_import
puts "Import finished. Timings: #{timings}".color(:green)
2017-08-17 22:00:37 +05:30
end
def new_project
2023-04-23 21:23:45 +05:30
namespace_path, _sep, project_name = @project_path.rpartition('/')
target_namespace = Namespace.find_by_full_path(namespace_path)
raise 'Namespace or group to import repository into does not exist.' unless target_namespace
2017-08-17 22:00:37 +05:30
2023-04-23 21:23:45 +05:30
Project.transaction do
2018-03-17 18:26:18 +05:30
project = Projects::CreateService.new(
2017-08-17 22:00:37 +05:30
@current_user,
2023-04-23 21:23:45 +05:30
name: project_name,
path: project_name,
description: @repo[:description],
namespace_id: target_namespace.id,
2017-08-17 22:00:37 +05:30
visibility_level: visibility_level,
2023-04-23 21:23:45 +05:30
skip_wiki: @repo[:has_wiki]
2017-08-17 22:00:37 +05:30
).execute
2018-03-17 18:26:18 +05:30
project.update!(
import_type: 'github',
2023-04-23 21:23:45 +05:30
import_source: @repo[:full_name],
import_url: @repo[:clone_url].sub('://', "://#{@options[:token]}@")
2018-03-17 18:26:18 +05:30
)
project
2017-08-17 22:00:37 +05:30
end
end
def visibility_level
2023-04-23 21:23:45 +05:30
@repo[:private] ? Gitlab::VisibilityLevel::PRIVATE : Gitlab::CurrentSettings.current_application_settings.default_project_visibility
2017-08-17 22:00:37 +05:30
end
end
class GithubRepos
2018-03-17 18:26:18 +05:30
def initialize(token, current_user, github_repo)
@client = Gitlab::GithubImport::Client.new(token)
@client.octokit.auto_paginate = true
2017-08-17 22:00:37 +05:30
@current_user = current_user
@github_repo = github_repo
end
def choose_one!
return found_github_repo if @github_repo
repos.each do |repo|
2023-04-23 21:23:45 +05:30
print "ID: #{repo[:id].to_s.bright}".color(:green)
print "\tName: #{repo[:full_name]}\n".color(:green)
2017-08-17 22:00:37 +05:30
end
print 'ID? '.bright
2023-04-23 21:23:45 +05:30
repos.find { |repo| repo[:id] == repo_id }
2017-08-17 22:00:37 +05:30
end
def found_github_repo
2023-04-23 21:23:45 +05:30
repos.find { |repo| repo[:full_name] == @github_repo }
2017-08-17 22:00:37 +05:30
end
def repo_id
2021-09-04 01:27:46 +05:30
@repo_id ||= $stdin.gets.chomp.to_i
2017-08-17 22:00:37 +05:30
end
def repos
2023-04-23 21:23:45 +05:30
@repos ||= @client.repos
2017-08-17 22:00:37 +05:30
end
end
namespace :import do
2023-05-27 22:25:52 +05:30
require 'benchmark'
require 'rainbow/ext/string'
2020-03-13 15:44:24 +05:30
desc 'GitLab | Import | Import a GitHub project - Example: import:github[ToKeN,root,root/blah,my/github_repo] (optional my/github_repo)'
2017-08-17 22:00:37 +05:30
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